ソースを参照

serialize bugfix

0x4a52466c696e74 1 年間 前
コミット
b2626f1963
2 ファイル変更12 行追加42 行削除
  1. 0 35
      conditions.go
  2. 12 7
      serialize.go

+ 0 - 35
conditions.go

@@ -1,38 +1,5 @@
 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
-}
-
-//////////////////////////////////////////////////////////////////////////////
-
-// swagger:model ConditionOperator
 type ConditionOperator string
 
 const (
@@ -68,7 +35,6 @@ const (
 	LogicAND   = "and"
 )
 
-// swagger:model ConditionLogic
 type ConditionLogic string
 
 func (s ConditionLogic) IsValid() bool {
@@ -84,7 +50,6 @@ func (s ConditionLogic) IsValid() bool {
 
 //////////////////////////////////////////////////////////////////////////////
 
-// swagger:model Condition
 type Condition struct {
 	Field    string
 	Logic    ConditionLogic

+ 12 - 7
serialize.go

@@ -37,7 +37,9 @@ func parseType(from, to reflect.Value, fieldName string) IErrorArgs {
 	to = realValue(to)
 	switch to.Kind() {
 	case reflect.Interface:
-		to.Set(from)
+		if from.Kind() != reflect.Invalid {
+			to.Set(from)
+		}
 		return nil
 	case reflect.Struct:
 		return parseStruct(from, to, fieldName)
@@ -51,15 +53,18 @@ func parseType(from, to reflect.Value, fieldName string) IErrorArgs {
 }
 
 func realValue(val reflect.Value) reflect.Value {
-	//log.Println(val.IsNil())
-	if val.Kind() == reflect.Ptr {
-		if val.IsNil() {
+	if val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
+		if val.IsNil() && val.Kind() == reflect.Ptr {
 			val.Set(reflect.New(val.Type().Elem()))
 		}
 		// check
-		val = val.Elem()
-		if val.Kind() == reflect.Ptr {
-			return realValue(val)
+		cval := val.Elem()
+		if cval.Kind() == reflect.Invalid {
+			return val
+		}
+		val = cval
+		if val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
+			return realValue(cval)
 		}
 	}
 	return val