Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leaf-list binary unmarshalling #900

Merged
merged 1 commit into from
Jul 26, 2023
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
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
Loading