/
route.go
359 lines (312 loc) · 7.74 KB
/
route.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package routegen
import (
"fmt"
"go/ast"
"go/token"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"golang.org/x/tools/go/packages"
)
type Route struct {
Name string
Sels []string
Path string // sub path
middlewareSel string
}
func newRoute(name string, sels []string, middlewareSel string, opt *RouteTypeCustomOption) *Route {
r := &Route{
Name: name,
Sels: sels,
middlewareSel: middlewareSel,
}
r.Path = r.routePathOpt(opt)
return r
}
func (s *Route) routePathOpt(opt *RouteTypeCustomOption) string {
path := ""
// sub route
if s.Name != "" {
// apply alias
if opt != nil && opt.PathComponentAlias != "" {
path = filepath.Join(path, opt.PathComponentAlias)
} else {
path = filepath.Join(path, kebabCaseString(s.Name))
}
}
return buildParamPath(path)
}
func (s *Route) isRootRoute() bool {
return s.Name == ""
}
func (s *Route) hasMiddleware() bool {
if s.middlewareSel == "" {
return false
}
for _, sel := range s.Sels {
if sel == s.middlewareSel {
return true
}
}
return false
}
type RoutePackage struct {
RelativePath string
RelativeGroupPath string
PkgPath string
Routes []*Route
SubPackages []*RoutePackage // for middleware
}
func (r *RoutePackage) rootRoute() *Route {
for _, route := range r.Routes {
if route.Name == "" {
return route
}
}
return nil
}
func (r *RoutePackage) routePath() string {
routePath := r.RelativePath
if r.RelativeGroupPath != "" {
routePath = r.RelativeGroupPath
}
return buildRoutePath(routePath)
}
type RouteTypeCustomOption struct {
PathComponentAlias string
}
type routeGroup struct {
path string
route *RoutePackage
}
type routeGen struct {
middlewareSel string
targetSels []string // sorted target selectors
sels map[string]struct{}
}
var routegenRegex = regexp.MustCompile(`^//\s*routegen\s+(.*)$`)
func newRouteGen(targetSelectors []string, middlewareSelector string) *routeGen {
r := &routeGen{
targetSels: targetSelectors,
middlewareSel: middlewareSelector,
}
set := make(map[string]struct{}, len(r.targetSels))
for _, s := range r.targetSels {
set[s] = struct{}{}
}
r.sels = set
return r
}
func (r *routeGen) parseRoute(root string) []*RoutePackage {
var routes []*RoutePackage
var groupStack []*routeGroup
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
var group *routeGroup
for len(groupStack) > 0 {
g := groupStack[len(groupStack)-1]
if strings.HasPrefix(path, g.path+"/") || g.path == root {
group = g
fmt.Println("\tgroup ->", group.path)
break
} else {
// pop stack
groupStack = groupStack[:len(groupStack)-1]
}
}
// get relative path
rel, _ := filepath.Rel(root, path)
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedCompiledGoFiles | packages.NeedSyntax,
Dir: path,
}
pkgs, err := packages.Load(cfg)
if err != nil {
return err
}
for _, pkg := range pkgs {
if route := r.processPkgRouteSels(pkg, rel); route != nil {
if rootRoute := route.rootRoute(); rootRoute != nil && rootRoute.hasMiddleware() {
fmt.Println("find middleware ->", path)
// push stack
groupStack = append(groupStack, &routeGroup{
path: path,
route: route,
})
}
// parent group
if group != nil {
grel, _ := filepath.Rel(group.path, path)
route.RelativeGroupPath = grel
// fmt.Println("group path", group.path)
// fmt.Println("rel", path)
// fmt.Println("grel", grel)
group.route.SubPackages = append(group.route.SubPackages, route)
} else {
routes = append(routes, route)
}
}
}
}
return nil
})
return routes
}
func (r *routeGen) processTypeOption(pkg *packages.Package) map[string]*RouteTypeCustomOption {
options := make(map[string]*RouteTypeCustomOption)
for _, f := range pkg.Syntax {
ast.Inspect(f, func(n ast.Node) bool {
if decl, ok := n.(*ast.GenDecl); ok {
if decl.Tok == token.TYPE && decl.Doc != nil {
var option RouteTypeCustomOption
setted := false
for _, comment := range decl.Doc.List {
match := routegenRegex.FindStringSubmatch(comment.Text)
if match != nil {
query := match[1]
if s := strings.SplitN(query, "=", 2); len(s) == 2 {
key, val := s[0], s[1]
if key == "alias" {
option.PathComponentAlias = val
setted = true
}
}
}
}
if setted {
if typeIdent := getTypeIdentFromGenDecl(decl); typeIdent != nil {
options[typeIdent.Name] = &option
}
}
}
}
return true
})
}
return options
}
func getTypeIdentFromGenDecl(decl *ast.GenDecl) *ast.Ident {
if decl.Tok == token.TYPE {
if len(decl.Specs) == 1 {
if ts, ok := decl.Specs[0].(*ast.TypeSpec); ok {
return ts.Name
}
}
}
return nil
}
func (r *routeGen) processPkgRouteSels(pkg *packages.Package, relativePath string) *RoutePackage {
route := &RoutePackage{
RelativePath: relativePath,
PkgPath: pkg.PkgPath,
}
// var routeNameSet []RouteSel
routeNameSet := make(map[string][]string)
for _, f := range pkg.Syntax {
ast.Inspect(f, func(n ast.Node) bool {
if fd, ok := n.(*ast.FuncDecl); ok {
sel := fd.Name.Name
if r.isTargetSelector(sel) {
routeName := ""
if rt := r.getFuncRecvType(fd); rt != nil {
// TODO: relativePath normalize
if !isPublicVar(rt) && relativePath != "." {
return true
}
routeName = rt.Name
}
routeNameSet[routeName] = append(routeNameSet[routeName], sel)
// fmt.Println("route", fd.Name, pkg.PkgPath)
}
}
return true
})
}
// generate routes
if len(routeNameSet) > 0 {
options := r.processTypeOption(pkg)
rs := make([]*Route, 0, len(routeNameSet))
// sort route name
routeNameKeys := make([]string, len(routeNameSet))
i := 0
for k := range routeNameSet {
routeNameKeys[i] = k
i++
}
sort.Strings(routeNameKeys)
for _, rn := range routeNameKeys {
sels := routeNameSet[rn]
opt := options[rn]
rs = append(rs, newRoute(rn, r.sortSels(sels), r.middlewareSel, opt))
}
route.Routes = rs
}
if len(route.Routes) == 0 {
return nil
}
return route
}
func (r *routeGen) sortSels(sels []string) []string {
if sels == nil {
return nil
}
sorted := make([]string, 0, len(sels))
for _, targetSel := range r.targetSels {
for _, sel := range sels {
if targetSel != sel {
continue
}
sorted = append(sorted, sel)
}
}
return sorted
}
func buildRoutePath(relativePath string, subPath ...string) string {
if relativePath == "." {
relativePath = ""
}
subPath = append([]string{"/", relativePath}, subPath...)
return buildParamPath(filepath.Join(subPath...))
}
func buildParamPath(path string) string {
pathComponents := strings.Split(path, "/")
for i, pathComponent := range pathComponents {
if pathComponent == "_" {
pathComponents[i] = "*"
} else if len(pathComponent) >= 2 {
if pathComponent[0:2] == "__" {
pathComponents[i] = pathComponent[1:]
} else if pathComponent[0:1] == "_" {
pathComponents[i] = ":" + pathComponent[1:]
}
}
}
return strings.Join(pathComponents, "/")
}
func isPublicVar(ident *ast.Ident) bool {
if len(ident.Name) == 0 {
return false
}
firstChar := string(ident.Name[0:1])
return firstChar == strings.ToUpper(firstChar)
}
func (r *routeGen) getFuncRecvType(fd *ast.FuncDecl) *ast.Ident {
if fd.Recv == nil || len(fd.Recv.List) != 1 {
return nil
}
switch recvType := fd.Recv.List[0].Type.(type) {
case *ast.Ident:
return recvType
case *ast.StarExpr:
if rt, ok := recvType.X.(*ast.Ident); ok {
return rt
}
}
return nil
}
func (r *routeGen) isTargetSelector(sel string) bool {
_, ok := r.sels[sel]
return ok
}