summaryrefslogtreecommitdiff
path: root/vendor/github.com/emicklei/go-restful/curly_route.go
blob: 296f94650e655e7cc0473f109e8b3fef7fb9a712 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
}

type sortableCurlyRoutes []curlyRoute

func (s *sortableCurlyRoutes) add(route curlyRoute) {
	*s = append(*s, route)
}

func (s sortableCurlyRoutes) routes() (routes []Route) {
	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 {
	ci := s[i]
	cj := s[j]

	// primary key
	if ci.staticCount < cj.staticCount {
		return true
	}
	if ci.staticCount > cj.staticCount {
		return false
	}
	// secundary key
	if ci.paramCount < cj.paramCount {
		return true
	}
	if ci.paramCount > cj.paramCount {
		return false
	}
	return ci.route.Path < cj.route.Path
}