Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding insert_sibling function #95

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
IOType,
LibyangError,
)
from libyang.data import dict_to_dnode


YANG_DIR = os.path.join(os.path.dirname(__file__), "yang")
Expand All @@ -32,6 +33,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 +924,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;
}
}
Loading