2017-11-08 07:28:33 +00:00
|
|
|
package main
|
|
|
|
|
2019-07-12 08:33:25 +00:00
|
|
|
import "strings"
|
|
|
|
|
2017-11-08 07:28:33 +00:00
|
|
|
type RouteGroup struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
Path string
|
|
|
|
RouteList []*RouteImpl
|
|
|
|
RunBefore []Runnable
|
2018-11-12 09:23:36 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
NoHead bool
|
2017-11-08 07:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-11-08 07:28:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 08:33:25 +00:00
|
|
|
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}
|
2017-11-08 07:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-11-08 07:28:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 08:33:25 +00:00
|
|
|
func (g *RouteGroup) NoHeader() *RouteGroup {
|
2022-02-21 03:32:53 +00:00
|
|
|
g.NoHead = true
|
|
|
|
return g
|
2018-11-12 09:23:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 08:33:25 +00:00
|
|
|
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
|
2017-11-10 03:33:11 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 08:33:25 +00:00
|
|
|
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
|
2017-11-08 07:28:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 08:33:25 +00:00
|
|
|
/*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
|
2019-07-12 08:33:25 +00:00
|
|
|
}*/
|
|
|
|
|
|
|
|
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
|
2017-11-08 07:28:33 +00:00
|
|
|
}
|