conditions.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. Logic() ConditionLogic
  33. Operator() ConditionOperator
  34. Value() any
  35. }
  36. //////////////////////////////////////////////////////////////////////////////
  37. type ConditionOperator string
  38. const (
  39. OperatorEqual = "="
  40. OperatorNotEqual = "!="
  41. OperatorMore = ">"
  42. OperatorLess = "<"
  43. OperatorMoreEqual = ">="
  44. OperatorLessEqual = "<="
  45. OperatorLike = "like"
  46. )
  47. func (s ConditionOperator) IsValid() bool {
  48. switch s {
  49. case OperatorEqual:
  50. case OperatorNotEqual:
  51. case OperatorMore:
  52. case OperatorLess:
  53. case OperatorMoreEqual:
  54. case OperatorLessEqual:
  55. case OperatorLike:
  56. default:
  57. return false
  58. }
  59. return true
  60. }
  61. //////////////////////////////////////////////////////////////////////////////
  62. const (
  63. LogicEmpty = ""
  64. LogicOR = "or"
  65. LogicAND = "and"
  66. )
  67. type ConditionLogic string
  68. func (s ConditionLogic) IsValid() bool {
  69. switch s {
  70. case LogicEmpty:
  71. case LogicOR:
  72. case LogicAND:
  73. default:
  74. return false
  75. }
  76. return true
  77. }
  78. //////////////////////////////////////////////////////////////////////////////
  79. type _condition struct {
  80. field string
  81. logic ConditionLogic
  82. operator ConditionOperator
  83. value any
  84. }
  85. func (s *_condition) Field() string {
  86. return s.field
  87. }
  88. func (s *_condition) Logic() ConditionLogic {
  89. return s.logic
  90. }
  91. func (s *_condition) Operator() ConditionOperator {
  92. return s.operator
  93. }
  94. func (s *_condition) Value() any {
  95. return s.value
  96. }
  97. func (s *_condition) IsValid() bool {
  98. return len(s.field) > 0 && s.logic.IsValid() && s.operator.IsValid()
  99. }