-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathg_chi_docs.go
268 lines (211 loc) · 4.77 KB
/
g_chi_docs.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
package main
import (
"errors"
"go/ast"
"go/parser"
"go/token"
path "path/filepath"
"strings"
"github.com/astaxie/beego/swagger"
)
const (
chiPath = "pkg/router/routes.go"
handlerPrefixCHI = "github.com/zalora/doraemon/handlers/"
)
func generateChiDocs(curpath string) error {
fset := token.NewFileSet()
f, err := parser.ParseFile(
fset,
path.Join(curpath, chiPath),
nil,
parser.ParseComments)
if err != nil {
return err
}
for _, im := range f.Imports {
var localName string
if im.Name != nil {
localName = im.Name.Name
}
analisyscontrollerPkg(localName, im.Path.Value)
}
for rt, item := range chiAPIs {
baseURLSplit := strings.Split(rt, "/")
if len(baseURLSplit) <= 1 {
continue
}
tag := baseURLSplit[1]
appendTag(item.Get, tag)
appendTag(item.Post, tag)
appendTag(item.Put, tag)
appendTag(item.Patch, tag)
appendTag(item.Head, tag)
appendTag(item.Delete, tag)
appendTag(item.Options, tag)
if len(rootapi.Paths) == 0 {
rootapi.Paths = make(map[string]*swagger.Item)
}
rt = urlReplace(rt)
rootapi.Paths[rt] = item
}
rootapi.Tags = append(rootapi.Tags, generateChiTags(f, fset)...)
return nil
}
func appendTag(op *swagger.Operation, tag string) {
if op == nil {
return
}
op.Tags = append(op.Tags, tag)
}
func isCHI(pkgpath string) bool {
return strings.HasPrefix(pkgpath, handlerPrefixCHI)
}
func generateChiTags(node *ast.File, fset *token.FileSet) []swagger.Tag {
var lineRouteMap map[int]string
for _, decl := range node.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
if funcDecl.Name.Name != "New" {
continue
}
funcLit, err := findMuxGroupArg(funcDecl)
if err != nil {
continue
}
route, err := findRouteCall(funcLit, fset)
if err != nil {
continue
}
lineRouteMap = extractLineRouteMap(route, fset)
break
}
lineCommentMap := extractLineCommentMap(node.Comments, fset)
var tags []swagger.Tag
for line, route := range lineRouteMap {
comment, ok := lineCommentMap[line-1]
if !ok {
continue
}
tags = append(tags, swagger.Tag{
Name: route,
Description: comment + "\n",
})
}
return tags
}
func findMuxGroupArg(funcDecl *ast.FuncDecl) (*ast.FuncLit, error) {
for _, stmt := range funcDecl.Body.List {
expr, ok := stmt.(*ast.ExprStmt)
if !ok {
continue
}
callExpr, ok := expr.X.(*ast.CallExpr)
if !ok {
continue
}
// mux.Group(func(r chi.Router) { ... }
if !assertCallExpression(callExpr, "mux", []string{"Group"}) {
continue
}
if len(callExpr.Args) != 1 {
continue
}
funcLit, ok := callExpr.Args[0].(*ast.FuncLit)
if !ok {
continue
}
return funcLit, nil
}
return nil, errors.New("mux group arg is not found")
}
func findRouteCall(funcLit *ast.FuncLit, fset *token.FileSet) (*ast.CallExpr, error) {
for _, stmt := range funcLit.Body.List {
expr, ok := stmt.(*ast.ExprStmt)
if !ok {
continue
}
callExpr, ok := expr.X.(*ast.CallExpr)
if !ok {
continue
}
// r.Route("v1", func(r chi.Router) { ... })
if !assertCallExpression(callExpr, "r", []string{"Route"}) {
continue
}
return callExpr, nil
}
return nil, errors.New("no route is found")
}
func assertCallExpression(callExpr *ast.CallExpr, object string, functions []string) bool {
selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok {
return false
}
ident, ok := selExpr.X.(*ast.Ident)
if !ok {
return false
}
if ident.Obj == nil {
return false
}
if ident.Obj.Name != object {
return false
}
for _, function := range functions {
if selExpr.Sel.Name == function {
return true
}
}
return false
}
func extractLineRouteMap(callExpr *ast.CallExpr, fset *token.FileSet) map[int]string {
if len(callExpr.Args) != 2 {
return nil
}
fnExpr := callExpr.Args[1]
fn, ok := fnExpr.(*ast.FuncLit)
if !ok {
return nil
}
lineRouteMap := make(map[int]string)
for _, stmt := range fn.Body.List {
exprStmt, ok := stmt.(*ast.ExprStmt)
if !ok {
continue
}
callExpr, ok := exprStmt.X.(*ast.CallExpr)
if !ok {
continue
}
if len(callExpr.Args) != 2 {
return nil
}
patternExpr := callExpr.Args[0]
patternBasicLit, ok := patternExpr.(*ast.BasicLit)
if !ok {
return nil
}
pattern := strings.Trim(patternBasicLit.Value, `"/`)
selectorExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok {
continue
}
if selectorExpr.Sel.Name != "Route" {
continue
}
pos := fset.Position(selectorExpr.Pos())
lineRouteMap[pos.Line] = pattern
}
return lineRouteMap
}
func extractLineCommentMap(comments []*ast.CommentGroup, fset *token.FileSet) map[int]string {
lineCommentMap := make(map[int]string)
for _, cg := range comments {
for _, c := range cg.List {
lineCommentMap[fset.Position(c.Pos()).Line] = strings.TrimLeft(c.Text, "/ ")
}
}
return lineCommentMap
}