package rest import ( "fmt" "git.ali33.ru/fcg-xvii/go-tools/json" ) func parseCondition(m json.Map, index int) (*Condition, error) { errPrefix := func(errData string) error { return fmt.Errorf("[%v]: %s", index, errData) } cond := Condition{ Field: m.String("field", ""), Logic: ConditionLogic(m.String("logic", "")), Operator: ConditionOperator(m.String("operator", "")), Value: m["value"], } if !cond.IsValid() { if len(cond.Field) == 0 { return nil, errPrefix("empty field name") } else if !cond.Operator.IsValid() { return nil, errPrefix(fmt.Sprintf("unexpected operator [%s]", cond.Operator)) } else if !cond.Operator.IsValid() { return nil, errPrefix(fmt.Sprintf("unexpected logic [%s]", cond.Logic)) } else { return nil, errPrefix("unexpected error") } } return &cond, nil } ////////////////////////////////////////////////////////////////////////////// type ConditionOperator string const ( OperatorEqual = "=" OperatorNotEqual = "!=" OperatorMore = ">" OperatorLess = "<" OperatorMoreEqual = ">=" OperatorLessEqual = "<=" OperatorLike = "like" ) func (s ConditionOperator) IsValid() bool { switch s { case OperatorEqual: case OperatorNotEqual: case OperatorMore: case OperatorLess: case OperatorMoreEqual: case OperatorLessEqual: case OperatorLike: 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 Logic ConditionLogic Operator ConditionOperator Value any } func (s *Condition) IsValid() bool { return len(s.Field) > 0 && s.Logic.IsValid() && s.Operator.IsValid() }