conditions.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package rest
  2. import (
  3. "fmt"
  4. "git.ali33.ru/fcg-xvii/go-tools/json"
  5. )
  6. func parseCondition(m json.Map, index int) (ICondition, error) {
  7. errPrefix := func(errData string) error {
  8. return fmt.Errorf("[%v]: %s", index, errData)
  9. }
  10. cond := _condition{
  11. field: m.String("field", ""),
  12. logic: _conditionLogic(m.String("logic", "")),
  13. operator: _conditionOperator(m.String("operator", "")),
  14. value: m["value"],
  15. }
  16. if !cond.IsValid() {
  17. if len(cond.field) == 0 {
  18. return nil, errPrefix("empty field name")
  19. } else if !cond.operator.IsValid() {
  20. return nil, errPrefix(fmt.Sprintf("unexpected operator [%s]", cond.operator))
  21. } else if !cond.operator.IsValid() {
  22. return nil, errPrefix(fmt.Sprintf("unexpected logic [%s]", cond.logic))
  23. } else {
  24. return nil, errPrefix("unexpected error")
  25. }
  26. }
  27. return &cond, nil
  28. }
  29. //////////////////////////////////////////////////////////////////////////////
  30. type ICondition interface {
  31. Field() string
  32. }
  33. //////////////////////////////////////////////////////////////////////////////
  34. type _conditionOperator string
  35. const (
  36. OperatorEqual = "="
  37. OperatorNotEqual = "!="
  38. OperatorMore = ">"
  39. OperatorLess = "<"
  40. OperatorMoreEqual = ">="
  41. OperatorLessEqual = "<="
  42. OperatorLike = "like"
  43. )
  44. func (s _conditionOperator) IsValid() bool {
  45. switch s {
  46. case OperatorEqual:
  47. case OperatorNotEqual:
  48. case OperatorMore:
  49. case OperatorLess:
  50. case OperatorMoreEqual:
  51. case OperatorLessEqual:
  52. case OperatorLike:
  53. default:
  54. return false
  55. }
  56. return true
  57. }
  58. //////////////////////////////////////////////////////////////////////////////
  59. const (
  60. LogicEmpty = ""
  61. LogicOR = "or"
  62. LogicAND = "and"
  63. )
  64. type _conditionLogic string
  65. func (s _conditionLogic) IsValid() bool {
  66. switch s {
  67. case LogicEmpty:
  68. case LogicOR:
  69. case LogicAND:
  70. default:
  71. return false
  72. }
  73. return true
  74. }
  75. //////////////////////////////////////////////////////////////////////////////
  76. type _condition struct {
  77. field string
  78. logic _conditionLogic
  79. operator _conditionOperator
  80. value any
  81. }
  82. func (s *_condition) Field() string {
  83. return s.field
  84. }
  85. func (s *_condition) Logic() _conditionLogic {
  86. return s.logic
  87. }
  88. func (s *_condition) Operator() _conditionOperator {
  89. return s.operator
  90. }
  91. func (s *_condition) IsValid() bool {
  92. return len(s.field) > 0 && s.logic.IsValid() && s.operator.IsValid()
  93. }