-
Notifications
You must be signed in to change notification settings - Fork 249
/
interfacecollector.go
226 lines (214 loc) · 5.69 KB
/
interfacecollector.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
package astutils
import (
"fmt"
"github.com/iancoleman/strcase"
"github.com/sirupsen/logrus"
"github.com/unionj-cloud/go-doudou/toolkit/stringutils"
"go/ast"
"go/parser"
"go/token"
"strings"
)
// InterfaceCollector collect interfaces by parsing source code
type InterfaceCollector struct {
Interfaces []InterfaceMeta
Package PackageMeta
exprString func(ast.Expr) string
cmap ast.CommentMap
}
// Visit traverse each node from source code
func (ic *InterfaceCollector) Visit(n ast.Node) ast.Visitor {
return ic.Collect(n)
}
// Collect collects all interfaces from source code
func (ic *InterfaceCollector) Collect(n ast.Node) ast.Visitor {
switch spec := n.(type) {
case *ast.Package:
return ic
case *ast.File: // actually it is package name
ic.Package = PackageMeta{
Name: spec.Name.Name,
}
return ic
case *ast.GenDecl:
if spec.Tok == token.TYPE {
comments := doc2Comments(spec.Doc)
for _, item := range spec.Specs {
typeSpec := item.(*ast.TypeSpec)
typeName := typeSpec.Name.Name
switch specType := typeSpec.Type.(type) {
case *ast.InterfaceType:
ic.Interfaces = append(ic.Interfaces, InterfaceMeta{
Name: typeName,
Methods: ic.field2Methods(specType.Methods.List),
Comments: comments,
})
}
}
}
}
return nil
}
func (ic *InterfaceCollector) field2Methods(list []*ast.Field) []MethodMeta {
var methods []MethodMeta
for _, method := range list {
if len(method.Names) == 0 {
panic("no method name")
}
mn := method.Names[0].Name
var mComments []string
if method.Doc != nil {
for _, comment := range method.Doc.List {
mComments = append(mComments, strings.TrimSpace(strings.TrimPrefix(comment.Text, "//")))
}
}
ft, _ := method.Type.(*ast.FuncType)
var params []FieldMeta
if ft.Params != nil {
params = ic.field2Params(ft.Params.List)
}
var results []FieldMeta
if ft.Results != nil {
results = ic.field2Results(ft.Results.List)
}
methods = append(methods, MethodMeta{
Name: mn,
Params: params,
Results: results,
Comments: mComments,
})
}
return methods
}
func (ic *InterfaceCollector) field2Params(list []*ast.Field) []FieldMeta {
var params []FieldMeta
pkeymap := make(map[string]int)
for _, param := range list {
pt := ic.exprString(param.Type)
if len(param.Names) > 0 {
for i, name := range param.Names {
field := FieldMeta{
Name: name.Name,
Type: pt,
}
var cnode ast.Node
if i == 0 {
cnode = param
} else {
cnode = name
}
if cmts, exists := ic.cmap[cnode]; exists {
for _, comment := range cmts {
field.Comments = append(field.Comments, strings.TrimSpace(strings.TrimPrefix(comment.Text(), "//")))
}
}
params = append(params, field)
}
continue
}
var pComments []string
if cmts, exists := ic.cmap[param]; exists {
for _, comment := range cmts {
pComments = append(pComments, strings.TrimSpace(strings.TrimPrefix(comment.Text(), "//")))
}
}
var pn string
elemt := strings.TrimPrefix(pt, "*")
if stringutils.IsNotEmpty(elemt) {
if strings.Contains(elemt, "[") {
elemt = elemt[strings.Index(elemt, "]")+1:]
elemt = strings.TrimPrefix(elemt, "*")
}
splits := strings.Split(elemt, ".")
_key := "p" + strcase.ToLowerCamel(splits[len(splits)-1][0:1])
if _, exists := pkeymap[_key]; exists {
pkeymap[_key]++
pn = _key + fmt.Sprintf("%d", pkeymap[_key])
} else {
pkeymap[_key]++
pn = _key
}
}
params = append(params, FieldMeta{
Name: pn,
Type: pt,
Comments: pComments,
})
}
return params
}
func (ic *InterfaceCollector) field2Results(list []*ast.Field) []FieldMeta {
var results []FieldMeta
rkeymap := make(map[string]int)
for _, result := range list {
var rComments []string
if cmts, exists := ic.cmap[result]; exists {
for _, comment := range cmts {
rComments = append(rComments, strings.TrimSpace(strings.TrimPrefix(comment.Text(), "//")))
}
}
rt := ic.exprString(result.Type)
if len(result.Names) > 0 {
for _, name := range result.Names {
results = append(results, FieldMeta{
Name: name.Name,
Type: rt,
Tag: "",
Comments: rComments,
})
}
continue
}
var rn string
elemt := strings.TrimPrefix(rt, "*")
if stringutils.IsNotEmpty(elemt) {
if strings.Contains(elemt, "[") {
elemt = elemt[strings.Index(elemt, "]")+1:]
elemt = strings.TrimPrefix(elemt, "*")
}
splits := strings.Split(elemt, ".")
_key := "r" + strcase.ToLowerCamel(splits[len(splits)-1][0:1])
if _, exists := rkeymap[_key]; exists {
rkeymap[_key]++
rn = _key + fmt.Sprintf("%d", rkeymap[_key])
} else {
rkeymap[_key]++
rn = _key
}
}
results = append(results, FieldMeta{
Name: rn,
Type: rt,
Tag: "",
Comments: rComments,
})
}
return results
}
func doc2Comments(doc *ast.CommentGroup) []string {
var comments []string
if doc != nil {
for _, comment := range doc.List {
comments = append(comments, strings.TrimSpace(strings.TrimPrefix(comment.Text, "//")))
}
}
return comments
}
// NewInterfaceCollector initializes an InterfaceCollector
func NewInterfaceCollector(exprString func(ast.Expr) string) *InterfaceCollector {
return &InterfaceCollector{
exprString: exprString,
}
}
// BuildInterfaceCollector initializes an InterfaceCollector and collects interfaces
func BuildInterfaceCollector(file string, exprString func(ast.Expr) string) InterfaceCollector {
ic := NewInterfaceCollector(exprString)
fset := token.NewFileSet()
root, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
if err != nil {
logrus.Panicln(err)
}
ic.cmap = ast.NewCommentMap(fset, root, root.Comments)
ast.Walk(ic, root)
return *ic
}