Merge pull request #70 from 17media/ptr-struct

Nest into struct for slice of pointer of struct
This commit is contained in:
Fatih Arslan 2016-08-07 19:54:23 -04:00 committed by GitHub
commit bf7dff7189
2 changed files with 32 additions and 1 deletions

View File

@ -558,7 +558,9 @@ func (s *Struct) nested(val reflect.Value) interface{} {
// TODO(arslan): should this be optional?
// do not iterate of non struct types, just pass the value. Ie: []int,
// []string, co... We only iterate further if it's a struct.
if val.Type().Elem().Kind() != reflect.Struct {
if val.Type().Elem().Kind() != reflect.Struct &&
!(val.Type().Elem().Kind() == reflect.Ptr &&
val.Type().Elem().Elem().Kind() == reflect.Struct) {
finalVal = val.Interface()
break
}

View File

@ -479,6 +479,35 @@ func TestMap_NestedSliceWithStructValues(t *testing.T) {
}
}
func TestMap_NestedSliceWithPointerOfStructValues(t *testing.T) {
type address struct {
Country string `structs:"customCountryName"`
}
type person struct {
Name string `structs:"name"`
Addresses []*address `structs:"addresses"`
}
p := person{
Name: "test",
Addresses: []*address{
&address{Country: "England"},
&address{Country: "Italy"},
},
}
mp := Map(p)
mpAddresses := mp["addresses"].([]interface{})
if _, exists := mpAddresses[0].(map[string]interface{})["Country"]; exists {
t.Errorf("Expecting customCountryName, but found Country")
}
if _, exists := mpAddresses[0].(map[string]interface{})["customCountryName"]; !exists {
t.Errorf("customCountryName key not found")
}
}
func TestMap_NestedSliceWithIntValues(t *testing.T) {
type person struct {
Name string `structs:"name"`