Skip to content

Commit

Permalink
Allow DeleteNode on root element if it is settable/error out otherwise (
Browse files Browse the repository at this point in the history
#904)

* Allow DeleteNode on root element if it is settable/error out otherwise

* Copying Daniel's code from https://github.com/openconfig/ygot/pull/649/files
  • Loading branch information
wenovus authored Aug 1, 2023
1 parent e0445ce commit df0faa5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ytypes/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ func retrieveNode(schema *yang.Entry, root interface{}, path, traversedPath *gpb
return nil, status.Errorf(codes.Unknown, "path %v points to a node with non-leaf schema %v", traversedPath, schema)
}
}
if args.delete {
if util.IsValueNil(root) {
return nil, nil
}
if rt, rv := reflect.TypeOf(root), reflect.ValueOf(root); rt.Kind() == reflect.Pointer && rv.Elem().CanSet() {
rv.Elem().Set(reflect.Zero(rv.Elem().Type()))
} else {
return nil, fmt.Errorf("cannot delete on unsettable element: (%T, %v)", root, root)
}
}
return []*TreeNode{{
Path: traversedPath,
Schema: schema,
Expand Down
23 changes: 23 additions & 0 deletions ytypes/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2740,6 +2740,29 @@ func TestDeleteNode(t *testing.T) {
inRoot: &ListElemStruct1{Key1: ygot.String("hello"), Outer: &OuterContainerType1{Inner: &InnerContainerType1{Int32LeafListName: []int32{42, 43, 44}, Int32LeafName: ygot.Int32(5)}}},
inPath: mustPath("/outer"),
want: &ListElemStruct1{Key1: ygot.String("hello")},
}, {
name: "deleting on root struct element",
inSchema: simpleSchema(),
inRoot: &ListElemStruct1{Key1: ygot.String("hello"), Outer: &OuterContainerType1{Inner: &InnerContainerType1{Int32LeafListName: []int32{42, 43, 44}, Int32LeafName: ygot.Int32(5)}}},
inPath: mustPath("/"),
want: &ListElemStruct1{},
}, {
name: "deleting on nil root struct element",
inSchema: simpleSchema(),
inRoot: (*ListElemStruct1)(nil),
inPath: mustPath("/"),
want: (*ListElemStruct1)(nil),
}, {
name: "deleting on non-deletable element",
inRoot: 42,
inPath: mustPath("/"),
wantErrSubstring: "cannot delete on unsettable element",
}, {
name: "deleting on root leaf element",
inSchema: simpleSchema(),
inRoot: &ListElemStruct1{Key1: ygot.String("hello"), Outer: &OuterContainerType1{Inner: &InnerContainerType1{Int32LeafListName: []int32{42, 43, 44}, Int32LeafName: ygot.Int32(5)}}},
inPath: mustPath("/"),
want: &ListElemStruct1{},
}, {
name: "deleting int32 leaf in inner node",
inSchema: simpleSchema(),
Expand Down

0 comments on commit df0faa5

Please sign in to comment.