conditions.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package rest
  2. type ConditionOperator string
  3. const (
  4. OperatorEqual = "="
  5. OperatorNotEqual = "!="
  6. OperatorMore = ">"
  7. OperatorLess = "<"
  8. OperatorMoreEqual = ">="
  9. OperatorLessEqual = "<="
  10. OperatorLike = "like"
  11. OperatorNot = "not"
  12. OperatorExists = "is"
  13. OperatorEmpty = ""
  14. )
  15. func (s ConditionOperator) IsValid() bool {
  16. switch s {
  17. case OperatorEqual:
  18. case OperatorNotEqual:
  19. case OperatorMore:
  20. case OperatorLess:
  21. case OperatorMoreEqual:
  22. case OperatorLessEqual:
  23. case OperatorLike:
  24. case OperatorNot:
  25. case OperatorExists:
  26. case OperatorEmpty:
  27. default:
  28. return false
  29. }
  30. return true
  31. }
  32. //////////////////////////////////////////////////////////////////////////////
  33. const (
  34. LogicEmpty = ""
  35. LogicOR = "or"
  36. LogicAND = "and"
  37. )
  38. type ConditionLogic string
  39. func (s ConditionLogic) IsValid() bool {
  40. switch s {
  41. case LogicEmpty:
  42. case LogicOR:
  43. case LogicAND:
  44. default:
  45. return false
  46. }
  47. return true
  48. }
  49. const (
  50. MethodEmpty = ""
  51. MethodLower = "lower"
  52. MethodUpper = "upper"
  53. )
  54. type ConditionMethod string
  55. func (s ConditionMethod) IsValid() bool {
  56. switch s {
  57. case MethodEmpty:
  58. case MethodLower:
  59. default:
  60. return false
  61. }
  62. return true
  63. }
  64. //////////////////////////////////////////////////////////////////////////////
  65. type Condition struct {
  66. Field string `json:"Field"`
  67. Logic ConditionLogic `json:"Logic"`
  68. Operator ConditionOperator `json:"Operator"`
  69. Method ConditionMethod `json:"Method"`
  70. Value any `json:"Value"`
  71. }
  72. func (s *Condition) IsValid() bool {
  73. return len(s.Field) > 0 && s.Logic.IsValid() && s.Operator.IsValid() && s.Method.IsValid()
  74. }
  75. //////////////////////////////////////////////////////////////////////////////
  76. type ConditionList []*Condition
  77. func (s *ConditionList) FieldExists(name string) bool {
  78. for _, v := range *s {
  79. if v.Field == name {
  80. return true
  81. }
  82. }
  83. return false
  84. }
  85. func (s *ConditionList) FieldsRemove(toRemove []string) {
  86. toRemoveMap := make(map[string]struct{})
  87. for _, item := range toRemove {
  88. toRemoveMap[item] = struct{}{}
  89. }
  90. var result ConditionList
  91. for _, item := range *s {
  92. if _, found := toRemoveMap[item.Field]; !found {
  93. result = append(result, item)
  94. }
  95. }
  96. *s = result
  97. }