conditions.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 OperatorEmpty:
  26. default:
  27. return false
  28. }
  29. return true
  30. }
  31. //////////////////////////////////////////////////////////////////////////////
  32. const (
  33. LogicEmpty = ""
  34. LogicOR = "or"
  35. LogicAND = "and"
  36. )
  37. type ConditionLogic string
  38. func (s ConditionLogic) IsValid() bool {
  39. switch s {
  40. case LogicEmpty:
  41. case LogicOR:
  42. case LogicAND:
  43. default:
  44. return false
  45. }
  46. return true
  47. }
  48. //////////////////////////////////////////////////////////////////////////////
  49. type Condition struct {
  50. Field string `json:"Field"`
  51. Logic ConditionLogic `json:"Logic"`
  52. Operator ConditionOperator `json:"Operator"`
  53. Value any `json:"Value"`
  54. }
  55. func (s *Condition) IsValid() bool {
  56. return len(s.Field) > 0 && s.Logic.IsValid() && s.Operator.IsValid()
  57. }
  58. //////////////////////////////////////////////////////////////////////////////
  59. type ConditionList []*Condition
  60. func (s *ConditionList) FieldExists(name string) bool {
  61. for _, v := range *s {
  62. if v.Field == name {
  63. return true
  64. }
  65. }
  66. return false
  67. }
  68. func (s *ConditionList) FieldsRemove(toRemove []string) {
  69. toRemoveMap := make(map[string]struct{})
  70. for _, item := range toRemove {
  71. toRemoveMap[item] = struct{}{}
  72. }
  73. var result ConditionList
  74. for _, item := range *s {
  75. if _, found := toRemoveMap[item.Field]; !found {
  76. result = append(result, item)
  77. }
  78. }
  79. *s = result
  80. }