Skip to content

Commit db25a8c

Browse files
authored
Fix BuildEmptyTree for ordered maps (#893)
1 parent 7c86e87 commit db25a8c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

ygot/struct_validation_map.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ func initialiseTree(t reflect.Type, v reflect.Value) {
212212
fVal := v.Field(i)
213213
fType := t.Field(i)
214214

215-
if util.IsTypeStructPtr(fType.Type) {
215+
_, isOrderedMap := fVal.Interface().(GoOrderedMap)
216+
if !isOrderedMap && util.IsTypeStructPtr(fType.Type) {
216217
// Only initialise nested struct pointers, since all struct fields within
217218
// a GoStruct are expected to be pointers, and we do not want to initialise
218219
// non-struct values. If the struct pointer is not nil, it is skipped.

ygot/struct_validation_map_exported_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,25 @@ func TestEmitJSON(t *testing.T) {
308308
}
309309
}
310310

311+
func TestBuildEmptyTree(t *testing.T) {
312+
tests := []struct {
313+
name string
314+
inStruct ygot.GoStruct
315+
want ygot.GoStruct
316+
}{{
317+
name: "device containing ordered map",
318+
inStruct: &ctestschema.Device{},
319+
want: &ctestschema.Device{OtherData: &ctestschema.OtherData{}},
320+
}}
321+
322+
for _, tt := range tests {
323+
ygot.BuildEmptyTree(tt.inStruct)
324+
if diff := cmp.Diff(tt.inStruct, tt.want); diff != "" {
325+
t.Errorf("%s: did not get expected output, diff(-got,+want):\n%s", tt.name, diff)
326+
}
327+
}
328+
}
329+
311330
func TestDeepCopyOrderedMap(t *testing.T) {
312331
tests := []struct {
313332
name string

0 commit comments

Comments
 (0)