From f9826104d8f566bfe8455fa335e3b7ffcec6de4c Mon Sep 17 00:00:00 2001 From: John Date: Sat, 8 Jun 2019 19:25:20 +0800 Subject: [PATCH] improve gconv --- g/util/gconv/gconv_struct.go | 28 +++++++++++++++------ g/util/gconv/gconv_z_unit_struct_test.go | 32 +++++++++++++++++++----- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/g/util/gconv/gconv_struct.go b/g/util/gconv/gconv_struct.go index 8ffe29a09..d5cc119c4 100644 --- a/g/util/gconv/gconv_struct.go +++ b/g/util/gconv/gconv_struct.go @@ -157,7 +157,9 @@ func StructDeep(params interface{}, pointer interface{}, mapping...map[string]st trv := rv.Field(i) switch trv.Kind() { case reflect.Struct: - StructDeep(params, trv, mapping...) + if err := StructDeep(params, trv, mapping...); err != nil { + return err + } } } } @@ -239,7 +241,9 @@ func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}) er switch structFieldValue.Kind() { // 属性为结构体 case reflect.Struct: - Struct(value, structFieldValue) + if err := Struct(value, structFieldValue); err != nil { + structFieldValue.Set(reflect.ValueOf(value)) + } // 属性为数组类型 case reflect.Slice: fallthrough @@ -253,11 +257,15 @@ func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}) er for i := 0; i < v.Len(); i++ { if t.Kind() == reflect.Ptr { e := reflect.New(t.Elem()).Elem() - Struct(v.Index(i).Interface(), e) + if err := Struct(v.Index(i).Interface(), e); err != nil { + e.Set(reflect.ValueOf(v.Index(i).Interface())) + } a.Index(i).Set(e.Addr()) } else { e := reflect.New(t).Elem() - Struct(v.Index(i).Interface(), e) + if err := Struct(v.Index(i).Interface(), e); err != nil { + e.Set(reflect.ValueOf(v.Index(i).Interface())) + } a.Index(i).Set(e) } } @@ -267,11 +275,15 @@ func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}) er t := a.Index(0).Type() if t.Kind() == reflect.Ptr { e := reflect.New(t.Elem()).Elem() - Struct(value, e) + if err := Struct(value, e); err != nil { + e.Set(reflect.ValueOf(value)) + } a.Index(0).Set(e.Addr()) } else { e := reflect.New(t).Elem() - Struct(value, e) + if err := Struct(value, e); err != nil { + e.Set(reflect.ValueOf(value)) + } a.Index(0).Set(e) } } @@ -280,7 +292,9 @@ func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}) er // 属性为指针类型 case reflect.Ptr: e := reflect.New(structFieldValue.Type().Elem()).Elem() - Struct(value, e) + if err := Struct(value, e); err != nil { + e.Set(reflect.ValueOf(value)) + } structFieldValue.Set(e.Addr()) default: diff --git a/g/util/gconv/gconv_z_unit_struct_test.go b/g/util/gconv/gconv_z_unit_struct_test.go index 35d4274f8..0efe7987f 100644 --- a/g/util/gconv/gconv_z_unit_struct_test.go +++ b/g/util/gconv/gconv_z_unit_struct_test.go @@ -7,12 +7,12 @@ package gconv_test import ( - "github.com/gogf/gf/g" - "github.com/gogf/gf/g/os/gtime" - "github.com/gogf/gf/g/test/gtest" - "github.com/gogf/gf/g/util/gconv" - "testing" - "time" + "github.com/gogf/gf/g" + "github.com/gogf/gf/g/os/gtime" + "github.com/gogf/gf/g/test/gtest" + "github.com/gogf/gf/g/util/gconv" + "testing" + "time" ) func Test_Struct_Basic1(t *testing.T) { @@ -104,6 +104,26 @@ func Test_Struct_Basic2(t *testing.T) { }) } +// 带有指针的基础类型属性 +func Test_Struct_Basic3(t *testing.T) { + gtest.Case(t, func() { + type User struct { + Uid int + Name *string + } + user := new(User) + params := g.Map { + "uid" : 1, + "Name" : "john", + } + if err := gconv.Struct(params, user); err != nil { + gtest.Error(err) + } + gtest.Assert(user.Uid, 1) + gtest.Assert(*user.Name, "john") + }) +} + // slice类型属性的赋值 func Test_Struct_Attr_Slice(t *testing.T) { gtest.Case(t, func() {