Skip to content
Open
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
4 changes: 4 additions & 0 deletions value/reflectcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (f *FieldCacheEntry) CanOmit(fieldVal reflect.Value) bool {
func (f *FieldCacheEntry) GetFrom(structVal reflect.Value) reflect.Value {
// field might be nested within 'inline' structs
for _, elem := range f.fieldPath {
if safeIsNil(structVal) {
// if any part of the path is nil, return the zero value for the field type
return reflect.Zero(f.fieldType)
}
structVal = dereference(structVal).FieldByIndex(elem)
}
return structVal
Expand Down
6 changes: 6 additions & 0 deletions value/valuereflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ func TestReflectStruct(t *testing.T) {
expectedMap: map[string]interface{}{"int": int64(10), "S": "string"},
expectedUnstructured: map[string]interface{}{"int": int64(10), "S": "string"},
},
{
name: "embeddedIsNil",
val: testEmbeddedStruct{},
expectedMap: map[string]interface{}{"int": int64(0), "S": ""},
expectedUnstructured: map[string]interface{}{"int": int64(0), "S": ""},
},
}

for _, tc := range cases {
Expand Down