/
main.go
253 lines (227 loc) · 5.83 KB
/
main.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
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"path"
"reflect"
"strings"
)
var (
// go mod name
modName = "github.com/jdxj/study-ast"
pkgPath string
name string
// 解析缓存
pkgMap = make(map[string]*ast.Package)
)
func main() {
flag.StringVar(&modName, "mod-name", "", "go mod name")
flag.StringVar(&pkgPath, "pkg-path", "", "target struct pkg path without mod-name")
flag.StringVar(&name, "name", "", "struct name")
flag.Parse()
if modName == "" || pkgPath == "" || name == "" {
return
}
ss := findStruct(pkgPath, name)
for _, s := range ss {
fmt.Printf("%s:\n", s.Name)
for _, v := range s.Fields {
fmt.Printf(" |%s|%s|%s|\n", v.Name, v.Type, v.Description)
}
}
}
// Struct 描述了其所含字段的描述
type Struct struct {
Name string
Fields []Field
}
type Field struct {
Name string
Type string
Description string
}
// findStruct 在指定的 pkgPath 中寻找 structName,
// 返回值中第0个为该 structName, 剩余的是其所依赖的 struct.
func findStruct(pkgPath, structName string) []Struct {
_, ok := pkgMap[pkgPath]
if !ok {
tfs := token.NewFileSet()
pkgs, err := parser.ParseDir(tfs, pkgPath, nil, parser.ParseComments)
if err != nil {
panic(err)
}
// pkgPath 为某个包的路径, 所以 pkgs 中只有一个值
for _, astPkg := range pkgs {
pkgMap[pkgPath] = astPkg
}
}
return findStructInPkg(pkgPath, pkgMap[pkgPath], structName)
}
func findStructInPkg(curPkgPath string, astPkg *ast.Package, structName string) []Struct {
var ss []Struct
for _, file := range astPkg.Files {
ss = append(ss, findStructInFile(file, curPkgPath, structName)...)
}
return ss
}
func findStructInFile(file *ast.File, curPkgPath, structName string) []Struct {
var (
ss []Struct
importMap = make(map[string]string)
)
for _, decl := range file.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
switch genDecl.Tok {
case token.IMPORT:
// 记录当前文件的 import
for _, spec := range genDecl.Specs {
importSpec := spec.(*ast.ImportSpec)
pkgPath := strings.Trim(importSpec.Path.Value, `"`)
pkgPath = strings.TrimPrefix(pkgPath, modName+"/")
pkg := path.Base(pkgPath)
if importSpec.Name != nil {
pkg = importSpec.Name.Name
}
importMap[pkg] = pkgPath
}
case token.TYPE:
// 寻找 struct 定义
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
if typeSpec.Name.Name != structName {
continue
}
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
// 创建 Struct
fields, ssIn := getFields(importMap, structType, structName, curPkgPath)
s := Struct{
Name: structName,
Fields: fields,
}
ss = append(ss, s)
ss = append(ss, ssIn...)
// 递归查找
ss = append(ss, getDeepStruct(importMap, structType, curPkgPath)...)
}
}
}
return ss
}
// getFields 获取当前 struct 的 field
func getFields(importMap map[string]string, parentStructType *ast.StructType, parentStructName, curPkgPath string) ([]Field, []Struct) {
var (
fields = make([]Field, 0, len(parentStructType.Fields.List))
ssOut []Struct
)
for _, field := range parentStructType.Fields.List {
pkgPath, structName, typ, structType := unwrapFieldType(importMap, curPkgPath, field)
// 匿名字段
if field.Names == nil {
ssIn := findStruct(pkgPath, structName)
// 第一个是 structName 本身, 将其 fields 赋给当前 struct
// len(ssIn) != 0, 否则是不能编译的 go 代码
fields = append(fields, ssIn[0].Fields...)
// 剩下的是 structName 所依赖的
ssOut = append(ssOut, ssIn[1:]...)
continue
}
// 匿名 struct
if structType != nil {
structName = fmt.Sprintf(".%s.%s", parentStructName, structName)
fields, ssIn := getFields(importMap, structType, structName, curPkgPath)
s := Struct{
Name: structName,
Fields: fields,
}
ssOut = append(ssOut, s)
ssOut = append(ssOut, ssIn...)
}
// 普通字段
fields = append(fields, Field{
Name: getTag(field),
//Type: getType(field),
Type: typ,
Description: getDescription(field),
})
}
return fields, ssOut
}
func getDeepStruct(importMap map[string]string, structType *ast.StructType, curPkgPath string) []Struct {
var ss []Struct
for _, field := range structType.Fields.List {
// 跳过匿名
if field.Names == nil {
continue
}
pkgPath, structName, _, st := unwrapFieldType(importMap, curPkgPath, field)
if st != nil {
continue
}
ss = append(ss, findStruct(pkgPath, structName)...)
}
return ss
}
func getTag(field *ast.Field) string {
if field.Tag == nil {
return ""
}
tag := strings.Trim(field.Tag.Value, "`")
return reflect.StructTag(tag).Get("json")
}
func unwrapFieldType(importMap map[string]string, curPkgPath string, field *ast.Field) (
pkgPath, structName, literal string, structType *ast.StructType) {
pkgPath = curPkgPath
if len(field.Names) != 0 {
// 匿名 struct 使用字段名
structName = field.Names[0].Name
}
var (
expr = field.Type
prefix string
)
for {
switch exprType := expr.(type) {
case *ast.ArrayType:
expr = exprType.Elt
prefix += "[]"
case *ast.StarExpr:
expr = exprType.X
case *ast.SelectorExpr:
pkgPath = importMap[exprType.X.(*ast.Ident).Name]
expr = exprType.Sel
case *ast.StructType:
literal = prefix + "struct"
structType = exprType
return
case *ast.InterfaceType:
literal = prefix + "Object"
structName = "interface"
return
case *ast.Ident:
structName = exprType.Name
literal = prefix + exprType.Name
return
}
}
}
func getDescription(field *ast.Field) string {
if field.Comment == nil {
return ""
}
desc := field.Comment.List[0].Text
desc = strings.Trim(desc, "//")
desc = strings.Trim(desc, " ")
return desc
}