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 unmarshal of empty type leaf value for gNMI encoding #292

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 2 deletions ytypes/leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ func sanitizeGNMI(parent interface{}, schema *yang.Entry, fieldName string, tv *
}

switch ykind {
case yang.Ybool:
case yang.Ybool, yang.Yempty:
return tv.GetBoolVal(), nil
case yang.Ystring:
return tv.GetStringVal(), nil
Expand Down Expand Up @@ -902,7 +902,7 @@ func sanitizeGNMI(parent interface{}, schema *yang.Entry, fieldName string, tv *
func gNMIToYANGTypeMatches(ykind yang.TypeKind, tv *gpb.TypedValue) bool {
var ok bool
switch ykind {
case yang.Ybool:
case yang.Ybool, yang.Yempty:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that we really need to make yang.Yempty always set the field to true if we receive this. Since YANGEmpty isn't a pointer in the generated code, then if we receive an empty field that has a bool value of false then this won't be something that the client application can tell was received. Could you add this handling as a special case please?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually there already appears to be handling for this case:

ygot/util/reflect.go

Lines 256 to 262 in f01913a

// YANG empty fields are represented as a derived bool value defined in the
// generated code. Here we cast the value to the type in the generated code.
if ft.Type.Kind() == reflect.Bool && t.Kind() == reflect.Bool {
nv := reflect.New(ft.Type).Elem()
nv.SetBool(v.Bool())
v = nv
}

When unmarshalling an empty leaf from a TypedValue in the current code, unmarshalScalar returns the non-pointer bool value, which unmarshalLeaf then takes and then feeds into InsertIntoStruct (directly or indirectly) to reach this special handling code.

This change doesn't support unmarshalling empty leafs (either JSON or gNMI) into a union type. However given its complexity I think it deserves to be addressed separately.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explored the question of unions containing empty in #582.

_, ok = tv.GetValue().(*gpb.TypedValue_BoolVal)
case yang.Ystring, yang.Yenum, yang.Yidentityref:
_, ok = tv.GetValue().(*gpb.TypedValue_StringVal)
Expand Down
10 changes: 10 additions & 0 deletions ytypes/leaf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,16 @@ func TestUnmarshalLeafGNMIEncoding(t *testing.T) {
},
wantVal: &LeafContainerStruct{BoolLeaf: ygot.Bool(true)},
},
{
desc: "success gNMI BoolVal to Yempty",
inSchema: typeToLeafSchema("empty-leaf", yang.Yempty),
inVal: &gpb.TypedValue{
Value: &gpb.TypedValue_BoolVal{
BoolVal: true,
},
},
wantVal: &LeafContainerStruct{EmptyLeaf: YANGEmpty(true)},
},
{
desc: "success gNMI StringVal to Ystring",
inSchema: typeToLeafSchema("string-leaf", yang.Ystring),
Expand Down