123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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()
- }
|