Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / github.com / emicklei / go-restful / curly_route.go
1 package restful
2
3 // Copyright 2013 Ernest Micklei. All rights reserved.
4 // Use of this source code is governed by a license
5 // that can be found in the LICENSE file.
6
7 // curlyRoute exits for sorting Routes by the CurlyRouter based on number of parameters and number of static path elements.
8 type curlyRoute struct {
9         route       Route
10         paramCount  int
11         staticCount int
12 }
13
14 // sortableCurlyRoutes orders by most parameters and path elements first.
15 type sortableCurlyRoutes []curlyRoute
16
17 func (s *sortableCurlyRoutes) add(route curlyRoute) {
18         *s = append(*s, route)
19 }
20
21 func (s sortableCurlyRoutes) routes() (routes []Route) {
22         routes = make([]Route, 0, len(s))
23         for _, each := range s {
24                 routes = append(routes, each.route) // TODO change return type
25         }
26         return routes
27 }
28
29 func (s sortableCurlyRoutes) Len() int {
30         return len(s)
31 }
32 func (s sortableCurlyRoutes) Swap(i, j int) {
33         s[i], s[j] = s[j], s[i]
34 }
35 func (s sortableCurlyRoutes) Less(i, j int) bool {
36         a := s[j]
37         b := s[i]
38
39         // primary key
40         if a.staticCount < b.staticCount {
41                 return true
42         }
43         if a.staticCount > b.staticCount {
44                 return false
45         }
46         // secundary key
47         if a.paramCount < b.paramCount {
48                 return true
49         }
50         if a.paramCount > b.paramCount {
51                 return false
52         }
53         return a.route.Path < b.route.Path
54 }