Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions value/reflectcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ func (f *FieldCacheEntry) CanOmit(fieldVal reflect.Value) bool {
return f.isOmitEmpty && (safeIsNil(fieldVal) || isZero(fieldVal))
}

// GetUsing returns the field identified by this FieldCacheEntry from the provided struct.
// GetFrom returns the field identified by this FieldCacheEntry from the provided struct.
func (f *FieldCacheEntry) GetFrom(structVal reflect.Value) reflect.Value {
// field might be nested within 'inline' structs
for _, elem := range f.fieldPath {
structVal = structVal.FieldByIndex(elem)
structVal = dereference(structVal).FieldByIndex(elem)
}
return structVal
}
Expand Down Expand Up @@ -150,7 +150,11 @@ func buildStructCacheEntry(t reflect.Type, infos map[string]*FieldCacheEntry, fi
continue
}
if isInline {
buildStructCacheEntry(field.Type, infos, append(fieldPath, field.Index))
e := field.Type
if field.Type.Kind() == reflect.Ptr {
e = field.Type.Elem()
}
buildStructCacheEntry(e, infos, append(fieldPath, field.Index))
continue
}
info := &FieldCacheEntry{JsonName: jsonName, isOmitEmpty: isOmitempty, fieldPath: append(fieldPath, field.Index), fieldType: field.Type}
Expand Down
9 changes: 9 additions & 0 deletions value/valuereflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ type testOmitemptyStruct struct {
Noomit *string `json:"noomit"`
Omit *string `json:"omit,omitempty"`
}
type testEmbeddedStruct struct {
*testBasicStruct `json:",inline"`
}

func TestReflectStruct(t *testing.T) {
cases := []struct {
Expand Down Expand Up @@ -281,6 +284,12 @@ func TestReflectStruct(t *testing.T) {
expectedMap: map[string]interface{}{"noomit": (*string)(nil)},
expectedUnstructured: map[string]interface{}{"noomit": nil},
},
{
name: "embedded",
val: testEmbeddedStruct{&testBasicStruct{I: 10, S: "string"}},
expectedMap: map[string]interface{}{"int": int64(10), "S": "string"},
expectedUnstructured: map[string]interface{}{"int": int64(10), "S": "string"},
},
}

for _, tc := range cases {
Expand Down