package rest import ( "fmt" "git.ali33.ru/fcg-xvii/go-tools/json" ) func parseCondition(m json.Map, index int) (ICondition, 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 ICondition interface { Field() string } ////////////////////////////////////////////////////////////////////////////// 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) Field() string { return s.field } func (s *_condition) Logic() _conditionLogic { return s.logic } func (s *_condition) Operator() _conditionOperator { return s.operator } func (s *_condition) IsValid() bool { return len(s.field) > 0 && s.logic.IsValid() && s.operator.IsValid() }