Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 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 | type sortableCurlyRoutes []curlyRoute |
| 15 | |
| 16 | func (s *sortableCurlyRoutes) add(route curlyRoute) { |
| 17 | *s = append(*s, route) |
| 18 | } |
| 19 | |
| 20 | func (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 | |
| 27 | func (s sortableCurlyRoutes) Len() int { |
| 28 | return len(s) |
| 29 | } |
| 30 | func (s sortableCurlyRoutes) Swap(i, j int) { |
| 31 | s[i], s[j] = s[j], s[i] |
| 32 | } |
| 33 | func (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 | } |