12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package rest
- type ConditionOperator string
- const (
- OperatorEqual = "="
- OperatorNotEqual = "!="
- OperatorMore = ">"
- OperatorLess = "<"
- OperatorMoreEqual = ">="
- OperatorLessEqual = "<="
- OperatorLike = "like"
- OperatorNot = "not"
- OperatorExists = "is"
- OperatorEmpty = ""
- )
- func (s ConditionOperator) IsValid() bool {
- switch s {
- case OperatorEqual:
- case OperatorNotEqual:
- case OperatorMore:
- case OperatorLess:
- case OperatorMoreEqual:
- case OperatorLessEqual:
- case OperatorLike:
- case OperatorNot:
- case OperatorEmpty:
- default:
- return false
- }
- return true
- }
- //////////////////////////////////////////////////////////////////////////////
- const (
- LogicEmpty = ""
- LogicOR = "or"
- LogicAND = "and"
- )
- type ConditionLogic string
- func (s ConditionLogic) IsValid() bool {
- switch s {
- case LogicEmpty:
- case LogicOR:
- case LogicAND:
- default:
- return false
- }
- return true
- }
- //////////////////////////////////////////////////////////////////////////////
- type Condition struct {
- Field string `json:"Field"`
- Logic ConditionLogic `json:"Logic"`
- Operator ConditionOperator `json:"Operator"`
- Value any `json:"Value"`
- }
- func (s *Condition) IsValid() bool {
- return len(s.Field) > 0 && s.Logic.IsValid() && s.Operator.IsValid()
- }
- //////////////////////////////////////////////////////////////////////////////
- type ConditionList []*Condition
- func (s *ConditionList) FieldExists(name string) bool {
- for _, v := range *s {
- if v.Field == name {
- return true
- }
- }
- return false
- }
- func (s *ConditionList) FieldsRemove(toRemove []string) {
- toRemoveMap := make(map[string]struct{})
- for _, item := range toRemove {
- toRemoveMap[item] = struct{}{}
- }
- var result ConditionList
- for _, item := range *s {
- if _, found := toRemoveMap[item.Field]; !found {
- result = append(result, item)
- }
- }
- *s = result
- }
|