route.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package pomeloMessage
  2. import (
  3. "strings"
  4. cconst "github.com/mhaya/const"
  5. cerr "github.com/mhaya/error"
  6. )
  7. // Route struct
  8. type Route struct {
  9. nodeType string // node server type name
  10. handleName string // handle name
  11. method string // method name
  12. }
  13. func (r *Route) NodeType() string {
  14. return r.nodeType
  15. }
  16. func (r *Route) HandleName() string {
  17. return r.handleName
  18. }
  19. func (r *Route) Method() string {
  20. return r.method
  21. }
  22. // NewRoute create a new route
  23. func NewRoute(nodeType, handleName, method string) *Route {
  24. return &Route{nodeType, handleName, method}
  25. }
  26. // String transforms the route into a string
  27. func (r *Route) String() string {
  28. return r.nodeType + cconst.DOT + r.handleName + cconst.DOT + r.method
  29. }
  30. // DecodeRoute decodes the route
  31. func DecodeRoute(route string) (*Route, error) {
  32. if route == "" {
  33. return nil, cerr.RouteFieldCantEmpty
  34. }
  35. r := strings.Split(route, cconst.DOT)
  36. for _, s := range r {
  37. if strings.TrimSpace(s) == "" {
  38. return nil, cerr.RouteFieldCantEmpty
  39. }
  40. }
  41. if len(r) != 3 {
  42. return nil, cerr.RouteInvalid
  43. }
  44. return NewRoute(r[0], r[1], r[2]), nil
  45. }