conditions.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. //////////////////////////////////////////////////////////////////////////////
  50. type Condition struct {
  51. Field string `json:"Field"`
  52. Logic ConditionLogic `json:"Logic"`
  53. Operator ConditionOperator `json:"Operator"`
  54. Value any `json:"Value"`
  55. }
  56. func (s *Condition) IsValid() bool {
  57. return len(s.Field) > 0 && s.Logic.IsValid() && s.Operator.IsValid()
  58. }
  59. //////////////////////////////////////////////////////////////////////////////
  60. type ConditionList []*Condition
  61. func (s *ConditionList) FieldExists(name string) bool {
  62. for _, v := range *s {
  63. if v.Field == name {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69. func (s *ConditionList) FieldsRemove(toRemove []string) {
  70. toRemoveMap := make(map[string]struct{})
  71. for _, item := range toRemove {
  72. toRemoveMap[item] = struct{}{}
  73. }
  74. var result ConditionList
  75. for _, item := range *s {
  76. if _, found := toRemoveMap[item.Field]; !found {
  77. result = append(result, item)
  78. }
  79. }
  80. *s = result
  81. }