Skip to content

Commit 2fc312b

Browse files
stewegsamuel-gauthier
authored andcommitted
data: add insert_[before,after] API to lists
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]>
1 parent 25d7de8 commit 2fc312b

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

cffi/cdefs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,8 @@ LY_ERR lyd_merge_tree(struct lyd_node **, const struct lyd_node *, uint16_t);
11261126
LY_ERR lyd_merge_siblings(struct lyd_node **, const struct lyd_node *, uint16_t);
11271127
LY_ERR lyd_insert_child(struct lyd_node *, struct lyd_node *);
11281128
LY_ERR lyd_insert_sibling(struct lyd_node *, struct lyd_node *, struct lyd_node **);
1129+
LY_ERR lyd_insert_after(struct lyd_node *, struct lyd_node *);
1130+
LY_ERR lyd_insert_before(struct lyd_node *, struct lyd_node *);
11291131
LY_ERR lyd_diff_apply_all(struct lyd_node **, const struct lyd_node *);
11301132

11311133
#define LYD_DUP_NO_META ...

libyang/data.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ def insert_sibling(self, node):
415415
if ret != lib.LY_SUCCESS:
416416
raise self.context.error("cannot insert sibling")
417417

418+
def insert_after(self, node):
419+
ret = lib.lyd_insert_after(self.cdata, node.cdata)
420+
if ret != lib.LY_SUCCESS:
421+
raise self.context.error("cannot insert sibling after")
422+
423+
def insert_before(self, node):
424+
ret = lib.lyd_insert_before(self.cdata, node.cdata)
425+
if ret != lib.LY_SUCCESS:
426+
raise self.context.error("cannot insert sibling before")
427+
418428
def name(self) -> str:
419429
return c2str(self.cdata.schema.name)
420430

tests/test_data.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,23 @@ def test_dnode_insert_sibling(self):
966966
self.assertIsInstance(sibling, DLeaf)
967967
self.assertEqual(sibling.cdata, dnode2.cdata)
968968

969+
def test_dnode_insert_sibling_before_after(self):
970+
R1 = {"yolo-nodetypes:records": [{"id": "id1", "name": "name1"}]}
971+
R2 = {"yolo-nodetypes:records": [{"id": "id2", "name": "name2"}]}
972+
R3 = {"yolo-nodetypes:records": [{"id": "id3", "name": "name3"}]}
973+
module = self.ctx.get_module("yolo-nodetypes")
974+
dnode1 = dict_to_dnode(R1, module, None, validate=False)
975+
dnode2 = dict_to_dnode(R2, module, None, validate=False)
976+
dnode3 = dict_to_dnode(R3, module, None, validate=False)
977+
self.assertEqual(dnode1.first_sibling().cdata, dnode1.cdata)
978+
dnode1.insert_before(dnode2)
979+
dnode1.insert_after(dnode3)
980+
self.assertEqual(
981+
[dnode2.cdata, dnode1.cdata, dnode3.cdata],
982+
[s.cdata for s in dnode1.first_sibling().siblings()],
983+
)
984+
self.assertEqual(dnode1.first_sibling().cdata, dnode2.cdata)
985+
969986
def _create_opaq_hostname(self):
970987
root = self.ctx.create_data_path(path="/yolo-system:conf")
971988
root.new_path(

tests/yang/yolo/yolo-nodetypes.yang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module yolo-nodetypes {
2020
type string;
2121
default "ASD";
2222
}
23+
ordered-by user;
2324
}
2425

2526
container conf {

0 commit comments

Comments
 (0)