Skip to content

Commit 06d486d

Browse files
stewegsamuel-gauthier
authored andcommitted
data: add only_node option to add_defaults
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. Fixes: #96 Signed-off-by: Stefan Gula <steweg@gmail.com> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 21849df commit 06d486d

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

cffi/cdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ LY_ERR lyd_merge_module(struct lyd_node **, const struct lyd_node *, const struc
10211021
#define LYD_IMPLICIT_OUTPUT ...
10221022
#define LYD_IMPLICIT_NO_DEFAULTS ...
10231023

1024+
LY_ERR lyd_new_implicit_tree(struct lyd_node *, uint32_t, struct lyd_node **);
10241025
LY_ERR lyd_new_implicit_all(struct lyd_node **, const struct ly_ctx *, uint32_t, struct lyd_node **);
10251026

10261027
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 **);

libyang/data.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,21 @@ def add_defaults(
260260
no_defaults: bool = False,
261261
no_state: bool = False,
262262
output: bool = False,
263+
only_node: bool = False,
263264
):
264265
flags = implicit_flags(
265266
no_config=no_config,
266267
no_defaults=no_defaults,
267268
no_state=no_state,
268269
output=output,
269270
)
270-
node_p = ffi.new("struct lyd_node **")
271-
node_p[0] = self.cdata
272-
ret = lib.lyd_new_implicit_all(node_p, self.context.cdata, flags, ffi.NULL)
271+
if only_node:
272+
node_p = ffi.cast("struct lyd_node *", self.cdata)
273+
ret = lib.lyd_new_implicit_tree(node_p, flags, ffi.NULL)
274+
else:
275+
node_p = ffi.new("struct lyd_node **")
276+
node_p[0] = self.cdata
277+
ret = lib.lyd_new_implicit_all(node_p, self.context.cdata, flags, ffi.NULL)
273278
if ret != lib.LY_SUCCESS:
274279
raise self.context.error("cannot get module")
275280

tests/test_data.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
DataType,
1515
DContainer,
1616
DLeaf,
17+
DList,
1718
DNode,
1819
DNotif,
1920
DRpc,
@@ -893,13 +894,22 @@ def test_find_all(self):
893894
dnode.free()
894895

895896
def test_add_defaults(self):
896-
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)
897-
node = dnode.find_path("/yolo-system:conf/speed")
897+
JSON = '{"yolo-nodetypes:records": [{"id": "rec1"}]}'
898+
dnode = self.ctx.parse_data_mem(
899+
JSON, "json", validate_present=True, parse_only=True
900+
)
901+
self.assertIsInstance(dnode, DList)
902+
node = dnode.find_one("id")
898903
self.assertIsInstance(node, DLeaf)
899-
node.free(with_siblings=False)
900-
node = dnode.find_path("/yolo-system:conf/speed")
904+
node = dnode.find_one("name")
905+
self.assertIsNone(node)
906+
dnode.add_defaults(only_node=True)
907+
node = dnode.find_one("name")
908+
self.assertIsInstance(node, DLeaf)
909+
self.assertEqual(node.value(), "ASD")
910+
node = dnode.find_path("/yolo-nodetypes:conf/speed")
901911
self.assertIsNone(node)
902-
dnode.add_defaults()
912+
dnode.add_defaults(only_node=False)
903913
node = dnode.find_path("/yolo-system:conf/speed")
904914
self.assertIsInstance(node, DLeaf)
905915
self.assertEqual(node.value(), 4321)

tests/yang/yolo/yolo-nodetypes.yang

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ module yolo-nodetypes {
1111
"Initial version.";
1212
}
1313

14+
list records {
15+
key id;
16+
leaf id {
17+
type string;
18+
}
19+
leaf name {
20+
type string;
21+
default "ASD";
22+
}
23+
}
24+
1425
container conf {
1526
presence "enable conf";
1627
description

0 commit comments

Comments
 (0)