dict.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package pomeloMessage
  2. import (
  3. "strings"
  4. clog "github.com/mhaya/logger"
  5. )
  6. var (
  7. routes = make(map[string]uint16) // 路由信息映射为uint16
  8. codes = make(map[uint16]string) // uint16映射为路由信息
  9. )
  10. // SetDictionary set routes map which be used to compress route.
  11. func SetDictionary(dict map[string]uint16) {
  12. if dict == nil {
  13. return
  14. }
  15. for route, code := range dict {
  16. r := strings.TrimSpace(route) //去掉开头结尾的空格
  17. // duplication check
  18. if _, ok := routes[r]; ok {
  19. clog.Errorf("duplicated route(route: %s, code: %d)", r, code)
  20. return
  21. }
  22. if _, ok := codes[code]; ok {
  23. clog.Errorf("duplicated route(route: %s, code: %d)", r, code)
  24. return
  25. }
  26. // update map, using last value when key duplicated
  27. routes[r] = code
  28. codes[code] = r
  29. }
  30. }
  31. // GetDictionary gets the routes map which is used to compress route.
  32. func GetDictionary() map[string]uint16 {
  33. return routes
  34. }
  35. func GetRoute(code uint16) (route string, found bool) {
  36. route, found = codes[code]
  37. return route, found
  38. }
  39. func GetCode(route string) (uint16, bool) {
  40. code, found := routes[route]
  41. return code, found
  42. }