Skip to content

Commit

Permalink
data: add insert_[before,after] API to lists
Browse files Browse the repository at this point in the history
This patch adds insert_[before,after] functions, which allows user to
specify where to insert the sibling in case of ordered (leaf-)lists.

Closes: #120
Signed-off-by: Stefan Gula <[email protected]>
Signed-off-by: Samuel Gauthier <[email protected]>
  • Loading branch information
steweg authored and samuel-gauthier committed Aug 2, 2024
1 parent 25d7de8 commit 2fc312b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,8 @@ 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_insert_after(struct lyd_node *, struct lyd_node *);
LY_ERR lyd_insert_before(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
10 changes: 10 additions & 0 deletions libyang/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,16 @@ def insert_sibling(self, node):
if ret != lib.LY_SUCCESS:
raise self.context.error("cannot insert sibling")

def insert_after(self, node):
ret = lib.lyd_insert_after(self.cdata, node.cdata)
if ret != lib.LY_SUCCESS:
raise self.context.error("cannot insert sibling after")

def insert_before(self, node):
ret = lib.lyd_insert_before(self.cdata, node.cdata)
if ret != lib.LY_SUCCESS:
raise self.context.error("cannot insert sibling before")

def name(self) -> str:
return c2str(self.cdata.schema.name)

Expand Down
17 changes: 17 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,23 @@ def test_dnode_insert_sibling(self):
self.assertIsInstance(sibling, DLeaf)
self.assertEqual(sibling.cdata, dnode2.cdata)

def test_dnode_insert_sibling_before_after(self):
R1 = {"yolo-nodetypes:records": [{"id": "id1", "name": "name1"}]}
R2 = {"yolo-nodetypes:records": [{"id": "id2", "name": "name2"}]}
R3 = {"yolo-nodetypes:records": [{"id": "id3", "name": "name3"}]}
module = self.ctx.get_module("yolo-nodetypes")
dnode1 = dict_to_dnode(R1, module, None, validate=False)
dnode2 = dict_to_dnode(R2, module, None, validate=False)
dnode3 = dict_to_dnode(R3, module, None, validate=False)
self.assertEqual(dnode1.first_sibling().cdata, dnode1.cdata)
dnode1.insert_before(dnode2)
dnode1.insert_after(dnode3)
self.assertEqual(
[dnode2.cdata, dnode1.cdata, dnode3.cdata],
[s.cdata for s in dnode1.first_sibling().siblings()],
)
self.assertEqual(dnode1.first_sibling().cdata, dnode2.cdata)

def _create_opaq_hostname(self):
root = self.ctx.create_data_path(path="/yolo-system:conf")
root.new_path(
Expand Down
1 change: 1 addition & 0 deletions tests/yang/yolo/yolo-nodetypes.yang
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module yolo-nodetypes {
type string;
default "ASD";
}
ordered-by user;
}

container conf {
Expand Down

0 comments on commit 2fc312b

Please sign in to comment.