k3s/vendor/github.com/emicklei/go-restful/curly_route.go

55 lines
1.2 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
package restful
// Copyright 2013 Ernest Micklei. All rights reserved.
// Use of this source code is governed by a license
// that can be found in the LICENSE file.
// curlyRoute exits for sorting Routes by the CurlyRouter based on number of parameters and number of static path elements.
type curlyRoute struct {
route Route
paramCount int
staticCount int
}
2019-09-27 21:51:53 +00:00
// sortableCurlyRoutes orders by most parameters and path elements first.
2019-01-12 04:58:27 +00:00
type sortableCurlyRoutes []curlyRoute
func (s *sortableCurlyRoutes) add(route curlyRoute) {
*s = append(*s, route)
}
func (s sortableCurlyRoutes) routes() (routes []Route) {
2019-09-27 21:51:53 +00:00
routes = make([]Route, 0, len(s))
2019-01-12 04:58:27 +00:00
for _, each := range s {
routes = append(routes, each.route) // TODO change return type
}
return routes
}
func (s sortableCurlyRoutes) Len() int {
return len(s)
}
func (s sortableCurlyRoutes) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sortableCurlyRoutes) Less(i, j int) bool {
2019-09-27 21:51:53 +00:00
a := s[j]
b := s[i]
2019-01-12 04:58:27 +00:00
// primary key
2019-09-27 21:51:53 +00:00
if a.staticCount < b.staticCount {
2019-01-12 04:58:27 +00:00
return true
}
2019-09-27 21:51:53 +00:00
if a.staticCount > b.staticCount {
2019-01-12 04:58:27 +00:00
return false
}
// secundary key
2019-09-27 21:51:53 +00:00
if a.paramCount < b.paramCount {
2019-01-12 04:58:27 +00:00
return true
}
2019-09-27 21:51:53 +00:00
if a.paramCount > b.paramCount {
2019-01-12 04:58:27 +00:00
return false
}
2019-09-27 21:51:53 +00:00
return a.route.Path < b.route.Path
2019-01-12 04:58:27 +00:00
}