Skip to content

Commit

Permalink
Fix SetRequest shadow path unmarshaling (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanG100 authored Oct 7, 2022
1 parent fcd271e commit 8d22029
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
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

0 comments on commit 8d22029

Please sign in to comment.