Skip to content

Commit

Permalink
Fix BuildEmptyTree for ordered maps (#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenovus authored Jul 24, 2023
1 parent 7c86e87 commit db25a8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ygot/struct_validation_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ func initialiseTree(t reflect.Type, v reflect.Value) {
fVal := v.Field(i)
fType := t.Field(i)

if util.IsTypeStructPtr(fType.Type) {
_, isOrderedMap := fVal.Interface().(GoOrderedMap)
if !isOrderedMap && util.IsTypeStructPtr(fType.Type) {
// Only initialise nested struct pointers, since all struct fields within
// a GoStruct are expected to be pointers, and we do not want to initialise
// non-struct values. If the struct pointer is not nil, it is skipped.
Expand Down
19 changes: 19 additions & 0 deletions ygot/struct_validation_map_exported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,25 @@ func TestEmitJSON(t *testing.T) {
}
}

func TestBuildEmptyTree(t *testing.T) {
tests := []struct {
name string
inStruct ygot.GoStruct
want ygot.GoStruct
}{{
name: "device containing ordered map",
inStruct: &ctestschema.Device{},
want: &ctestschema.Device{OtherData: &ctestschema.OtherData{}},
}}

for _, tt := range tests {
ygot.BuildEmptyTree(tt.inStruct)
if diff := cmp.Diff(tt.inStruct, tt.want); diff != "" {
t.Errorf("%s: did not get expected output, diff(-got,+want):\n%s", tt.name, diff)
}
}
}

func TestDeepCopyOrderedMap(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit db25a8c

Please sign in to comment.