-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpack.go
499 lines (479 loc) · 12.8 KB
/
pack.go
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package stroo
import (
"errors"
"go/ast"
"go/types"
"log"
)
type Imports struct {
Name string
Path string
}
type PackageInfo struct {
Name string
Path string
Types TypesSlice
Interfaces TypesSlice
Functions Methods
Vars Vars
TypesInfo *types.Info
Imports []*Imports
PrintDebug bool
}
func (pkg *PackageInfo) LoadImports(fromImports []*types.Package) {
for _, imprt := range fromImports {
if pkg.PrintDebug {
log.Printf("Adding %q %q to imports", imprt.Path(), imprt.Name())
}
pkg.Imports = append(pkg.Imports, &Imports{Path: imprt.Path(), Name: imprt.Name()})
}
}
func fixFieldsInfo(defs *types.Info, forType TypeInfo) error {
for idx := range forType.Fields {
for key, value := range defs.Defs {
if key.Name == forType.Fields[idx].Kind {
if forType.Fields[idx].Package == "" {
log.Printf("%q DOESN'T have a package", forType.Fields[idx].Kind)
}
underlyingValue := value.Type().Underlying()
switch underType := underlyingValue.(type) {
case *types.Pointer:
underlyingPointer := underType.Elem().Underlying()
if !forType.Fields[idx].IsPointer {
forType.Fields[idx].IsPointer = true
}
switch underPtr := underlyingPointer.(type) {
case *types.Struct:
forType.Fields[idx].IsStruct = true // this is mandatory
case *types.Slice:
if !forType.Fields[idx].IsArray {
forType.Fields[idx].IsArray = true
}
case *types.Interface:
if !forType.Fields[idx].IsInterface {
forType.Fields[idx].IsInterface = true
}
default:
log.Printf("Fixer [pointer]: %#v", underPtr)
}
case *types.Struct:
forType.Fields[idx].IsStruct = true // this is mandatory
case *types.Slice:
if !forType.Fields[idx].IsArray {
forType.Fields[idx].IsArray = true
}
case *types.Interface:
if !forType.Fields[idx].IsInterface {
forType.Fields[idx].IsInterface = true
}
case *types.Basic:
log.Printf("Fixer [basic]: %#v", underType) // this is never? the case ?
case *types.Signature:
// ignore signatures
//log.Printf("Fixer [signature]: %#v", underType)
default:
log.Printf("Fixer [?]: %#v", underType)
}
break // found it, bail out
}
}
}
return nil
}
func readIdent(ident *ast.Ident, comment *ast.CommentGroup) (*TypeInfo, error) {
var (
result TypeInfo
err error
)
if comment != nil {
// direct call from Preorder
result.Comment = comment
}
result.Kind = ident.Name
return &result, err
}
func readPointer(ptr *ast.StarExpr, comment *ast.CommentGroup) (*TypeInfo, error) {
var (
result TypeInfo
err error
)
if comment != nil {
// direct call from Preorder
result.Comment = comment
}
result.IsPointer = true
switch typedSpec := ptr.X.(type) {
case *ast.Ident:
fieldInfo, err := readIdent(typedSpec, nil)
if err == nil {
result.Kind = fieldInfo.Kind
}
case *ast.SelectorExpr:
fieldInfo, err := readSelector(typedSpec, nil)
if err == nil {
result.Kind = fieldInfo.Kind
result.IsImported = fieldInfo.IsImported
if fieldInfo.Package != "" {
result.Package = fieldInfo.Package
}
result.IsImported = fieldInfo.IsImported
}
case *ast.ArrayType:
elInfo, err := readElemType(typedSpec)
if err == nil {
result.Kind = elInfo.Kind
result.IsPointer = elInfo.IsPointer
}
case *ast.MapType:
result.IsMap = true
result.Kind = "map (temporary)"
case *ast.ChanType:
result.IsChan = true
result.Kind = "chan (temporary)"
case *ast.StructType:
result.Kind = "struct (temporary)"
default:
//log.Printf("UNHANDLED PTR CASE : %T", typedSpec)
}
return &result, err
}
func readSelector(sel *ast.SelectorExpr, comment *ast.CommentGroup) (*TypeInfo, error) {
var (
result TypeInfo
err error
)
if comment != nil {
// direct call from Preorder
result.Comment = comment
}
result.IsImported = true
result.Kind = sel.Sel.Name
if sel.Sel != nil {
idt, ok := sel.X.(*ast.Ident)
if ok {
fieldInfo, err := readIdent(idt, nil)
if err == nil {
result.Kind = fieldInfo.Kind + "." + result.Kind
}
} else {
log.Printf("BAD SELECTOR [NOT IDENT] : %#v", sel)
}
} else {
log.Printf("BAD SELECTOR [NIL SEL]: %#v", sel)
}
return &result, err
}
func readField(pkg *types.Package, field *ast.Field, comment *ast.CommentGroup) (TypesSlice, error) {
var (
oneResult TypeInfo
err error
fieldNames []string
)
oneResult.Comment = comment
oneResult.Package = pkg.Name()
oneResult.PackagePath = pkg.Path()
switch len(field.Names) {
case 0:
// embedded field
oneResult.IsEmbedded = true
case 1:
oneResult.IsExported = field.Names[0].IsExported()
oneResult.Name = field.Names[0].Name
default:
// processedField declaration is like `Name, Phone string`
oneResult.IsExported = field.Names[0].IsExported()
oneResult.Name = field.Names[0].Name
for idx, fieldName := range field.Names {
if idx > 0 {
fieldNames = append(fieldNames, fieldName.Name)
}
}
}
switch typedSpec := field.Type.(type) {
case *ast.Ident:
fieldInfo, err := readIdent(typedSpec, nil)
if err == nil {
oneResult.Kind = fieldInfo.Kind
}
case *ast.StarExpr:
fieldInfo, err := readPointer(typedSpec, nil)
if err == nil {
oneResult.IsPointer = fieldInfo.IsPointer
oneResult.Kind = fieldInfo.Kind
oneResult.IsImported = fieldInfo.IsImported
}
case *ast.SelectorExpr:
fieldInfo, err := readSelector(typedSpec, nil)
if err == nil {
oneResult.IsImported = fieldInfo.IsImported
oneResult.Kind = fieldInfo.Kind
if fieldInfo.Package != "" {
oneResult.Package = fieldInfo.Package
}
oneResult.IsImported = fieldInfo.IsImported
}
case *ast.MapType:
oneResult.IsMap = true
oneResult.Kind = "map (temporary)"
//log.Printf("%q is map", oneResult.Name)
case *ast.ChanType:
oneResult.IsChan = true
oneResult.Kind = "chan (temporary)"
// TODO : read chan kind
//log.Printf("%q is chan", oneResult.Name)
case *ast.ArrayType:
oneResult.IsArray = true
elemTypeInfo, err := readElemType(typedSpec)
if err == nil {
oneResult.IsPointer = elemTypeInfo.IsPointer
oneResult.Kind = elemTypeInfo.Kind
}
//log.Printf("%q is array : %#v", oneResult.Name, typedSpec)
case *ast.FuncType:
oneResult.IsFunc = true
//log.Printf("%q %s is func", oneResult.Name, oneResult.Kind)
params, returns, err := readFunc(typedSpec)
if err == nil {
oneResult.MethodList = append(oneResult.MethodList, FunctionInfo{Params: params, Returns: returns})
}
case *ast.InterfaceType:
oneResult.IsInterface = true
oneResult.Kind = "interface"
default:
//log.Printf("UNHANDLED FIELD CASE : %T", typedSpec)
}
if field.Tag != nil {
oneResult.Tags, err = ParseTags(field.Tag.Value)
}
result := make(TypesSlice, 0)
result = append(result, oneResult)
if len(fieldNames) > 0 {
for _, name := range fieldNames {
result = append(result, oneResult.Clone(name))
}
}
return result, err
}
func readType(pkg *types.Package, astSpec *ast.TypeSpec, comment *ast.CommentGroup) (TypeInfo, error) {
var (
result TypeInfo
err error
)
result.Comment = comment
if astSpec.Name == nil {
log.Printf("possible error : astSpec.Name is nil on %#v", astSpec)
return TypeInfo{}, errors.New("type name is nil")
}
result.Package = pkg.Name()
result.PackagePath = pkg.Path()
result.Kind = astSpec.Name.Name
switch typedSpec := astSpec.Type.(type) {
case *ast.StructType:
result.Name = result.Kind
//log.Printf("Traversing struct kind : %q", result.Kind)
for _, field := range typedSpec.Fields.List {
fieldsInfo, err := readField(pkg, field, field.Comment)
if err == nil {
result.Fields = append(result.Fields, fieldsInfo...)
}
}
case *ast.ArrayType:
//log.Printf("Traversing array : %q", result.Kind)
result.Name = astSpec.Name.Name
result.IsArray = true
elInfo, err := readElemType(typedSpec)
if err == nil {
result.Kind = elInfo.Kind
result.IsPointer = elInfo.IsPointer
result.IsImported = elInfo.IsImported
}
case *ast.InterfaceType:
for _, method := range typedSpec.Methods.List {
fieldInfo, err := readField(pkg, method, nil)
if err == nil {
result.Fields = append(result.Fields, fieldInfo...)
}
}
case *ast.FuncType:
result.Name = result.Kind
paramsInfo, resultsInfo, err := readFunc(typedSpec)
if err == nil {
result.MethodList = append(result.MethodList, FunctionInfo{
Package: pkg.Name(),
PackagePath: pkg.Path(),
Name: "func",
Params: paramsInfo,
Returns: resultsInfo,
comment: comment,
//IsExported: ???,
})
}
default:
//log.Printf("UNHANDLED TYPE CASE : %T", typedSpec)
}
return result, err
}
func readElemType(arr *ast.ArrayType) (*TypeInfo, error) {
var (
result TypeInfo
err error
)
switch elType := arr.Elt.(type) {
case *ast.Ident:
fieldInfo, err := readIdent(elType, nil)
if err == nil {
result.Kind = fieldInfo.Kind
}
case *ast.StarExpr:
fieldInfo, err := readPointer(elType, nil)
if err == nil {
result.Kind = fieldInfo.Kind
result.IsPointer = fieldInfo.IsPointer
result.IsImported = fieldInfo.IsImported
}
case *ast.SelectorExpr:
fieldInfo, err := readSelector(elType, nil)
if err == nil {
result.Kind = fieldInfo.Kind
result.IsPointer = fieldInfo.IsPointer // TODO : check
result.IsImported = fieldInfo.IsImported // TODO : check
if fieldInfo.Package != "" {
result.Package = fieldInfo.Package // TODO : check
}
if result.PackagePath != "" {
result.PackagePath = fieldInfo.PackagePath // TODO : check
}
}
case *ast.ArrayType:
elInfo, err := readElemType(elType)
if err == nil {
result.Kind = elInfo.Kind
result.IsPointer = elInfo.IsPointer
}
case *ast.MapType:
result.Kind = "map (temporary)"
case *ast.StructType:
result.Kind = "struct (temporary)"
case *ast.ChanType:
result.Kind = "chan (temporary)"
default:
//log.Printf("UNHANDLED ELEM CASE : %T", elType)
}
return &result, err
}
func readFunc(spec *ast.FuncType) ([]VarInfo, []VarInfo, error) {
var (
err error
)
var params []VarInfo
if spec.Params != nil {
for _, p := range spec.Params.List {
param := buildVarFromExpr(p)
if param.Kind == "" && param.Name == "" {
log.Printf("noname/notype found while inspecting params of %#v", spec.Params.List)
continue
}
params = append(params, param)
}
}
var returns []VarInfo
if spec.Results != nil {
for _, r := range spec.Results.List {
param := buildVarFromExpr(r)
if param.Kind == "" && param.Name == "" {
log.Printf("noname/notype found while inspecting returns of %#v", spec.Results.List)
continue
}
returns = append(returns, param)
}
}
return params, returns, err
}
// get function information from the function object
func readFuncDecl(spec *ast.FuncDecl) (*FunctionInfo, error) {
if spec.Name == nil {
return nil, errors.New("spec name is nil while reading function")
}
info := FunctionInfo{
Name: spec.Name.Name,
IsExported: spec.Name.IsExported(),
comment: spec.Doc,
}
if spec.Recv != nil {
if len(spec.Recv.List) > 0 {
stList := spec.Recv.List[0]
if len(stList.Names) > 0 {
info.ReceiverName = stList.Names[0].Name
}
switch recType := stList.Type.(type) {
case *ast.Ident:
info.ReceiverType = recType.Name
case *ast.StarExpr:
info.IsMethodReceiver = true
info.ReceiverType = recType.X.(*ast.Ident).Name
default:
//log.Printf("don't know what to do with receiver : %T %#v", recType, recType)
}
}
}
params, returns, err := readFunc(spec.Type)
if err == nil {
info.Params = params
info.Returns = returns
}
return &info, nil
}
func buildVarFromExpr(field *ast.Field) VarInfo {
var param VarInfo
if len(field.Names) == 1 {
param.Name = field.Names[0].Name // it has a name
}
switch paramType := field.Type.(type) {
case *ast.Ident:
fieldInfo, err := readIdent(paramType, nil)
if err == nil {
param.Kind = fieldInfo.Kind
}
case *ast.StarExpr:
fieldInfo, err := readPointer(paramType, nil)
if err == nil {
param.Kind = fieldInfo.Kind
}
case *ast.SelectorExpr:
fieldInfo, err := readSelector(paramType, nil)
if err == nil {
param.Kind = fieldInfo.Kind // TODO : check
}
case *ast.ArrayType:
typeInfo, err := readElemType(paramType)
if err == nil {
param.Kind = "[]" + typeInfo.Kind
}
case *ast.InterfaceType:
param.Kind = "interface"
case *ast.FuncType:
param.Kind = "function"
case *ast.MapType:
param.Kind = "map"
default:
log.Printf("UNHANDLED PARAM/RETURN CASE : %T", paramType)
}
return param
}
func readValue(defObj types.Object, valueSpec *ast.ValueSpec) ([]VarInfo, error) {
var result []VarInfo
for _, varName := range valueSpec.Names {
var name string
switch defObj.(type) {
case *types.Var:
name = defObj.(*types.Var).Type().String()
case *types.Const:
name = defObj.(*types.Const).Type().String()
}
result = append(result, VarInfo{
Name: varName.Name,
Kind: name,
})
}
return result, nil
}