/
route_group.go
85 lines (74 loc) · 1.77 KB
/
route_group.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
package main
import "strings"
type RouteGroup struct {
Path string
RouteList []*RouteImpl
RunBefore []Runnable
NoHead bool
}
func newRouteGroup(path string, routes ...*RouteImpl) *RouteGroup {
g := &RouteGroup{Path: path}
for _, route := range routes {
route.Parent = g
g.RouteList = append(g.RouteList, route)
}
return g
}
func (g *RouteGroup) Not(path ...string) *RouteSubset {
routes := make([]*RouteImpl, len(g.RouteList))
copy(routes, g.RouteList)
for i, route := range routes {
if inStringList(route.Path, path) {
routes = append(routes[:i], routes[i+1:]...)
}
}
return &RouteSubset{routes}
}
func inStringList(needle string, list []string) bool {
for _, item := range list {
if item == needle {
return true
}
}
return false
}
func (g *RouteGroup) NoHeader() *RouteGroup {
g.NoHead = true
return g
}
func (g *RouteGroup) Before(lines ...string) *RouteGroup {
for _, line := range lines {
g.RunBefore = append(g.RunBefore, Runnable{line, false})
}
return g
}
func (g *RouteGroup) LitBefore(lines ...string) *RouteGroup {
for _, line := range lines {
g.RunBefore = append(g.RunBefore, Runnable{line, true})
}
return g
}
/*func (g *RouteGroup) Routes(routes ...*RouteImpl) *RouteGroup {
for _, route := range routes {
route.Parent = g
g.RouteList = append(g.RouteList, route)
}
return g
}*/
func (g *RouteGroup) Routes(routes ...interface{}) *RouteGroup {
for _, route := range routes {
switch r := route.(type) {
case *RouteImpl:
r.Parent = g
g.RouteList = append(g.RouteList, r)
case RouteSet:
for _, rr := range r.Items {
rr.Name = r.Name + rr.Name
rr.Path = strings.TrimSuffix(r.Path, "/") + "/" + strings.TrimPrefix(rr.Path, "/")
rr.Parent = g
g.RouteList = append(g.RouteList, rr)
}
}
}
return g
}