gosora/router_gen/route_group.go

86 lines
2.0 KiB
Go
Raw Normal View History

package main
import "strings"
type RouteGroup struct {
2022-02-21 03:32:53 +00:00
Path string
RouteList []*RouteImpl
RunBefore []Runnable
2022-02-21 03:32:53 +00:00
NoHead bool
}
func newRouteGroup(path string, routes ...*RouteImpl) *RouteGroup {
2022-02-21 03:32:53 +00:00
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 {
2022-02-21 03:32:53 +00:00
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 {
2022-02-21 03:32:53 +00:00
for _, item := range list {
if item == needle {
return true
}
}
return false
}
func (g *RouteGroup) NoHeader() *RouteGroup {
2022-02-21 03:32:53 +00:00
g.NoHead = true
return g
}
func (g *RouteGroup) Before(lines ...string) *RouteGroup {
2022-02-21 03:32:53 +00:00
for _, line := range lines {
g.RunBefore = append(g.RunBefore, Runnable{line, false})
}
return g
}
func (g *RouteGroup) LitBefore(lines ...string) *RouteGroup {
2022-02-21 03:32:53 +00:00
for _, line := range lines {
g.RunBefore = append(g.RunBefore, Runnable{line, true})
}
return g
}
/*func (g *RouteGroup) Routes(routes ...*RouteImpl) *RouteGroup {
2022-02-21 03:32:53 +00:00
for _, route := range routes {
route.Parent = g
g.RouteList = append(g.RouteList, route)
}
return g
}*/
func (g *RouteGroup) Routes(routes ...interface{}) *RouteGroup {
2022-02-21 03:32:53 +00:00
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
}