From aec091723e4f65ee53890b54e6e53aa752654e33 Mon Sep 17 00:00:00 2001 From: Stefan Gula Date: Tue, 28 Nov 2023 15:02:58 +0100 Subject: [PATCH] Adding insert_sibling function This patch introduce insert_sibling function usable for inserting data nodes on the module level --- cffi/cdefs.h | 1 + libyang/data.py | 5 +++++ tests/test_data.py | 18 ++++++++++++++++++ tests/yang/yolo/yolo-nodetypes.yang | 4 ++++ 4 files changed, 28 insertions(+) diff --git a/cffi/cdefs.h b/cffi/cdefs.h index ba6bd5d4..7ab29e50 100644 --- a/cffi/cdefs.h +++ b/cffi/cdefs.h @@ -971,6 +971,7 @@ LY_ERR lyd_any_value_str(const struct lyd_node *, char **); LY_ERR lyd_merge_tree(struct lyd_node **, const struct lyd_node *, uint16_t); LY_ERR lyd_merge_siblings(struct lyd_node **, const struct lyd_node *, uint16_t); LY_ERR lyd_insert_child(struct lyd_node *, struct lyd_node *); +LY_ERR lyd_insert_sibling(struct lyd_node *, struct lyd_node *, struct lyd_node **); LY_ERR lyd_diff_apply_all(struct lyd_node **, const struct lyd_node *); #define LYD_DUP_NO_META ... diff --git a/libyang/data.py b/libyang/data.py index c2bf95f5..cf42fd43 100644 --- a/libyang/data.py +++ b/libyang/data.py @@ -322,6 +322,11 @@ def insert_child(self, node): if ret != lib.LY_SUCCESS: raise self.context.error("cannot insert node") + def insert_sibling(self, node): + ret = lib.lyd_insert_sibling(self.cdata, node.cdata, ffi.NULL) + if ret != lib.LY_SUCCESS: + raise self.context.error("cannot insert node") + def name(self) -> str: return c2str(self.cdata.schema.name) diff --git a/tests/test_data.py b/tests/test_data.py index 25bec67d..0b7ac9f1 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -14,12 +14,14 @@ DataType, DContainer, DLeaf, + DList, DNode, DNotif, DRpc, IOType, LibyangError, ) +from libyang.data import dict_to_dnode YANG_DIR = os.path.join(os.path.dirname(__file__), "yang") @@ -32,6 +34,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: @@ -922,3 +925,18 @@ def test_dnode_unlink(self): self.assertIsNone(child, None) finally: dnode.free() + + def test_dnode_insert_sibling(self): + MAIN = {"yolo-nodetypes:conf": {"percentage": "20.2"}} + SIBLING = {"yolo-nodetypes:test1": 10} + module = self.ctx.get_module("yolo-nodetypes") + dnode1 = dict_to_dnode(MAIN, module, None, validate=False) + dnode2 = dict_to_dnode(SIBLING, module, None, validate=False) + self.assertEqual(len(list(dnode1.siblings(include_self=False))), 0) + self.assertEqual(len(list(dnode2.siblings(include_self=False))), 0) + dnode2.insert_sibling(dnode1) + self.assertEqual(len(list(dnode1.siblings(include_self=False))), 1) + self.assertEqual(len(list(dnode2.siblings(include_self=False))), 1) + sibling = next(dnode1.siblings(include_self=False), None) + self.assertIsInstance(sibling, DLeaf) + self.assertEqual(sibling.cdata, dnode2.cdata) diff --git a/tests/yang/yolo/yolo-nodetypes.yang b/tests/yang/yolo/yolo-nodetypes.yang index 3dd76832..426d274a 100644 --- a/tests/yang/yolo/yolo-nodetypes.yang +++ b/tests/yang/yolo/yolo-nodetypes.yang @@ -29,4 +29,8 @@ module yolo-nodetypes { default 2.6; } } + + leaf test1 { + type uint8; + } }