Skip to content

Commit

Permalink
Adding insert_sibling function
Browse files Browse the repository at this point in the history
This patch introduce insert_sibling function usable for inserting data
nodes on the module level
  • Loading branch information
steweg committed Jan 26, 2024
1 parent 3e3af68 commit aec0917
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down
5 changes: 5 additions & 0 deletions libyang/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand Down Expand Up @@ -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)
4 changes: 4 additions & 0 deletions tests/yang/yolo/yolo-nodetypes.yang
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ module yolo-nodetypes {
default 2.6;
}
}

leaf test1 {
type uint8;
}
}

0 comments on commit aec0917

Please sign in to comment.