-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
325 lines (278 loc) · 7.29 KB
/
context.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
package eapi
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"strconv"
"github.com/gotomicro/eapi/spec"
"github.com/gotomicro/eapi/utils"
"golang.org/x/tools/go/packages"
)
type RouteAnalyzer func(ctx *Context, node ast.Node) (routes []*API)
type Context struct {
Env *Environment
pkg *packages.Package
file *ast.File
analyzer *Analyzer
commentStack *CommentStack
}
func newContext(analyzer *Analyzer, env *Environment) *Context {
return &Context{
Env: env,
analyzer: analyzer,
}
}
func (c *Context) WithPackage(pkg *packages.Package) *Context {
res := *c
res.pkg = pkg
return &res
}
func (c *Context) WithFile(file *ast.File) *Context {
res := *c
res.file = file
return &res
}
func (c *Context) Block() *Context {
res := *c
res.Env = NewEnvironment(c.Env)
res.commentStack = NewCommentStack(c.commentStack, nil)
return &res
}
func (c *Context) CommentStack() *CommentStack {
return c.commentStack
}
func (c *Context) Package() *packages.Package {
return c.pkg
}
func (c *Context) File() *ast.File {
return c.file
}
func (c *Context) LineColumn(pos token.Pos) string {
return c.pkg.Fset.Position(pos).String()
}
func (c *Context) GetDefinition(pkg, name string) Definition {
return c.analyzer.definitions[pkg+"."+name]
}
func (c *Context) ParseType(t types.Type) Definition {
switch t := t.(type) {
case *types.Pointer:
return c.ParseType(t.Elem())
case *types.Named:
return c.GetDefinition(t.Obj().Pkg().Path(), t.Obj().Name())
case types.Object:
return c.GetDefinition(t.Pkg().Path(), t.Name())
}
return nil
}
func (c *Context) Doc() *spec.T {
return c.analyzer.Doc()
}
func (c *Context) AddAPI(items ...*API) {
c.analyzer.AddRoutes(items...)
}
func (c *Context) ParseStatusCode(status ast.Expr) int {
switch status := status.(type) {
case *ast.SelectorExpr:
return c.ParseStatusCode(status.Sel)
case *ast.Ident:
obj := c.pkg.TypesInfo.Uses[status]
if obj == nil {
break
}
objConst, ok := obj.(*types.Const)
if !ok {
break
}
statusCode, err := strconv.ParseInt(objConst.Val().String(), 10, 64)
if err != nil {
break
}
return int(statusCode)
case *ast.BasicLit:
code, err := strconv.ParseInt(status.Value, 10, 64)
if err != nil {
fmt.Printf("unknown status code '%s' at %s\n", status.Value, c.LineColumn(status.Pos()))
break
}
return int(code)
default:
// unknown status code
fmt.Printf("unknown status code %s\n", c.LineColumn(status.Pos()))
}
// unknown status code
fmt.Printf("unknown status code %s\n", c.LineColumn(status.Pos()))
// fallback to 200
return 200
}
func (c *Context) GetSchemaByExpr(expr ast.Expr, contentType string) *spec.SchemaRef {
return NewSchemaBuilder(c, contentType).ParseExpr(expr)
}
func (c *Context) GetHeadingCommentOf(pos token.Pos) *ast.CommentGroup {
if c.File() == nil {
return nil
}
position := c.Package().Fset.Position(pos)
for _, commentGroup := range c.File().Comments {
start := c.Package().Fset.Position(commentGroup.Pos())
end := c.Package().Fset.Position(commentGroup.End())
if end.Line == position.Line-1 && start.Column <= position.Column {
return commentGroup
}
}
return nil
}
func (c *Context) GetTrailingCommentOf(pos token.Pos) *ast.CommentGroup {
if c.File() == nil {
return nil
}
position := c.Package().Fset.Position(pos)
for _, commentGroup := range c.File().Comments {
commentPos := c.Package().Fset.Position(commentGroup.End())
if commentPos.Line == position.Line {
return commentGroup
}
}
return nil
}
func (c *Context) APIs() *APIs {
return c.analyzer.APIs()
}
func (c *Context) NewEnv() *Context {
res := *c
res.Env = NewEnvironment(nil)
return &res
}
type CallRule struct {
Rules map[string][]string // typeName to function-names
}
func NewCallRule() *CallRule {
r := new(CallRule)
r.Rules = make(map[string][]string)
return r
}
func (c *CallRule) WithRule(typeName string, fnNames ...string) *CallRule {
c.Rules[typeName] = append(c.Rules[typeName], fnNames...)
return c
}
func (c *Context) MatchCall(n ast.Node, rule *CallRule, callback func(call *ast.CallExpr, typeName, fnName string)) {
callExpr, ok := n.(*ast.CallExpr)
if !ok {
return
}
actualTypeName, actualFnName, err := c.GetCallInfo(callExpr)
if err != nil {
return
}
for typeName, fnNames := range rule.Rules {
for _, fnName := range fnNames {
if typeName == actualTypeName && fnName == actualFnName {
callback(callExpr, typeName, fnName)
}
}
}
return
}
func (c *Context) GetFuncFromAstNode(n ast.Node) *types.Func {
var obj interface{}
switch handlerArg := n.(type) {
case *ast.Ident:
obj = c.Package().TypesInfo.ObjectOf(handlerArg)
case *ast.SelectorExpr:
obj = c.Package().TypesInfo.ObjectOf(handlerArg.Sel)
default:
return nil
}
fn, ok := obj.(*types.Func)
if !ok {
return nil
}
return fn
}
type CallInfo struct {
Type string
Method string
}
// GetCallInfo returns the package or type and name associated with a call expression
//
// e.g. GetCallInfo(`c.GET("/ping", ...)`) returns ("*github/gin-gonic/gin.RouterGroup", "GET", nil)
func (c *Context) GetCallInfo(n ast.Node) (string, string, error) {
switch node := n.(type) {
case *ast.CallExpr:
switch fn := node.Fun.(type) {
case *ast.SelectorExpr:
// try to parse type of sel.Sel
info := c.parseCallInfoByIdent(fn.Sel)
if info != nil {
return info.Type, info.Method, nil
}
switch expr := fn.X.(type) {
case *ast.Ident:
if expr.Obj != nil && expr.Obj.Kind == ast.Var {
t := c.Package().TypesInfo.TypeOf(expr)
if t != nil {
return t.String(), fn.Sel.Name, nil
}
return "undefined", fn.Sel.Name, fmt.Errorf("missing type info")
}
return expr.Name, fn.Sel.Name, nil
case *ast.SelectorExpr:
if expr.Sel != nil {
t := c.Package().TypesInfo.TypeOf(expr.Sel)
if t != nil {
return t.String(), fn.Sel.Name, nil
}
return "undefined", fn.Sel.Name, fmt.Errorf("missing type info")
}
case *ast.CallExpr:
switch call := expr.Fun.(type) {
case *ast.Ident:
if call.Name == "new" {
t := c.Package().TypesInfo.TypeOf(expr.Args[0])
if t != nil {
return t.String(), fn.Sel.Name, nil
}
return "undefined", fn.Sel.Name, fmt.Errorf("missing type info")
}
if call.Obj != nil {
switch decl := call.Obj.Decl.(type) {
case *ast.FuncDecl:
ret := decl.Type.Results
if ret != nil && len(ret.List) > 0 {
ret1 := ret.List[0]
if ret1 != nil {
t := c.Package().TypesInfo.TypeOf(ret1.Type)
if t != nil {
return t.String(), fn.Sel.Name, nil
}
return "undefined", fn.Sel.Name, fmt.Errorf("missing type info")
}
}
}
}
}
}
case *ast.Ident:
// try to parse type of sel.Sel
info := c.parseCallInfoByIdent(fn)
if info != nil {
return info.Type, info.Method, nil
}
return c.Package().Name, fn.Name, nil
}
}
return "", "", fmt.Errorf("unable to determine call info")
}
func (c *Context) parseCallInfoByIdent(ident *ast.Ident) (info *CallInfo) {
info = &CallInfo{}
t := c.Package().TypesInfo.ObjectOf(ident)
fn, ok := t.(*types.Func)
if !ok {
return nil
}
info.Type, info.Method = utils.GetFuncInfo(fn)
return
}
func (c *Context) ParseComment(commentGroup *ast.CommentGroup) *Comment {
return ParseComment(commentGroup, c.Package().Fset)
}