github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/router_gen/route_group.go (about) 1 package main 2 3 import "strings" 4 5 type RouteGroup struct { 6 Path string 7 RouteList []*RouteImpl 8 RunBefore []Runnable 9 10 NoHead bool 11 } 12 13 func newRouteGroup(path string, routes ...*RouteImpl) *RouteGroup { 14 g := &RouteGroup{Path: path} 15 for _, route := range routes { 16 route.Parent = g 17 g.RouteList = append(g.RouteList, route) 18 } 19 return g 20 } 21 22 func (g *RouteGroup) Not(path ...string) *RouteSubset { 23 routes := make([]*RouteImpl, len(g.RouteList)) 24 copy(routes, g.RouteList) 25 for i, route := range routes { 26 if inStringList(route.Path, path) { 27 routes = append(routes[:i], routes[i+1:]...) 28 } 29 } 30 return &RouteSubset{routes} 31 } 32 33 func inStringList(needle string, list []string) bool { 34 for _, item := range list { 35 if item == needle { 36 return true 37 } 38 } 39 return false 40 } 41 42 func (g *RouteGroup) NoHeader() *RouteGroup { 43 g.NoHead = true 44 return g 45 } 46 47 func (g *RouteGroup) Before(lines ...string) *RouteGroup { 48 for _, line := range lines { 49 g.RunBefore = append(g.RunBefore, Runnable{line, false}) 50 } 51 return g 52 } 53 54 func (g *RouteGroup) LitBefore(lines ...string) *RouteGroup { 55 for _, line := range lines { 56 g.RunBefore = append(g.RunBefore, Runnable{line, true}) 57 } 58 return g 59 } 60 61 /*func (g *RouteGroup) Routes(routes ...*RouteImpl) *RouteGroup { 62 for _, route := range routes { 63 route.Parent = g 64 g.RouteList = append(g.RouteList, route) 65 } 66 return g 67 }*/ 68 69 func (g *RouteGroup) Routes(routes ...interface{}) *RouteGroup { 70 for _, route := range routes { 71 switch r := route.(type) { 72 case *RouteImpl: 73 r.Parent = g 74 g.RouteList = append(g.RouteList, r) 75 case RouteSet: 76 for _, rr := range r.Items { 77 rr.Name = r.Name + rr.Name 78 rr.Path = strings.TrimSuffix(r.Path, "/") + "/" + strings.TrimPrefix(rr.Path, "/") 79 rr.Parent = g 80 g.RouteList = append(g.RouteList, rr) 81 } 82 } 83 } 84 return g 85 }