sort.go 1014 B

123456789101112131415161718192021222324252627282930
  1. package rule
  2. import "ehang.io/nps/core/process"
  3. type Sort []*Rule
  4. func (s Sort) Len() int { return len(s) }
  5. func (s Sort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  6. // Less rule sort by
  7. func (s Sort) Less(i, j int) bool {
  8. iHandlerSort := orderMap[s[i].Handler.GetName()]
  9. iProcessSort := orderMap[s[i].Process.GetName()]
  10. jHandlerSort := orderMap[s[j].Handler.GetName()]
  11. jProcessSort := orderMap[s[j].Process.GetName()]
  12. iSort := iHandlerSort<<16 | iProcessSort<<8
  13. jSort := jHandlerSort<<16 | jProcessSort<<8
  14. if vi, ok := s[i].Process.(*process.HttpServeProcess); ok {
  15. if vj, ok := s[j].Process.(*process.HttpServeProcess); ok {
  16. iSort = iSort | (len(vj.RouteUrl) & (2 ^ 8 - 1))
  17. jSort = jSort | (len(vi.RouteUrl) & (2 ^ 8 - 1))
  18. }
  19. }
  20. if vi, ok := s[i].Process.(*process.HttpsServeProcess); ok {
  21. if vj, ok := s[j].Process.(*process.HttpsServeProcess); ok {
  22. iSort = iSort | (len(vj.RouteUrl) & (2 ^ 8 - 1))
  23. jSort = jSort | (len(vi.RouteUrl) & (2 ^ 8 - 1))
  24. }
  25. }
  26. return iSort > jSort
  27. }