123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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
- Logic() ConditionLogic
- Operator() ConditionOperator
- Value() any
- }
- //////////////////////////////////////////////////////////////////////////////
- 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) Value() any {
- return s.value
- }
- func (s *_condition) IsValid() bool {
- return len(s.field) > 0 && s.logic.IsValid() && s.operator.IsValid()
- }
|