orders.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package rest
  2. import (
  3. "fmt"
  4. "reflect"
  5. "git.ali33.ru/fcg-xvii/go-tools/json"
  6. )
  7. type OrderType byte
  8. func parseOrder(order json.Map, parent string) (IOrderObject, error) {
  9. parentName := func(name string) string {
  10. if len(parent) > 0 {
  11. return fmt.Sprintf("%s.%s", parent, name)
  12. }
  13. return name
  14. }
  15. obj := &_orderObject{
  16. orders: make(map[string]IOrder),
  17. }
  18. for k, v := range order {
  19. rv := reflect.ValueOf(v)
  20. switch rv.Kind() {
  21. case reflect.Bool:
  22. obj.orders[k] = &_orderField{
  23. fieldName: k,
  24. isAsc: v.(bool),
  25. }
  26. case reflect.Map:
  27. t := reflect.TypeOf(order)
  28. if rv.CanConvert(t) {
  29. childRV := rv.Convert(t)
  30. child, err := parseOrder(childRV.Interface().(json.Map), parentName(k))
  31. if err != nil {
  32. return nil, err
  33. }
  34. obj.orders[k] = child
  35. }
  36. default:
  37. return nil, fmt.Errorf("unexpected type in [%v]", parentName(k))
  38. }
  39. }
  40. return obj, nil
  41. }
  42. const (
  43. OrderField OrderType = iota
  44. OrderObject
  45. )
  46. ////////////////////////////////////////////
  47. type IOrder interface {
  48. Type() OrderType
  49. }
  50. type IOrderField interface {
  51. IOrder
  52. FieldName() string
  53. IsAsc() bool
  54. }
  55. type IOrderObject interface {
  56. IOrder
  57. MapOrder() map[string]IOrder
  58. MapFields() map[string]IOrderField
  59. MapObjects() map[string]IOrderObject
  60. }
  61. ////////////////////////////////////////////
  62. type _orderField struct {
  63. fieldName string
  64. isAsc bool
  65. }
  66. func (s *_orderField) FieldName() string {
  67. return s.fieldName
  68. }
  69. func (s *_orderField) IsAsc() bool {
  70. return s.isAsc
  71. }
  72. func (s *_orderField) Type() OrderType {
  73. return OrderField
  74. }
  75. func (s *_orderField) String() string {
  76. return fmt.Sprintf("%v: %v\n", s.fieldName, s.isAsc)
  77. }
  78. ////////////////////////////////////////////
  79. type _orderObject struct {
  80. orders map[string]IOrder
  81. }
  82. func (s *_orderObject) Type() OrderType {
  83. return OrderObject
  84. }
  85. func (s *_orderObject) MapOrder() map[string]IOrder {
  86. return s.orders
  87. }
  88. func (s *_orderObject) MapFields() map[string]IOrderField {
  89. res := make(map[string]IOrderField)
  90. for k, v := range s.orders {
  91. if v.Type() == OrderField {
  92. res[k] = v.(IOrderField)
  93. }
  94. }
  95. return res
  96. }
  97. func (s *_orderObject) MapObjects() map[string]IOrderObject {
  98. res := make(map[string]IOrderObject)
  99. for k, v := range s.orders {
  100. if v.Type() == OrderObject {
  101. res[k] = v.(IOrderObject)
  102. }
  103. }
  104. return res
  105. }
  106. func (s *_orderObject) String() string {
  107. res := "{\n"
  108. for k, v := range s.orders {
  109. res += fmt.Sprintf("\t%s: %v", k, v)
  110. }
  111. res += "}\n"
  112. return res
  113. }