conditions.go 1.7 KB

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