Skip to content

Commit

Permalink
Fix leaf-list binary unmarshalling. (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenovus authored Jul 26, 2023
1 parent ca98dd5 commit 33d3dbd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
24 changes: 23 additions & 1 deletion ytypes/leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ func unmarshalLeaf(inSchema *yang.Entry, parent interface{}, value interface{},
if err != nil {
return err
}
if ykind == yang.Ybinary {
fieldIsSliceofSlice, err := isFieldSliceofSlice(parent, fieldName)
if err != nil {
return err
}
if ykind == yang.Ybinary && !fieldIsSliceofSlice {
// Binary is a slice field which is treated as a scalar.
return util.InsertIntoStruct(parent, fieldName, v)
}
Expand All @@ -397,6 +401,24 @@ func unmarshalLeaf(inSchema *yang.Entry, parent interface{}, value interface{},
return util.UpdateField(parent, fieldName, v)
}

func isFieldSliceofSlice(parentStruct interface{}, fieldName string) (bool, error) {
if util.IsValueNil(parentStruct) {
return false, fmt.Errorf("parent is nil in UpdateField for field %s", fieldName)
}

pt := reflect.TypeOf(parentStruct)

if !util.IsTypeStructPtr(pt) {
return false, fmt.Errorf("parent type %T must be a struct ptr", parentStruct)
}
ft, ok := pt.Elem().FieldByName(fieldName)
if !ok {
return false, fmt.Errorf("parent type %T does not have a field name %s", parentStruct, fieldName)
}

return ft.Type.Kind() == reflect.Slice && ft.Type.Elem().Kind() == reflect.Slice, nil
}

// unmarshalUnion unmarshals a union schema type with the given value into
// parent.
/*
Expand Down
23 changes: 23 additions & 0 deletions ytypes/leaf_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func TestValidateLeafList(t *testing.T) {

type LeafListContainer struct {
Int32LeafList []*int32 `path:"int32-leaf-list"`
BinaryLeafList []Binary `path:"binary-leaf-list"`
EnumLeafList []EnumType `path:"enum-leaf-list"`
UnionLeafSlice []UnionLeafType `path:"union-leaflist"`
UnionLeafSliceSimple []UnionLeafTypeSimple `path:"union-leaflist-simple"`
Expand Down Expand Up @@ -214,6 +215,14 @@ func TestUnmarshalLeafListGNMIEncoding(t *testing.T) {
Type: &yang.YangType{Kind: yang.Yint32},
}

binaryLeafListSchema := &yang.Entry{
Parent: containerSchema,
Name: "binary-leaf-list",
Kind: yang.LeafEntry,
ListAttr: yang.NewDefaultListAttr(),
Type: &yang.YangType{Kind: yang.Ybinary},
}

enumLeafListSchema := &yang.Entry{
Parent: containerSchema,
Name: "enum-leaf-list",
Expand Down Expand Up @@ -294,6 +303,20 @@ func TestUnmarshalLeafListGNMIEncoding(t *testing.T) {
}},
want: LeafListContainer{Int32LeafList: []*int32{ygot.Int32(-42), ygot.Int32(0), ygot.Int32(42)}},
},
{
desc: "binary success",
sch: binaryLeafListSchema,
val: &gpb.TypedValue{Value: &gpb.TypedValue_LeaflistVal{
LeaflistVal: &gpb.ScalarArray{
Element: []*gpb.TypedValue{
{Value: &gpb.TypedValue_BytesVal{BytesVal: []byte{0xa0, 0x00, 0x00, 0x00}}},
{Value: &gpb.TypedValue_BytesVal{BytesVal: []byte{0xb0, 0x00, 0x00, 0x00}}},
{Value: &gpb.TypedValue_BytesVal{BytesVal: []byte{0xc0, 0x00, 0x00, 0x00}}},
},
},
}},
want: LeafListContainer{BinaryLeafList: []Binary{Binary([]byte{0xa0, 0x00, 0x00, 0x00}), Binary([]byte{0xb0, 0x00, 0x00, 0x00}), Binary([]byte{0xc0, 0x00, 0x00, 0x00})}},
},
{
desc: "int32 success with existing values",
sch: int32LeafListSchema,
Expand Down

0 comments on commit 33d3dbd

Please sign in to comment.