Skip to content

Commit

Permalink
Adding only_node option to add_defaults
Browse files Browse the repository at this point in the history
This patch allows user to use only_node option within add_defaults
function. This option limits the scope of creating and adding implicit
default data nodes to just given tree where DNode is considered as root
  • Loading branch information
steweg committed Jan 27, 2024
1 parent 3e3af68 commit eec4f53
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ LY_ERR lyd_merge_module(struct lyd_node **, const struct lyd_node *, const struc
#define LYD_IMPLICIT_OUTPUT ...
#define LYD_IMPLICIT_NO_DEFAULTS ...

LY_ERR lyd_new_implicit_tree(struct lyd_node *, uint32_t, struct lyd_node **);
LY_ERR lyd_new_implicit_all(struct lyd_node **, const struct ly_ctx *, uint32_t, struct lyd_node **);

LY_ERR lyd_new_meta(const struct ly_ctx *, struct lyd_node *, const struct lys_module *, const char *, const char *, ly_bool, struct lyd_meta **);
Expand Down
11 changes: 8 additions & 3 deletions libyang/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,21 @@ def add_defaults(
no_defaults: bool = False,
no_state: bool = False,
output: bool = False,
only_node: bool = False,
):
flags = implicit_flags(
no_config=no_config,
no_defaults=no_defaults,
no_state=no_state,
output=output,
)
node_p = ffi.new("struct lyd_node **")
node_p[0] = self.cdata
ret = lib.lyd_new_implicit_all(node_p, self.context.cdata, flags, ffi.NULL)
if only_node:
node_p = ffi.cast("struct lyd_node *", self.cdata)
ret = lib.lyd_new_implicit_tree(node_p, flags, ffi.NULL)
else:
node_p = ffi.new("struct lyd_node **")
node_p[0] = self.cdata
ret = lib.lyd_new_implicit_all(node_p, self.context.cdata, flags, ffi.NULL)
if ret != lib.LY_SUCCESS:
raise self.context.error("cannot get module")

Expand Down
21 changes: 16 additions & 5 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
DataType,
DContainer,
DLeaf,
DList,
DNode,
DNotif,
DRpc,
Expand All @@ -32,6 +33,7 @@ def setUp(self):
modules = [
self.ctx.load_module("ietf-netconf"),
self.ctx.load_module("yolo-system"),
self.ctx.load_module("yolo-nodetypes"),
]

for mod in modules:
Expand Down Expand Up @@ -891,13 +893,22 @@ def test_find_all(self):
dnode.free()

def test_add_defaults(self):
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)
node = dnode.find_path("/yolo-system:conf/speed")
JSON = '{"yolo-nodetypes:records": [{"id": "rec1"}]}'
dnode = self.ctx.parse_data_mem(
JSON, "json", validate_present=True, parse_only=True
)
self.assertIsInstance(dnode, DList)
node = dnode.find_one("id")
self.assertIsInstance(node, DLeaf)
node.free(with_siblings=False)
node = dnode.find_path("/yolo-system:conf/speed")
node = dnode.find_one("name")
self.assertIsNone(node)
dnode.add_defaults(only_node=True)
node = dnode.find_one("name")
self.assertIsInstance(node, DLeaf)
self.assertEqual(node.value(), "ASD")
node = dnode.find_path("/yolo-nodetypes:conf/speed")
self.assertIsNone(node)
dnode.add_defaults()
dnode.add_defaults(only_node=False)
node = dnode.find_path("/yolo-system:conf/speed")
self.assertIsInstance(node, DLeaf)
self.assertEqual(node.value(), 4321)
Expand Down
11 changes: 11 additions & 0 deletions tests/yang/yolo/yolo-nodetypes.yang
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ module yolo-nodetypes {
"Initial version.";
}

list records {
key id;
leaf id {
type string;
}
leaf name {
type string;
default "ASD";
}
}

container conf {
description
"Configuration.";
Expand Down

0 comments on commit eec4f53

Please sign in to comment.