-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
395 lines (354 loc) · 8.91 KB
/
Copy pathmain.go
File metadata and controls
395 lines (354 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package main
import (
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
)
type FieldInfo struct {
Name string
Field *ast.Field
TypeSize int
}
func getTypeSize(expr ast.Expr) int {
switch t := expr.(type) {
case *ast.Ident:
switch t.Name {
case "bool":
return 1
case "int8", "uint8", "byte":
return 1
case "int16", "uint16":
return 2
case "int32", "uint32", "rune", "float32":
return 4
case "int64", "uint64", "float64", "complex64":
return 8
case "complex128":
return 16
case "int", "uint", "uintptr":
return 8
case "string":
return 16
}
case *ast.StarExpr:
return 8
case *ast.ArrayType:
if t.Len == nil {
return 24
}
return 8
case *ast.MapType:
return 8
case *ast.ChanType:
return 8
case *ast.InterfaceType:
return 16
case *ast.FuncType:
return 8
}
return 8
}
func analyzeStruct(structType *ast.StructType) ([]FieldInfo, bool) {
if structType.Fields == nil || len(structType.Fields.List) <= 1 {
return nil, false
}
var fields []FieldInfo
for _, field := range structType.Fields.List {
typeSize := getTypeSize(field.Type)
if len(field.Names) > 0 {
for _, name := range field.Names {
fields = append(fields, FieldInfo{
Field: field,
TypeSize: typeSize,
Name: name.Name,
})
}
} else {
fields = append(fields, FieldInfo{
Field: field,
TypeSize: typeSize,
Name: "",
})
}
}
sortedFields := make([]FieldInfo, len(fields))
copy(sortedFields, fields)
sort.Slice(sortedFields, func(i, j int) bool {
if sortedFields[i].TypeSize != sortedFields[j].TypeSize {
return sortedFields[i].TypeSize > sortedFields[j].TypeSize
}
return sortedFields[i].Name < sortedFields[j].Name
})
needsReordering := false
for i, field := range fields {
if field.TypeSize != sortedFields[i].TypeSize || field.Name != sortedFields[i].Name {
needsReordering = true
break
}
}
return sortedFields, needsReordering
}
func generateReorderedStruct(original *ast.StructType, reorderedFields []FieldInfo) *ast.StructType {
newStruct := &ast.StructType{
Struct: original.Struct,
Fields: &ast.FieldList{
Opening: original.Fields.Opening,
Closing: original.Fields.Closing,
},
}
fieldGroups := make(map[*ast.Field][]FieldInfo)
for _, fi := range reorderedFields {
fieldGroups[fi.Field] = append(fieldGroups[fi.Field], fi)
}
var newFields []*ast.Field
processed := make(map[*ast.Field]bool)
for _, fi := range reorderedFields {
if processed[fi.Field] {
continue
}
group := fieldGroups[fi.Field]
if len(group) == 1 {
newField := &ast.Field{
Doc: fi.Field.Doc,
Names: fi.Field.Names,
Type: fi.Field.Type,
Tag: fi.Field.Tag,
Comment: fi.Field.Comment,
}
newFields = append(newFields, newField)
} else {
for _, f := range group {
newField := &ast.Field{
Doc: f.Field.Doc,
Type: f.Field.Type,
Tag: f.Field.Tag,
Comment: f.Field.Comment,
}
if f.Name != "" {
newField.Names = []*ast.Ident{{Name: f.Name}}
}
newFields = append(newFields, newField)
}
}
processed[fi.Field] = true
}
newStruct.Fields.List = newFields
return newStruct
}
func fixStructsInFile(filename string) error {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return fmt.Errorf("failed to parse file: %v", err)
}
hasChanges := false
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.GenDecl:
if x.Tok == token.TYPE {
for _, spec := range x.Specs {
if typeSpec, ok := spec.(*ast.TypeSpec); ok {
if structType, ok := typeSpec.Type.(*ast.StructType); ok {
reorderedFields, needsReordering := analyzeStruct(structType)
if needsReordering {
hasChanges = true
pos := fset.Position(typeSpec.Pos())
fmt.Printf("Fixed struct '%s' at %s:%d:%d\n",
typeSpec.Name.Name, filename, pos.Line, pos.Column)
newStruct := generateReorderedStruct(structType, reorderedFields)
typeSpec.Type = newStruct
}
}
}
}
}
}
return true
})
if hasChanges {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("failed to create file: %v", err)
}
defer file.Close()
if err := format.Node(file, fset, node); err != nil {
return fmt.Errorf("failed to format code: %v", err)
}
fmt.Printf("Successfully fixed %s\n", filename)
} else {
fmt.Printf("✔ %s\n", filename)
}
return nil
}
func lintFile(filename string) error {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return fmt.Errorf("failed to parse file: %v", err)
}
hasIssues := false
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.GenDecl:
if x.Tok == token.TYPE {
for _, spec := range x.Specs {
if typeSpec, ok := spec.(*ast.TypeSpec); ok {
if structType, ok := typeSpec.Type.(*ast.StructType); ok {
reorderedFields, needsReordering := analyzeStruct(structType)
if needsReordering {
hasIssues = true
pos := fset.Position(typeSpec.Pos())
fmt.Printf("\n%s:%d:%d: struct '%s' fields can be reordered for better memory efficiency\n",
filename, pos.Line, pos.Column, typeSpec.Name.Name)
fmt.Println("Current order:")
for _, field := range structType.Fields.List {
if len(field.Names) > 0 {
for _, name := range field.Names {
size := getTypeSize(field.Type)
fmt.Printf(" %s %s (size: %d bytes)\n",
name.Name, formatType(field.Type), size)
}
} else {
size := getTypeSize(field.Type)
fmt.Printf(" %s (embedded, size: %d bytes)\n",
formatType(field.Type), size)
}
}
fmt.Println("Suggested order:")
for _, fi := range reorderedFields {
if fi.Name != "" {
fmt.Printf(" %s %s (size: %d bytes)\n",
fi.Name, formatType(fi.Field.Type), fi.TypeSize)
} else {
fmt.Printf(" %s (embedded, size: %d bytes)\n",
formatType(fi.Field.Type), fi.TypeSize)
}
}
newStruct := generateReorderedStruct(structType, reorderedFields)
fmt.Println("\nReordered struct:")
var buf strings.Builder
format.Node(&buf, fset, &ast.GenDecl{
Tok: token.TYPE,
Specs: []ast.Spec{
&ast.TypeSpec{
Name: typeSpec.Name,
Type: newStruct,
},
},
})
fmt.Println(buf.String())
}
}
}
}
}
}
return true
})
if !hasIssues {
fmt.Printf("✔ %s\n", filename)
os.Exit(0)
}
return nil
}
func formatType(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + formatType(t.X)
case *ast.ArrayType:
if t.Len == nil {
return "[]" + formatType(t.Elt)
}
return "[...]" + formatType(t.Elt)
case *ast.MapType:
return "map[" + formatType(t.Key) + "]" + formatType(t.Value)
case *ast.ChanType:
return "chan " + formatType(t.Value)
case *ast.InterfaceType:
return "interface{}"
case *ast.FuncType:
return "func(...)"
}
return "unknown"
}
func collectGoFiles(root string) ([]string, error) {
var files []string
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && (d.Name() == "vendor" || d.Name() == "testdata" || strings.HasPrefix(d.Name(), ".")) {
return filepath.SkipDir
}
if !d.IsDir() && strings.HasSuffix(d.Name(), ".go") && !strings.HasSuffix(d.Name(), "_test.go") {
files = append(files, path)
}
return nil
})
return files, err
}
func pwd() (string, error) {
dir, err := os.Getwd()
if err != nil {
fmt.Println("error finding current working directory:", err)
return "", err
}
return dir, nil
}
func main() {
var fix bool
flag.BoolVar(&fix, "fix", false, "automatically fix struct field ordering issues")
flag.Parse()
targets := flag.Args()
var files []string
var err error
cwd, _ := pwd()
if len(targets) == 0 {
// Default: process all .go files in current directory
files, err = collectGoFiles(cwd)
if err != nil {
fmt.Printf("Failed to collect Go files: %v\n", err)
os.Exit(1)
}
} else {
info, err := os.Stat(targets[0])
if err != nil {
fmt.Printf("Invalid path: %v\n", err)
os.Exit(1)
}
if info.IsDir() {
files, err = collectGoFiles(targets[0])
if err != nil {
fmt.Printf("Failed to collect Go files from directory: %v\n", err)
os.Exit(1)
}
} else {
files = []string{targets[0]}
}
}
hadIssues := false
for _, file := range files {
if fix {
err = fixStructsInFile(file)
} else {
err = lintFile(file)
}
if err != nil {
hadIssues = true
fmt.Printf("Error in %s: %v\n", file, err)
}
}
if hadIssues {
os.Exit(1)
}
}