Bladeren bron

conditions orders fix

0x4a52466c696e74 3 maanden geleden
bovenliggende
commit
b1558b73d1
4 gewijzigde bestanden met toevoegingen van 21 en 239 verwijderingen
  1. 18 43
      conditions.go
  2. 3 132
      orders.go
  3. 0 26
      request.go
  4. 0 38
      z_test.go

+ 18 - 43
conditions.go

@@ -6,23 +6,23 @@ import (
 	"git.ali33.ru/fcg-xvii/go-tools/json"
 )
 
-func parseCondition(m json.Map, index int) (ICondition, error) {
+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"],
+	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 {
+		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 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")
 		}
@@ -32,15 +32,6 @@ func parseCondition(m json.Map, index int) (ICondition, error) {
 
 //////////////////////////////////////////////////////////////////////////////
 
-type ICondition interface {
-	Field() string
-	Logic() ConditionLogic
-	Operator() ConditionOperator
-	Value() any
-}
-
-//////////////////////////////////////////////////////////////////////////////
-
 type ConditionOperator string
 
 const (
@@ -91,29 +82,13 @@ func (s ConditionLogic) IsValid() bool {
 
 //////////////////////////////////////////////////////////////////////////////
 
-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
+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()
+func (s *Condition) IsValid() bool {
+	return len(s.Field) > 0 && s.Logic.IsValid() && s.Operator.IsValid()
 }

+ 3 - 132
orders.go

@@ -1,135 +1,6 @@
 package rest
 
-import (
-	"fmt"
-	"reflect"
-
-	"git.ali33.ru/fcg-xvii/go-tools/json"
-)
-
-type OrderType byte
-
-func parseOrder(order json.Map, parent string) (IOrderObject, error) {
-	parentName := func(name string) string {
-		if len(parent) > 0 {
-			return fmt.Sprintf("%s.%s", parent, name)
-		}
-		return name
-	}
-	obj := &_orderObject{
-		orders: make(map[string]IOrder),
-	}
-	for k, v := range order {
-		rv := reflect.ValueOf(v)
-		switch rv.Kind() {
-		case reflect.Bool:
-			obj.orders[k] = &_orderField{
-				fieldName: k,
-				isAsc:     v.(bool),
-			}
-		case reflect.Map:
-			t := reflect.TypeOf(order)
-			if rv.CanConvert(t) {
-				childRV := rv.Convert(t)
-				child, err := parseOrder(childRV.Interface().(json.Map), parentName(k))
-				if err != nil {
-					return nil, err
-				}
-				obj.orders[k] = child
-			}
-		default:
-			return nil, fmt.Errorf("unexpected type in [%v]", parentName(k))
-		}
-	}
-	return obj, nil
-}
-
-const (
-	OrderField OrderType = iota
-	OrderObject
-)
-
-////////////////////////////////////////////
-
-type IOrder interface {
-	Type() OrderType
-}
-
-type IOrderField interface {
-	IOrder
-	FieldName() string
-	IsAsc() bool
-}
-
-type IOrderObject interface {
-	IOrder
-	MapOrder() map[string]IOrder
-	MapFields() map[string]IOrderField
-	MapObjects() map[string]IOrderObject
-}
-
-////////////////////////////////////////////
-
-type _orderField struct {
-	fieldName string
-	isAsc     bool
-}
-
-func (s *_orderField) FieldName() string {
-	return s.fieldName
-}
-
-func (s *_orderField) IsAsc() bool {
-	return s.isAsc
-}
-
-func (s *_orderField) Type() OrderType {
-	return OrderField
-}
-
-func (s *_orderField) String() string {
-	return fmt.Sprintf("%v: %v\n", s.fieldName, s.isAsc)
-}
-
-////////////////////////////////////////////
-
-type _orderObject struct {
-	orders map[string]IOrder
-}
-
-func (s *_orderObject) Type() OrderType {
-	return OrderObject
-}
-
-func (s *_orderObject) MapOrder() map[string]IOrder {
-	return s.orders
-}
-
-func (s *_orderObject) MapFields() map[string]IOrderField {
-	res := make(map[string]IOrderField)
-	for k, v := range s.orders {
-		if v.Type() == OrderField {
-			res[k] = v.(IOrderField)
-		}
-	}
-	return res
-}
-
-func (s *_orderObject) MapObjects() map[string]IOrderObject {
-	res := make(map[string]IOrderObject)
-	for k, v := range s.orders {
-		if v.Type() == OrderObject {
-			res[k] = v.(IOrderObject)
-		}
-	}
-	return res
-}
-
-func (s *_orderObject) String() string {
-	res := "{\n"
-	for k, v := range s.orders {
-		res += fmt.Sprintf("\t%s: %v", k, v)
-	}
-	res += "}\n"
-	return res
+type Order struct {
+	Name  string
+	IsAsc bool
 }

+ 0 - 26
request.go

@@ -29,8 +29,6 @@ type IRequest interface {
 	RFile(string) (IReadCloserLen, bool)
 	RClose()
 	Fields(any, RequestFiles) (json.Map, IErrorArgs)
-	ParseOrder() (IOrderObject, IErrorArgs)
-	ParseConditions() ([]ICondition, IErrorArgs)
 }
 
 ////////////////////////////////////////////
@@ -85,30 +83,6 @@ func (s *Request) ParseLimit() int {
 	return s.Data.Int32("limit", 20)
 }
 
-func (s *Request) ParseOrder() (IOrderObject, IErrorArgs) {
-	res, err := parseOrder(
-		s.Data.Map("order", json.Map{}),
-		"",
-	)
-	if err != nil {
-		return nil, ErrorFiled("order", err.Error())
-	}
-	return res, nil
-}
-
-func (s *Request) ParseConditions() (res []ICondition, rErr IErrorArgs) {
-	s.Data.EachMap("conditions", func(index int, m json.Map) bool {
-		cond, err := parseCondition(m, index)
-		if err != nil {
-			rErr = ErrorFiled("conditions", err.Error())
-			return false
-		}
-		res = append(res, cond)
-		return true
-	})
-	return
-}
-
 ////////////////////////////////////////////////////////////
 
 type IRequestIn interface {

+ 0 - 38
z_test.go

@@ -251,44 +251,6 @@ func TestServer(t *testing.T) {
 
 }
 
-func TestOrder(t *testing.T) {
-	order := json.Map{
-		"one": true,
-		"two": false,
-		"three": json.Map{
-			"c_one": true,
-			"c_two": false,
-		},
-	}
-
-	obj, err := parseOrder(order, "")
-	if err != nil {
-		t.Fatal(err)
-	}
-	t.Log("obj ============================")
-	t.Log(obj)
-	t.Log("fields ============================")
-	t.Log(obj.MapFields())
-	t.Log("objects ============================")
-	t.Log(obj.MapObjects())
-	t.Log("order ============================")
-	t.Log(obj.MapOrder())
-}
-
-func TestConditions(t *testing.T) {
-	src := json.Map{
-		"field":    "id",
-		"operator": "=",
-		"logic":    "and",
-		"value":    1,
-	}
-	cond, err := parseCondition(src, 0)
-	if err != nil {
-		t.Fatal(err)
-	}
-	t.Log(cond)
-}
-
 func TestFieldKeys(t *testing.T) {
 	l := []any{
 		"id",