curly_route.go 1.1 KB

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