blob: 296f94650e655e7cc0473f109e8b3fef7fb9a712 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001package 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.
8type curlyRoute struct {
9 route Route
10 paramCount int
11 staticCount int
12}
13
14type sortableCurlyRoutes []curlyRoute
15
16func (s *sortableCurlyRoutes) add(route curlyRoute) {
17 *s = append(*s, route)
18}
19
20func (s sortableCurlyRoutes) routes() (routes []Route) {
21 for _, each := range s {
22 routes = append(routes, each.route) // TODO change return type
23 }
24 return routes
25}
26
27func (s sortableCurlyRoutes) Len() int {
28 return len(s)
29}
30func (s sortableCurlyRoutes) Swap(i, j int) {
31 s[i], s[j] = s[j], s[i]
32}
33func (s sortableCurlyRoutes) Less(i, j int) bool {
34 ci := s[i]
35 cj := s[j]
36
37 // primary key
38 if ci.staticCount < cj.staticCount {
39 return true
40 }
41 if ci.staticCount > cj.staticCount {
42 return false
43 }
44 // secundary key
45 if ci.paramCount < cj.paramCount {
46 return true
47 }
48 if ci.paramCount > cj.paramCount {
49 return false
50 }
51 return ci.route.Path < cj.route.Path
52}