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 SetRequest shadow path unmarshaling #65

Merged
merged 1 commit into from
Oct 7, 2022
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
6 changes: 3 additions & 3 deletions ygnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func set[T any](ctx context.Context, c *Client, q ConfigQuery[T], val T, op setO
if q.isLeaf() && q.isScalar() {
setVal = &val
}
if err := populateSetRequest(req, path, setVal, op); err != nil {
if err := populateSetRequest(req, path, setVal, op, !q.IsState()); err != nil {
return nil, nil, err
}

Expand All @@ -330,7 +330,7 @@ const (
)

// populateSetRequest fills a SetResponse for a val and operation type.
func populateSetRequest(req *gpb.SetRequest, path *gpb.Path, val interface{}, op setOperation) error {
func populateSetRequest(req *gpb.SetRequest, path *gpb.Path, val interface{}, op setOperation, preferShadowPath bool) error {
if req == nil {
return fmt.Errorf("cannot populate a nil SetRequest")
}
Expand All @@ -341,7 +341,7 @@ func populateSetRequest(req *gpb.SetRequest, path *gpb.Path, val interface{}, op
case replacePath, updatePath:
// Since the GoStructs are generated using preferOperationalState, we
// need to turn on preferShadowPath to prefer marshalling config paths.
js, err := ygot.Marshal7951(val, ygot.JSONIndent(" "), &ygot.RFC7951JSONConfig{AppendModuleName: true, PreferShadowPath: true})
js, err := ygot.Marshal7951(val, ygot.JSONIndent(" "), &ygot.RFC7951JSONConfig{AppendModuleName: true, PreferShadowPath: preferShadowPath})
if err != nil {
return fmt.Errorf("could not encode value into JSON format: %w", err)
}
Expand Down
28 changes: 16 additions & 12 deletions ygnmi/ygnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,10 @@ func Delete[T any](ctx context.Context, c *Client, q ConfigQuery[T]) (*Result, e
}

type batchOp struct {
path PathStruct
val interface{}
mode setOperation
path PathStruct
val interface{}
mode setOperation
config bool
}

// SetBatch allows multiple Set operations (Replace, Update, Delete) to be applied as part of a single Set transaction.
Expand All @@ -525,7 +526,7 @@ func (sb *SetBatch) Set(ctx context.Context, c *Client) (*Result, error) {
if err != nil {
return nil, err
}
if err := populateSetRequest(req, path, op.val, op.mode); err != nil {
if err := populateSetRequest(req, path, op.val, op.mode, op.config); err != nil {
return nil, err
}
}
Expand All @@ -545,9 +546,10 @@ func BatchUpdate[T any](sb *SetBatch, q ConfigQuery[T], val T) {
setVal = &val
}
sb.ops = append(sb.ops, &batchOp{
path: q.PathStruct(),
val: setVal,
mode: updatePath,
path: q.PathStruct(),
val: setVal,
mode: updatePath,
config: !q.IsState(),
})
}

Expand All @@ -558,17 +560,19 @@ func BatchReplace[T any](sb *SetBatch, q ConfigQuery[T], val T) {
setVal = &val
}
sb.ops = append(sb.ops, &batchOp{
path: q.PathStruct(),
val: setVal,
mode: replacePath,
path: q.PathStruct(),
val: setVal,
mode: replacePath,
config: !q.IsState(),
})
}

// BatchDelete stores an update operation in the SetBatch.
func BatchDelete[T any](sb *SetBatch, q ConfigQuery[T]) {
sb.ops = append(sb.ops, &batchOp{
path: q.PathStruct(),
mode: deletePath,
path: q.PathStruct(),
mode: deletePath,
config: !q.IsState(),
})
}

Expand Down