mark gconv.StructDeep/StructsDeep deprecated

This commit is contained in:
John 2020-11-05 23:02:29 +08:00
parent 176dcdc7cc
commit 9f04e46166
15 changed files with 74 additions and 186 deletions

View File

@ -20,6 +20,7 @@ func (v *Var) Struct(pointer interface{}, mapping ...map[string]string) error {
// Struct maps value of <v> to <pointer> recursively.
// The parameter <pointer> should be a pointer to a struct instance.
// The parameter <mapping> is used to specify the key-to-attribute mapping rules.
// Deprecated, use Struct instead.
func (v *Var) StructDeep(pointer interface{}, mapping ...map[string]string) error {
return gconv.StructDeep(v.Val(), pointer, mapping...)
}
@ -30,6 +31,7 @@ func (v *Var) Structs(pointer interface{}, mapping ...map[string]string) error {
}
// StructsDeep converts and returns <v> as given struct slice recursively.
// Deprecated, use Struct instead.
func (v *Var) StructsDeep(pointer interface{}, mapping ...map[string]string) error {
return gconv.StructsDeep(v.Val(), pointer, mapping...)
}

View File

@ -726,5 +726,5 @@ func mapToStruct(data map[string]interface{}, pointer interface{}) error {
for tag, attr := range structs.TagMapName(pointer, []string{ORM_TAG_FOR_STRUCT}) {
mapping[strings.Split(tag, ",")[0]] = attr
}
return gconv.StructDeep(data, pointer, mapping)
return gconv.Struct(data, pointer, mapping)
}

View File

@ -315,6 +315,7 @@ func (j *Json) GetStruct(pattern string, pointer interface{}, mapping ...map[str
}
// GetStructDeep does GetStruct recursively.
// Deprecated, use GetStruct instead.
func (j *Json) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.StructDeep(j.Get(pattern), pointer, mapping...)
}
@ -325,6 +326,7 @@ func (j *Json) GetStructs(pattern string, pointer interface{}, mapping ...map[st
}
// GetStructsDeep converts any slice to given struct slice recursively.
// Deprecated, use GetStructs instead.
func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.StructsDeep(j.Get(pattern), pointer, mapping...)
}
@ -394,6 +396,7 @@ func (j *Json) ToStruct(pointer interface{}, mapping ...map[string]string) error
// ToStructDeep converts current Json object to specified object recursively.
// The <pointer> should be a pointer type of *struct.
// Deprecated, use ToStruct instead.
func (j *Json) ToStructDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()

View File

@ -203,7 +203,7 @@ func Test_ToStruct1(t *testing.T) {
})
}
func Test_ToStructDeep(t *testing.T) {
func Test_ToStruct(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Item struct {
Title string `json:"title"`
@ -231,7 +231,7 @@ func Test_ToStructDeep(t *testing.T) {
t.Assert(j.GetBool("items"), false)
t.Assert(j.GetArray("items"), nil)
m := new(M)
err = j.ToStructDeep(m)
err = j.ToStruct(m)
t.Assert(err, nil)
t.AssertNE(m.Me, nil)
t.Assert(m.Me["day"], "20009")

View File

@ -299,6 +299,7 @@ func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[s
}
// GetStructDeep does GetStruct recursively.
// Deprecated, use GetStruct instead.
func (c *Config) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStructDeep(pattern, pointer, mapping...)
@ -315,6 +316,7 @@ func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[
}
// GetStructsDeep converts any slice to given struct slice recursively.
// Deprecated, use GetStructs instead.
func (c *Config) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStructsDeep(pattern, pointer, mapping...)

View File

@ -355,6 +355,7 @@ func (s *Session) GetStruct(key string, pointer interface{}, mapping ...map[stri
return gconv.Struct(s.Get(key), pointer, mapping...)
}
// Deprecated, use GetStruct instead.
func (s *Session) GetStructDeep(key string, pointer interface{}, mapping ...map[string]string) error {
return gconv.StructDeep(s.Get(key), pointer, mapping...)
}
@ -363,6 +364,7 @@ func (s *Session) GetStructs(key string, pointer interface{}, mapping ...map[str
return gconv.Structs(s.Get(key), pointer, mapping...)
}
// Deprecated, use GetStructs instead.
func (s *Session) GetStructsDeep(key string, pointer interface{}, mapping ...map[string]string) error {
return gconv.StructsDeep(s.Get(key), pointer, mapping...)
}

View File

@ -390,14 +390,14 @@ func MapStrStrDeep(value interface{}, tags ...string) map[string]string {
// using reflect.
// See doMapToMap.
func MapToMap(params interface{}, pointer interface{}, mapping ...map[string]string) error {
return doMapToMap(params, pointer, false, mapping...)
return doMapToMap(params, pointer, mapping...)
}
// MapToMapDeep converts any map type variable <params> to another map type variable <pointer>
// using reflect recursively.
// See doMapToMap.
// Deprecated, use MapToMap instead.
func MapToMapDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error {
return doMapToMap(params, pointer, true, mapping...)
return doMapToMap(params, pointer, mapping...)
}
// doMapToMap converts any map type variable <params> to another map type variable <pointer>.
@ -410,7 +410,7 @@ func MapToMapDeep(params interface{}, pointer interface{}, mapping ...map[string
//
// The optional parameter <mapping> is used for struct attribute to map key mapping, which makes
// sense only if the items of original map <params> is type struct.
func doMapToMap(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) (err error) {
func doMapToMap(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
var (
paramsRv = reflect.ValueOf(params)
paramsKind = paramsRv.Kind()
@ -461,14 +461,8 @@ func doMapToMap(params interface{}, pointer interface{}, deep bool, mapping ...m
e := reflect.New(pointerValueType).Elem()
switch pointerValueKind {
case reflect.Map, reflect.Struct:
if deep {
if err = StructDeep(paramsRv.MapIndex(key).Interface(), e, mapping...); err != nil {
return err
}
} else {
if err = Struct(paramsRv.MapIndex(key).Interface(), e, mapping...); err != nil {
return err
}
if err = Struct(paramsRv.MapIndex(key).Interface(), e, mapping...); err != nil {
return err
}
default:
e.Set(
@ -497,14 +491,14 @@ func doMapToMap(params interface{}, pointer interface{}, deep bool, mapping ...m
// MapToMaps converts any map type variable <params> to another map type variable <pointer>.
// See doMapToMaps.
func MapToMaps(params interface{}, pointer interface{}, mapping ...map[string]string) error {
return doMapToMaps(params, pointer, false, mapping...)
return doMapToMaps(params, pointer, mapping...)
}
// MapToMapsDeep converts any map type variable <params> to another map type variable
// <pointer> recursively.
// See doMapToMaps.
// Deprecated, use MapToMaps instead.
func MapToMapsDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error {
return doMapToMaps(params, pointer, true, mapping...)
return doMapToMaps(params, pointer, mapping...)
}
// doMapToMaps converts any map type variable <params> to another map type variable <pointer>.
@ -519,7 +513,7 @@ func MapToMapsDeep(params interface{}, pointer interface{}, mapping ...map[strin
// sense only if the items of original map is type struct.
//
// TODO it's supposed supporting target type <pointer> like: map[int][]map, map[string][]map.
func doMapToMaps(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) (err error) {
func doMapToMaps(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
var (
paramsRv = reflect.ValueOf(params)
paramsKind = paramsRv.Kind()
@ -560,14 +554,8 @@ func doMapToMaps(params interface{}, pointer interface{}, deep bool, mapping ...
)
for _, key := range paramsKeys {
e := reflect.New(pointerValueType).Elem()
if deep {
if err = StructsDeep(paramsRv.MapIndex(key).Interface(), e.Addr(), mapping...); err != nil {
return err
}
} else {
if err = Structs(paramsRv.MapIndex(key).Interface(), e.Addr(), mapping...); err != nil {
return err
}
if err = Structs(paramsRv.MapIndex(key).Interface(), e.Addr(), mapping...); err != nil {
return err
}
dataMap.SetMapIndex(
reflect.ValueOf(

View File

@ -33,6 +33,7 @@ func Scan(params interface{}, pointer interface{}, mapping ...map[string]string)
// parameter <pointer> to implement the converting..
// It calls function StructDeep if <pointer> is type of *struct/**struct to do the converting.
// It calls function StructsDeep if <pointer> is type of *[]struct/*[]*struct to do the converting.
// Deprecated, use Scan instead.
func ScanDeep(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
t := reflect.TypeOf(pointer)
k := t.Kind()

View File

@ -26,6 +26,7 @@ func SliceStruct(params interface{}, pointer interface{}, mapping ...map[string]
}
// SliceStructDeep is alias of StructsDeep.
// Deprecated, use SliceStruct instead.
func SliceStructDeep(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
return StructsDeep(params, pointer, mapping...)
}

View File

@ -39,17 +39,17 @@ var (
// in mapping procedure to do the matching.
// It ignores the map key, if it does not match.
func Struct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
return doStruct(params, pointer, false, mapping...)
return doStruct(params, pointer, mapping...)
}
// StructDeep do Struct function recursively.
// See Struct.
// Deprecated, use Struct instead.
func StructDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error {
return doStruct(params, pointer, true, mapping...)
return doStruct(params, pointer, mapping...)
}
// doStruct is the core internal converting function for any data to struct recursively or not.
func doStruct(params interface{}, pointer interface{}, recursive bool, mapping ...map[string]string) (err error) {
// doStruct is the core internal converting function for any data to struct.
func doStruct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
if params == nil {
// If <params> is nil, no conversion.
return nil
@ -156,7 +156,7 @@ func doStruct(params interface{}, pointer interface{}, recursive bool, mapping .
// mapV is the the attribute name of the struct.
if paramV, ok := paramsMap[mapK]; ok {
doneMap[mapV] = struct{}{}
if err := bindVarToStructAttr(elem, mapV, paramV, recursive, mapping...); err != nil {
if err := bindVarToStructAttr(elem, mapV, paramV, mapping...); err != nil {
return err
}
}
@ -179,7 +179,7 @@ func doStruct(params interface{}, pointer interface{}, recursive bool, mapping .
continue
}
// Maybe it's struct/*struct.
if recursive && elemFieldType.Anonymous {
if elemFieldType.Anonymous {
elemFieldValue = elem.Field(i)
// Ignore the interface attribute if it's nil.
if elemFieldValue.Kind() == reflect.Interface {
@ -188,7 +188,7 @@ func doStruct(params interface{}, pointer interface{}, recursive bool, mapping .
continue
}
}
if err = doStruct(paramsMap, elemFieldValue, recursive, mapping...); err != nil {
if err = doStruct(paramsMap, elemFieldValue, mapping...); err != nil {
return err
}
} else {
@ -251,30 +251,15 @@ func doStruct(params interface{}, pointer interface{}, recursive bool, mapping .
}
// Mark it done.
doneMap[attrName] = struct{}{}
if err := bindVarToStructAttr(elem, attrName, mapV, recursive, mapping...); err != nil {
if err := bindVarToStructAttr(elem, attrName, mapV, mapping...); err != nil {
return err
}
}
// Recursively concerting for struct attributes with the same params map.
if recursive && elem.Kind() == reflect.Struct {
for i := 0; i < elemType.NumField(); i++ {
// Only do converting to public attributes.
if !utils.IsLetterUpper(elemType.Field(i).Name[0]) {
continue
}
fieldValue := elem.Field(i)
if fieldValue.Kind() == reflect.Struct {
if err := doStruct(paramsMap, fieldValue, recursive, mapping...); err != nil {
return err
}
}
}
}
return nil
}
// bindVarToStructAttr sets value to struct object attribute by name.
func bindVarToStructAttr(elem reflect.Value, name string, value interface{}, recursive bool, mapping ...map[string]string) (err error) {
func bindVarToStructAttr(elem reflect.Value, name string, value interface{}, mapping ...map[string]string) (err error) {
structFieldValue := elem.FieldByName(name)
if !structFieldValue.IsValid() {
return nil
@ -285,7 +270,7 @@ func bindVarToStructAttr(elem reflect.Value, name string, value interface{}, rec
}
defer func() {
if e := recover(); e != nil {
err = bindVarToReflectValue(structFieldValue, value, recursive, mapping...)
err = bindVarToReflectValue(structFieldValue, value, mapping...)
if err != nil {
err = gerror.Wrapf(err, `error binding value to attribute "%s"`, name)
}
@ -300,7 +285,7 @@ func bindVarToStructAttr(elem reflect.Value, name string, value interface{}, rec
}
// bindVarToReflectValue sets <value> to reflect value object <structFieldValue>.
func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}, recursive bool, mapping ...map[string]string) (err error) {
func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}, mapping ...map[string]string) (err error) {
kind := structFieldValue.Kind()
// Converting using interface, for some kinds.
@ -327,7 +312,7 @@ func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}, re
}
// Recursively converting for struct attribute.
if err := doStruct(value, structFieldValue, recursive); err != nil {
if err := doStruct(value, structFieldValue); err != nil {
// Note there's reflect conversion mechanism here.
structFieldValue.Set(reflect.ValueOf(value).Convert(structFieldValue.Type()))
}
@ -344,14 +329,14 @@ func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}, re
for i := 0; i < v.Len(); i++ {
if t.Kind() == reflect.Ptr {
e := reflect.New(t.Elem()).Elem()
if err := doStruct(v.Index(i).Interface(), e, recursive); err != nil {
if err := doStruct(v.Index(i).Interface(), e); err != nil {
// Note there's reflect conversion mechanism here.
e.Set(reflect.ValueOf(v.Index(i).Interface()).Convert(t))
}
a.Index(i).Set(e.Addr())
} else {
e := reflect.New(t).Elem()
if err := doStruct(v.Index(i).Interface(), e, recursive); err != nil {
if err := doStruct(v.Index(i).Interface(), e); err != nil {
// Note there's reflect conversion mechanism here.
e.Set(reflect.ValueOf(v.Index(i).Interface()).Convert(t))
}
@ -364,14 +349,14 @@ func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}, re
t := a.Index(0).Type()
if t.Kind() == reflect.Ptr {
e := reflect.New(t.Elem()).Elem()
if err := doStruct(value, e, recursive); err != nil {
if err := doStruct(value, e); err != nil {
// Note there's reflect conversion mechanism here.
e.Set(reflect.ValueOf(value).Convert(t))
}
a.Index(0).Set(e.Addr())
} else {
e := reflect.New(t).Elem()
if err := doStruct(value, e, recursive); err != nil {
if err := doStruct(value, e); err != nil {
// Note there's reflect conversion mechanism here.
e.Set(reflect.ValueOf(value).Convert(t))
}
@ -390,7 +375,7 @@ func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}, re
return err
}
elem := item.Elem()
if err = bindVarToReflectValue(elem, value, recursive, mapping...); err == nil {
if err = bindVarToReflectValue(elem, value, mapping...); err == nil {
structFieldValue.Set(elem.Addr())
}

View File

@ -14,12 +14,13 @@ import (
// Structs converts any slice to given struct slice.
func Structs(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
return doStructs(params, pointer, false, mapping...)
return doStructs(params, pointer, mapping...)
}
// StructsDeep converts any slice to given struct slice recursively.
// Deprecated, use Structs instead.
func StructsDeep(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
return doStructs(params, pointer, true, mapping...)
return doStructs(params, pointer, mapping...)
}
// doStructs converts any slice to given struct slice.
@ -29,7 +30,7 @@ func StructsDeep(params interface{}, pointer interface{}, mapping ...map[string]
// The parameter <pointer> should be type of pointer to slice of struct.
// Note that if <pointer> is a pointer to another pointer of type of slice of struct,
// it will create the struct/pointer internally.
func doStructs(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) (err error) {
func doStructs(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
if params == nil {
// If <params> is nil, no conversion.
return nil
@ -88,27 +89,15 @@ func doStructs(params interface{}, pointer interface{}, deep bool, mapping ...ma
if itemType.Kind() == reflect.Ptr {
// Slice element is type pointer.
e := reflect.New(itemType.Elem()).Elem()
if deep {
if err = StructDeep(paramsMaps[i], e, mapping...); err != nil {
return err
}
} else {
if err = Struct(paramsMaps[i], e, mapping...); err != nil {
return err
}
if err = Struct(paramsMaps[i], e, mapping...); err != nil {
return err
}
array.Index(i).Set(e.Addr())
} else {
// Slice element is not type of pointer.
e := reflect.New(itemType).Elem()
if deep {
if err = StructDeep(paramsMaps[i], e, mapping...); err != nil {
return err
}
} else {
if err = Struct(paramsMaps[i], e, mapping...); err != nil {
return err
}
if err = Struct(paramsMaps[i], e, mapping...); err != nil {
return err
}
array.Index(i).Set(e)
}

View File

@ -1278,7 +1278,7 @@ func Test_Struct_PrivateAttribute_All(t *testing.T) {
})
}
func Test_Struct_Deep_All(t *testing.T) {
func Test_Struct_Embedded_All(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Ids struct {
Id int `json:"id"`
@ -1303,7 +1303,7 @@ func Test_Struct_Deep_All(t *testing.T) {
"create_time": "2019",
}
user := new(User)
gconv.StructDeep(data, user)
gconv.Struct(data, user)
t.Assert(user.Id, 100)
t.Assert(user.Uid, 101)
t.Assert(user.Nickname, "T1")

View File

@ -138,14 +138,6 @@ func Test_MapToMapDeep(t *testing.T) {
err := gconv.MapToMap(params, &m)
t.Assert(err, nil)
t.Assert(len(m), 1)
t.Assert(m["key"].Id, 0)
t.Assert(m["key"].Name, "john")
})
gtest.C(t, func(t *gtest.T) {
m := (map[string]*User)(nil)
err := gconv.MapToMapDeep(params, &m)
t.Assert(err, nil)
t.Assert(len(m), 1)
t.Assert(m["key"].Id, 1)
t.Assert(m["key"].Name, "john")
})
@ -255,7 +247,7 @@ func Test_MapToMaps2(t *testing.T) {
})
}
func Test_MapToMapsDeep(t *testing.T) {
func Test_MapToMaps3(t *testing.T) {
type Ids struct {
Id int
Uid int
@ -283,20 +275,6 @@ func Test_MapToMapsDeep(t *testing.T) {
err := gconv.MapToMaps(params, &m)
t.Assert(err, nil)
t.Assert(len(m), 2)
t.Assert(m["100"][0].Id, 0)
t.Assert(m["100"][1].Id, 0)
t.Assert(m["100"][0].Name, "john")
t.Assert(m["100"][1].Name, "smith")
t.Assert(m["200"][0].Id, 0)
t.Assert(m["200"][1].Id, 0)
t.Assert(m["200"][0].Name, "green")
t.Assert(m["200"][1].Name, "jim")
})
gtest.C(t, func(t *gtest.T) {
m := make(map[string][]*User)
err := gconv.MapToMapsDeep(params, &m)
t.Assert(err, nil)
t.Assert(len(m), 2)
t.Assert(m["100"][0].Id, 1)
t.Assert(m["100"][1].Id, 2)
t.Assert(m["100"][0].Name, "john")
@ -308,7 +286,7 @@ func Test_MapToMapsDeep(t *testing.T) {
})
}
func Test_MapToMapsDeepWithTag(t *testing.T) {
func Test_MapToMapsWithTag(t *testing.T) {
type Ids struct {
Id int
Uid int
@ -336,20 +314,6 @@ func Test_MapToMapsDeepWithTag(t *testing.T) {
err := gconv.MapToMaps(params, &m)
t.Assert(err, nil)
t.Assert(len(m), 2)
t.Assert(m["100"][0].Id, 0)
t.Assert(m["100"][1].Id, 0)
t.Assert(m["100"][0].Name, "john")
t.Assert(m["100"][1].Name, "smith")
t.Assert(m["200"][0].Id, 0)
t.Assert(m["200"][1].Id, 0)
t.Assert(m["200"][0].Name, "green")
t.Assert(m["200"][1].Name, "jim")
})
gtest.C(t, func(t *gtest.T) {
m := make(map[string][]*User)
err := gconv.MapToMapsDeep(params, &m)
t.Assert(err, nil)
t.Assert(len(m), 2)
t.Assert(m["100"][0].Id, 1)
t.Assert(m["100"][1].Id, 2)
t.Assert(m["100"][0].Name, "john")

View File

@ -68,28 +68,10 @@ func Test_Slice_Structs(t *testing.T) {
t.Assert(len(users), 2)
t.Assert(users[0].Id, params[0]["id"])
t.Assert(users[0].Name, params[0]["name"])
t.Assert(users[0].Age, 0)
t.Assert(users[0].Age, 18)
t.Assert(users[1].Id, params[1]["id"])
t.Assert(users[1].Name, params[1]["name"])
t.Assert(users[1].Age, 0)
})
gtest.C(t, func(t *gtest.T) {
users := make([]User, 0)
params := []g.Map{
{"id": 1, "name": "john", "age": 18},
{"id": 2, "name": "smith", "age": 20},
}
err := gconv.StructsDeep(params, &users)
t.Assert(err, nil)
t.Assert(len(users), 2)
t.Assert(users[0].Id, params[0]["id"])
t.Assert(users[0].Name, params[0]["name"])
t.Assert(users[0].Age, params[0]["age"])
t.Assert(users[1].Id, params[1]["id"])
t.Assert(users[1].Name, params[1]["name"])
t.Assert(users[1].Age, params[1]["age"])
t.Assert(users[1].Age, 20)
})
}

View File

@ -374,7 +374,7 @@ func Test_Struct_PrivateAttribute(t *testing.T) {
})
}
func Test_StructDeep1(t *testing.T) {
func Test_StructEmbedded1(t *testing.T) {
type Base struct {
Age int
}
@ -394,25 +394,11 @@ func Test_StructDeep1(t *testing.T) {
t.Assert(err, nil)
t.Assert(user.Id, params["id"])
t.Assert(user.Name, params["name"])
t.Assert(user.Age, 0)
})
gtest.C(t, func(t *gtest.T) {
user := new(User)
params := g.Map{
"id": 1,
"name": "john",
"age": 18,
}
err := gconv.StructDeep(params, user)
t.Assert(err, nil)
t.Assert(user.Id, params["id"])
t.Assert(user.Name, params["name"])
t.Assert(user.Age, params["age"])
t.Assert(user.Age, 18)
})
}
func Test_StructDeep2(t *testing.T) {
func Test_StructEmbedded2(t *testing.T) {
type Ids struct {
Id int
Uid int
@ -434,30 +420,13 @@ func Test_StructDeep2(t *testing.T) {
user := new(User)
err := gconv.Struct(params, user)
t.Assert(err, nil)
t.Assert(user.Id, 0)
t.Assert(user.Uid, 0)
t.Assert(user.Name, "john")
})
gtest.C(t, func(t *gtest.T) {
user := new(User)
err := gconv.StructDeep(params, user)
t.Assert(err, nil)
t.Assert(user.Id, 1)
t.Assert(user.Uid, 10)
t.Assert(user.Name, "john")
})
gtest.C(t, func(t *gtest.T) {
user := (*User)(nil)
err := gconv.StructDeep(params, &user)
t.Assert(err, nil)
t.Assert(user.Id, 1)
t.Assert(user.Uid, 10)
t.Assert(user.Name, "john")
})
}
func Test_StructDeep3(t *testing.T) {
func Test_StructEmbedded3(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Ids struct {
Id int `json:"id"`
@ -482,7 +451,7 @@ func Test_StructDeep3(t *testing.T) {
"create_time": "2019",
}
user := new(User)
err := gconv.StructDeep(data, user)
err := gconv.Struct(data, user)
t.Assert(err, nil)
t.Assert(user.Id, 100)
t.Assert(user.Uid, 101)
@ -492,7 +461,7 @@ func Test_StructDeep3(t *testing.T) {
}
// https://github.com/gogf/gf/issues/775
func Test_StructDeep4(t *testing.T) {
func Test_StructEmbedded4(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Sub2 struct {
SubName string
@ -521,14 +490,14 @@ func Test_StructDeep4(t *testing.T) {
},
}
tx := new(Test)
if err := gconv.StructDeep(data, &tx); err != nil {
if err := gconv.Struct(data, &tx); err != nil {
panic(err)
}
t.Assert(tx, expect)
})
}
func Test_StructDeep5(t *testing.T) {
func Test_StructEmbedded5(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Base struct {
Pass1 string `params:"password1"`
@ -554,13 +523,13 @@ func Test_StructDeep5(t *testing.T) {
var err error
user1 := new(UserWithBase1)
user2 := new(UserWithBase2)
err = gconv.StructDeep(data, user1)
err = gconv.Struct(data, user1)
t.Assert(err, nil)
t.Assert(user1, &UserWithBase1{1, "john", Base{"123", "456"}})
err = gconv.StructDeep(data, user2)
err = gconv.Struct(data, user2)
t.Assert(err, nil)
t.Assert(user2, &UserWithBase2{1, "john", Base{"123", "456"}})
t.Assert(user2, &UserWithBase2{1, "john", Base{"", ""}})
})
}
@ -867,7 +836,7 @@ func Test_Struct_CatchPanic(t *testing.T) {
Result int
}
type User struct {
Score
Score Score
}
user := new(User)
@ -927,14 +896,14 @@ type TestStruct struct {
TestInterface
}
func Test_Struct_WithInterfaceAttr(t *testing.T) {
func Test_Struct_Embedded(t *testing.T) {
// Implemented interface attribute.
gtest.C(t, func(t *gtest.T) {
v1 := TestStruct{
TestInterface: &T{"john"},
}
v2 := g.Map{}
err := gconv.StructDeep(v2, &v1)
err := gconv.Struct(v2, &v1)
t.Assert(err, nil)
t.Assert(v1.Test(), "john")
})
@ -946,7 +915,7 @@ func Test_Struct_WithInterfaceAttr(t *testing.T) {
v2 := g.Map{
"name": "test",
}
err := gconv.StructDeep(v2, &v1)
err := gconv.Struct(v2, &v1)
t.Assert(err, nil)
t.Assert(v1.Test(), "test")
})
@ -956,7 +925,7 @@ func Test_Struct_WithInterfaceAttr(t *testing.T) {
v2 := g.Map{
"name": "test",
}
err := gconv.StructDeep(v2, &v1)
err := gconv.Struct(v2, &v1)
t.Assert(err, nil)
t.Assert(v1.TestInterface, nil)
})