-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstruct_parser.go
429 lines (394 loc) · 10.1 KB
/
struct_parser.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
package main
import (
"fmt"
"go/ast"
goparser "go/parser"
"go/token"
"go/types"
"strings"
"golang.org/x/tools/go/loader"
)
type Parser struct {
Tag string
program *loader.Program
loaderCfg *loader.Config
fileImportNamed map[*ast.File]map[string]string
fileTypeDoc map[*ast.File]map[string]string
parsedType map[string]struct{}
}
func NewParser(tagName string, modPath string) (*Parser, error) {
cfg := &loader.Config{
ParserMode: goparser.ParseComments,
}
cfg.Import(modPath)
p, err := cfg.Load()
if err != nil {
return nil, err
}
res := &Parser{
Tag: tagName,
program: p,
loaderCfg: cfg,
fileImportNamed: map[*ast.File]map[string]string{},
fileTypeDoc: map[*ast.File]map[string]string{},
parsedType: map[string]struct{}{},
}
return res, nil
}
func (p *Parser) getFileImportNamedMap(file *ast.File) map[string]string {
namedImportMap := map[string]string{}
for _, imp := range file.Imports {
modPath := strings.Trim(imp.Path.Value, "\"")
if imp.Name == nil {
namedImportMap[p.program.Package(modPath).Pkg.Name()] = modPath
} else {
namedImportMap[imp.Name.Name] = modPath
}
}
return namedImportMap
}
func (p *Parser) getFileTypeDocMap(file *ast.File) map[string]string {
typeDocMap := map[string]string{}
for _, imp := range file.Decls {
genDecl, ok := imp.(*ast.GenDecl)
if !ok {
continue
}
if genDecl.Tok != token.TYPE {
continue
}
var comment = ""
if genDecl.Doc != nil {
comment = TrimComment(genDecl.Doc.Text())
if comment != "" {
comment += "\n"
}
}
for _, spec := range genDecl.Specs {
if specType, ok := spec.(*ast.TypeSpec); ok {
var subComment = GetDescribeFromComment(specType.Doc, specType.Comment)
typeDocMap[specType.Name.Name] = comment + subComment
}
}
}
return typeDocMap
}
func (p *Parser) parseFileStruct(
modPath string,
file *ast.File,
exParseMap map[string]struct{},
objName string,
objStructType *ast.StructType) map[string]StructInfo {
res := make(map[string]StructInfo, 0)
typeKey := TypeKey(modPath, objName)
if _, ok := p.parsedType[typeKey]; ok {
return res
}
p.parsedType[typeKey] = struct{}{}
namedImportMap := p.fileImportNamed[file]
typeDocMap := p.fileTypeDoc[file]
structInfo := p.parseStruct(objStructType, typeDocMap[objName])
res[typeKey] = structInfo
for idx, field := range structInfo.Fields {
if field.Reference == "" {
continue
}
subModPath := ""
if field.Reference == "." {
subModPath = modPath
} else {
subModPath = namedImportMap[field.Reference]
}
structInfo.Fields[idx].Reference = subModPath
key := TypeKey(subModPath, field.Type)
if _, ok := exParseMap[key]; !ok {
exParseMap[key] = struct{}{}
if _, ok := p.parsedType[key]; ok {
continue
}
for typeName, fields := range p.Parse(subModPath, field.Type) {
res[typeName] = fields
}
}
}
return res
}
func (p *Parser) Parse(modPath string, typeName string) map[string]StructInfo {
pktInfo := p.program.Package(modPath)
if pktInfo == nil {
return map[string]StructInfo{}
}
res := make(map[string]StructInfo, 0)
exParseMap := make(map[string]struct{})
for _, file := range pktInfo.Files {
if _, ok := p.fileImportNamed[file]; !ok {
p.fileImportNamed[file] = p.getFileImportNamedMap(file)
}
if _, ok := p.fileTypeDoc[file]; !ok {
p.fileTypeDoc[file] = p.getFileTypeDocMap(file)
}
obj := file.Scope.Lookup(typeName)
if obj == nil {
continue
}
objTypeSpec := obj.Decl.(*ast.TypeSpec)
var objType = objTypeSpec.Type
if v, ok := objType.(*ast.StarExpr); ok {
objType = v.X
}
switch ost := objType.(type) {
case *ast.StructType:
structMaps := p.parseFileStruct(modPath, file, exParseMap, obj.Name, ost)
for name, info := range structMaps {
res[name] = info
}
case *ast.Ident:
if ost.Obj == nil { // 基本数据类型, 枚举值
enums := p.getEnumTypeValues(&pktInfo.Info, typeName, file.Decls)
res[TypeKey(modPath, typeName)] = StructInfo{
Describe: p.fileTypeDoc[file][typeName],
Enums: &EnumInfo{
Type: ost.Name,
Names: enums,
}}
}
case *ast.SelectorExpr:
subModPath := p.fileImportNamed[file][ost.X.(*ast.Ident).Name]
subTypeName := ost.Sel.Name
structMaps := p.Parse(subModPath, subTypeName)
oldKey := TypeKey(subModPath, subTypeName)
info := structMaps[oldKey]
info.Describe += fmt.Sprintf("alias `%s`", oldKey)
structMaps[TypeKey(modPath, typeName)] = info
delete(structMaps, oldKey)
return structMaps
}
}
return res
}
func (p *Parser) getEnumTypeValues(pktInfo *types.Info, name string, decls []ast.Decl) [][2]string {
res := make([][2]string, 0)
EXIT:
for _, obj := range decls {
if genDecl, ok := obj.(*ast.GenDecl); ok {
if genDecl.Tok != token.CONST || len(genDecl.Specs) == 0 {
continue
}
firstSpec := genDecl.Specs[0]
if valueSpec, ok := firstSpec.(*ast.ValueSpec); ok {
t, ok := valueSpec.Type.(*ast.Ident)
if !ok || t.Name != name {
continue
}
} else {
continue
}
for _, s := range genDecl.Specs {
v := s.(*ast.ValueSpec) // safe because decl.Tok == token.CONST
for _, name := range v.Names {
c := pktInfo.ObjectOf(name).(*types.Const)
res = append(res, [2]string{
c.Val().ExactString(),
GetDescribeFromComment(v.Doc, v.Comment),
})
}
}
break EXIT
}
}
return res
}
func (p *Parser) parseTypeExpr(obj ast.Expr) []FieldInfo {
if v, ok := obj.(*ast.StarExpr); ok {
obj = v.X
}
var res []FieldInfo
switch ot := obj.(type) {
case *ast.InterfaceType:
if ot.Incomplete || (ot.Methods != nil && len(ot.Methods.List) == 0) {
field := FieldInfo{
Type: "any",
skipNum: 1,
}
res = append(res, field)
}
case *ast.SelectorExpr:
res = []FieldInfo{{Type: ot.Sel.Name, Reference: ot.X.(*ast.Ident).Name, skipNum: 1}}
case *ast.Ident:
field := FieldInfo{
Type: ot.Name,
skipNum: 1,
}
if ot.Obj != nil {
field.Reference = "."
}
res = append(res, field)
case *ast.StructType:
res = p.parseStruct(ot, "").Fields
case *ast.MapType:
prefix := fmt.Sprintf("{%s}.", ot.Key)
res = p.parseTypeExpr(ot.Value)
for idx := range res {
res[idx].Name = prefix + res[idx].Name
}
case *ast.SliceExpr:
prefix := "[]."
res = p.parseTypeExpr(ot.X)
for idx := range res {
res[idx].Name = prefix + res[idx].Name
}
case *ast.ArrayType:
prefix := "[]"
t, ok := ot.Len.(*ast.BasicLit)
if ok {
prefix = fmt.Sprintf("[%s]", t.Value)
}
res = p.parseTypeExpr(ot.Elt)
for idx := range res {
res[idx].Name = prefix + res[idx].Name
}
}
return res
}
func (p *Parser) parseStruct(objStructType *ast.StructType, desc string) StructInfo {
res := StructInfo{
Describe: desc,
Fields: make([]FieldInfo, 0, len(objStructType.Fields.List)),
}
for _, f := range objStructType.Fields.List {
if len(f.Names) == 0 {
f.Names = []*ast.Ident{
{Name: f.Type.(fmt.Stringer).String()},
}
}
if f.Names[0].Name[0] <= 'Z' && f.Names[0].Name[0] >= 'A' {
res.Fields = append(res.Fields, p.parseStructField(f)...)
}
}
return res
}
func (p *Parser) parseStructField(f *ast.Field) []FieldInfo {
res := make([]FieldInfo, 0)
tagStr := ""
if f.Tag != nil {
tagStr = strings.Trim(f.Tag.Value, "`")
}
tagInfo := ParseStructTag(p.Tag, tagStr)
if tagInfo.Name == "-" {
return res
}
baseField := FieldInfo{
Name: tagInfo.Name,
Default: tagInfo.Default,
Require: tagInfo.Require,
Enums: EnumInfo{
Names: tagInfo.Enums,
},
}
if baseField.Name == "" {
baseField.Name = f.Names[0].Name
}
baseField.Describe += GetDescribeFromComment(f.Doc, f.Comment)
if s, ok := f.Type.(*ast.StarExpr); ok {
f.Type = s.X
}
switch tt := f.Type.(type) {
case *ast.Ident:
baseField.Type = tt.Name
if tt.Obj != nil {
baseField.Reference = "."
}
if tagInfo.Inline && baseField.Reference == "." {
for _, field := range p.parseTypeExpr(tt.Obj.Decl.(*ast.TypeSpec).Type) {
res = append(res, field)
}
} else {
res = append(res, baseField)
}
case *ast.StructType:
for _, f := range p.parseStruct(tt, baseField.Describe).Fields {
if tagInfo.Inline {
res = append(res, f)
} else {
if f.skipNum != 0 {
field := baseField.Copy()
field.Type = f.Type
if f.Name != "" {
field.Name += "." + f.Name
}
res = append(res, field)
} else {
f.Name = baseField.Name + "." + f.Name
res = append(res, f)
}
}
}
case *ast.MapType:
baseField.Name += fmt.Sprintf(".{%s}", tt.Key)
var subFields = p.parseTypeExpr(tt.Value)
for _, f := range subFields {
if f.skipNum != 0 {
field := baseField.Copy()
field.Type = f.Type
field.Reference = f.Reference
if f.Name != "" {
field.Name += "." + f.Name
}
res = append(res, field)
} else {
f.Name = baseField.Name + "." + f.Name
res = append(res, f)
}
}
case *ast.SliceExpr:
baseField.Name += ".[]"
for _, f := range p.parseTypeExpr(tt.X) {
if f.skipNum != 0 {
field := baseField.Copy()
field.Type = f.Type
field.Reference = f.Reference
if f.Name != "" {
field.Name += "." + f.Name
}
res = append(res, field)
} else {
f.Name = baseField.Name + "." + f.Name
res = append(res, f)
}
}
case *ast.ArrayType:
t, ok := tt.Len.(*ast.BasicLit)
if ok {
baseField.Name += fmt.Sprintf(".[%s]", t.Value)
} else {
baseField.Name += ".[]"
}
for _, f := range p.parseTypeExpr(tt.Elt) {
if f.skipNum != 0 {
field := baseField.Copy()
field.Type = f.Type
field.Reference = f.Reference
if f.Name != "" {
field.Name += "." + f.Name
}
res = append(res, field)
} else {
f.Name = baseField.Name + "." + f.Name
res = append(res, f)
}
}
case *ast.SelectorExpr:
baseField.Type = tt.Sel.Name
baseField.Reference = tt.X.(*ast.Ident).Name
res = append(res, baseField)
case *ast.InterfaceType:
if tt.Incomplete || (tt.Methods != nil && len(tt.Methods.List) == 0) {
baseField.Type = "any"
res = append(res, baseField)
}
}
return res
}
func TypeKey(modPath string, typeName string) string {
return modPath + "." + typeName
}