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 {
|
|
|
|
Path string
|
|
|
|
RouteList []*RouteImpl
|
|
|
|
RunBefore []Runnable
|
2018-11-12 09:23:36 +00:00
|
|
|
|
|
|
|
NoHead bool
|
2017-11-08 07:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRouteGroup(path string, routes ...*RouteImpl) *RouteGroup {
|
2019-07-12 08:33:25 +00:00
|
|
|
g := &RouteGroup{Path: path}
|
2017-11-12 07:18:25 +00:00
|
|
|
for _, route := range routes {
|
2019-07-12 08:33:25 +00:00
|
|
|
route.Parent = g
|
|
|
|
g.RouteList = append(g.RouteList, route)
|
2017-11-12 07:18:25 +00:00
|
|
|
}
|
2019-07-12 08:33:25 +00:00
|
|
|
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 {
|
|
|
|
routes := make([]*RouteImpl, len(g.RouteList))
|
|
|
|
copy(routes, g.RouteList)
|
2017-11-08 07:28:33 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-12 08:33:25 +00:00
|
|
|
func (g *RouteGroup) NoHeader() *RouteGroup {
|
|
|
|
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 {
|
2017-11-10 03:33:11 +00:00
|
|
|
for _, line := range lines {
|
2019-07-12 08:33:25 +00:00
|
|
|
g.RunBefore = append(g.RunBefore, Runnable{line, false})
|
2017-11-10 03:33:11 +00:00
|
|
|
}
|
2019-07-12 08:33:25 +00:00
|
|
|
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 {
|
2017-11-10 03:33:11 +00:00
|
|
|
for _, line := range lines {
|
2019-07-12 08:33:25 +00:00
|
|
|
g.RunBefore = append(g.RunBefore, Runnable{line, true})
|
2017-11-08 07:28:33 +00:00
|
|
|
}
|
2019-07-12 08:33:25 +00:00
|
|
|
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 {
|
2017-11-12 07:18:25 +00:00
|
|
|
for _, route := range routes {
|
2019-07-12 08:33:25 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 07:18:25 +00:00
|
|
|
}
|
2019-07-12 08:33:25 +00:00
|
|
|
return g
|
2017-11-08 07:28:33 +00:00
|
|
|
}
|