conditions.go 1.8 KB

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