-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleast.go
282 lines (267 loc) · 7.3 KB
/
simpleast.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
// Package simpleast provides a simple way to inspect Go files and extract
// structs and methods from them.
package simpleast
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"slices"
"strconv"
"strings"
)
// Struct represents a Go struct. Fields and Methods are not ordered.
type Struct struct {
Name string `json:"name,omitempty"`
DocComment string `json:"doc_comment,omitempty"`
TypeParams []Field `json:"type_params,omitempty"`
Fields []Field `json:"fields,omitempty"`
Methods []Method `json:"methods,omitempty"`
}
// Field represents a Go field of Struct.
type Field struct {
Name string `json:"name,omitempty"`
DocComment string `json:"doc_comment,omitempty"`
Type string `json:"type,omitempty"`
Tags StructTags `json:"tags,omitempty"`
}
type StructTags []StructTag
func (st StructTags) Get(name string) string {
for _, tag := range st {
if tag.Name == name {
return tag.Value
}
}
return ""
}
// StructTag represents a Go struct field tag.
type StructTag struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}
// Method represents a Go method of Struct.
type Method struct {
Name string `json:"name,omitempty"`
DocComment string `json:"doc_comment,omitempty"`
TypeParams []string `json:"type_params,omitempty"`
Parameters []Field `json:"parameters,omitempty"`
Results []Field `json:"results,omitempty"`
structName string
}
// ParseStructs extracts structs and methods from a Go file.
func ParseStructs(r io.Reader) ([]*Struct, error) {
src, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
f, err := parser.ParseFile(token.NewFileSet(), "", src, parser.AllErrors|parser.ParseComments)
if err != nil {
return nil, fmt.Errorf("parse file: %w", err)
}
var structs []*Struct
ast.Inspect(f, func(n ast.Node) bool {
switch decl := n.(type) {
case *ast.GenDecl:
isSingleTypeGroup := len(decl.Specs) == 1
hasOneDocGroup := decl.Doc != nil && len(decl.Doc.List) != 0
if isSingleTypeGroup && hasOneDocGroup {
typeSpec, isTypeSpec := decl.Specs[0].(*ast.TypeSpec)
if isTypeSpec && typeSpec.Doc == nil {
typeSpec.Doc = decl.Doc
}
}
structs = append(structs, parseASTSpecs(decl.Specs)...)
case *ast.FuncDecl:
receivers := parseASTFuncDecl(decl)
for _, receiver := range receivers {
idx := slices.IndexFunc(structs, func(s *Struct) bool { return s.Name == receiver.structName })
if idx == -1 {
structs = append(structs, &Struct{
Name: receiver.structName,
Methods: []Method{receiver},
})
continue
}
structs[idx].Methods = append(structs[idx].Methods, receiver)
}
}
return true
})
return structs, nil
}
func expressionString(expr ast.Expr) string {
switch e := expr.(type) {
case *ast.Ident:
return e.Name
case *ast.StarExpr:
return "*" + expressionString(e.X)
case *ast.SelectorExpr:
return expressionString(e.X) + "." + e.Sel.Name
case *ast.ArrayType:
return "[]" + expressionString(e.Elt)
case *ast.MapType:
return "map[" + expressionString(e.Key) + "]" + expressionString(e.Value)
default:
return ""
}
}
func parseASTSpecs(specs []ast.Spec) []*Struct {
structs := []*Struct{}
for _, spec := range specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
fields := []Field{}
for _, field := range structType.Fields.List {
fieldTags := ""
if field.Tag != nil {
fieldTags = field.Tag.Value
}
fieldComment := field.Doc.Text()
for _, fieldName := range field.Names {
fields = append(fields, Field{
Name: fieldName.Name,
DocComment: fieldComment,
Type: expressionString(field.Type),
Tags: parseFieldTags(fieldTags),
})
}
}
typeParams := []Field{}
if typeSpec.TypeParams != nil {
for _, field := range typeSpec.TypeParams.List {
for _, name := range field.Names {
typeParams = append(typeParams, Field{
Name: name.Name,
Type: expressionString(field.Type),
})
}
}
}
structs = append(structs, &Struct{
Name: typeSpec.Name.Name,
DocComment: typeSpec.Doc.Text(),
TypeParams: typeParams,
Fields: fields,
Methods: []Method{},
})
}
return structs
}
func parseASTFuncDecl(decl *ast.FuncDecl) []Method {
methods := []Method{}
if decl.Recv == nil {
return methods
}
for _, recv := range decl.Recv.List {
structName := ""
typeParamsExpr := []ast.Expr{}
switch t := recv.Type.(type) {
case *ast.StarExpr:
if ident, ok := t.X.(*ast.Ident); ok {
structName = ident.Name
} else if indexList, ok := t.X.(*ast.IndexListExpr); ok {
if ident, ok := indexList.X.(*ast.Ident); ok {
structName = ident.Name
}
typeParamsExpr = indexList.Indices
} else if index, ok := t.X.(*ast.IndexExpr); ok {
if ident, ok := index.X.(*ast.Ident); ok {
structName = ident.Name
}
typeParamsExpr = []ast.Expr{index.Index}
}
case *ast.Ident:
structName = t.Name
case *ast.IndexListExpr:
if ident, ok := t.X.(*ast.Ident); ok {
structName = ident.Name
}
typeParamsExpr = t.Indices
case *ast.IndexExpr:
if ident, ok := t.X.(*ast.Ident); ok {
structName = ident.Name
}
typeParamsExpr = []ast.Expr{t.Index}
}
typeParams := []string{}
for _, index := range typeParamsExpr {
if ident, ok := index.(*ast.Ident); ok {
typeParams = append(typeParams, ident.Name)
}
}
params := []Field{}
for _, param := range decl.Type.Params.List {
for _, paramName := range param.Names {
params = append(params, Field{
Name: paramName.Name,
Type: expressionString(param.Type),
})
}
}
returns := []Field{}
if decl.Type.Results != nil {
for _, result := range decl.Type.Results.List {
returns = append(returns, Field{
Type: expressionString(result.Type),
})
}
}
methods = append(methods, Method{
structName: structName,
Name: decl.Name.Name,
DocComment: decl.Doc.Text(),
TypeParams: typeParams,
Parameters: params,
Results: returns,
})
}
return methods
}
func parseFieldTags(tags string) StructTags {
// A StructTag is the tag string in a struct field.
//
// By convention, tag strings are a concatenation of
// optionally space-separated key:"value" pairs.
// Each key is a non-empty string consisting of non-control
// characters other than space (U+0020 ' '), quote (U+0022 '"'),
// and colon (U+003A ':'). Each value is quoted using U+0022 '"'
// characters and Go string literal syntax.
tags = strings.Trim(tags, "`")
tags = strings.TrimLeft(tags, " ")
var structTags StructTags
currentTag := StructTag{}
for tags != "" {
colonDivider := strings.Index(tags, ":")
if colonDivider == -1 {
break
}
name := tags[:colonDivider]
currentTag.Name = name
tags = tags[colonDivider+1:]
insideQuotes := false
for i := 0; i < len(tags); i++ {
if !insideQuotes && tags[i] == '"' {
insideQuotes = true
} else if insideQuotes && tags[i] == '\\' {
i++
} else if insideQuotes && tags[i] == '"' {
v, err := strconv.Unquote(tags[:i+1])
if err != nil {
break
}
currentTag.Value = v
tags = strings.TrimLeft(tags[i+1:], " ")
structTags = append(structTags, currentTag)
currentTag = StructTag{}
}
}
}
return structTags
}