data FEATURE add lyd_first_sibling() function
diff --git a/src/tree_data.c b/src/tree_data.c
index ef44019..4e0e18b 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -3389,3 +3389,22 @@
return pos;
}
+
+API struct lyd_node *
+lyd_first_sibling(const struct lyd_node *node)
+{
+ struct lyd_node *start;
+
+ if (!node) {
+ return NULL;
+ }
+
+ /* get the first sibling */
+ if (node->parent) {
+ start = node->parent->child;
+ } else {
+ for (start = (struct lyd_node *)node; start->prev->next; start = start->prev);
+ }
+
+ return start;
+}
diff --git a/src/tree_data.h b/src/tree_data.h
index 1a2fb46..a5accd5 100644
--- a/src/tree_data.h
+++ b/src/tree_data.h
@@ -786,6 +786,15 @@
uint32_t lyd_list_pos(const struct lyd_node *instance);
/**
+ * @brief Get the first sibling of the given node.
+ *
+ * @param[in] node Node which first sibling is going to be the result.
+ * @return The first sibling of the given node or the node itself if it is the first child of the parent.
+ */
+struct lyd_node *
+lyd_first_sibling(const struct lyd_node *node);
+
+/**
* @brief Learn the length of LYB data.
*
* @param[in] data LYB data to examine.
diff --git a/tests/utests/data/test_tree_data.c b/tests/utests/data/test_tree_data.c
index 0cf0851..2143904 100644
--- a/tests/utests/data/test_tree_data.c
+++ b/tests/utests/data/test_tree_data.c
@@ -358,6 +358,32 @@
*state = NULL;
}
+static void
+test_first_sibling(void **state)
+{
+ *state = test_first_sibling;
+
+ const char *data;
+ struct lyd_node *tree;
+ struct lyd_node_inner *parent;
+
+ data = "<bar xmlns=\"urn:tests:a\">test</bar>"
+ "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>one</c></l1>"
+ "<foo xmlns=\"urn:tests:a\">test</foo>";
+ assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
+ assert_ptr_equal(tree, lyd_first_sibling(tree->next));
+ assert_ptr_equal(tree, lyd_first_sibling(tree));
+ assert_ptr_equal(tree, lyd_first_sibling(tree->prev));
+ parent = (struct lyd_node_inner*)tree->next;
+ assert_int_equal(LYS_LIST, parent->schema->nodetype);
+ assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->next));
+ assert_ptr_equal(parent->child, lyd_first_sibling(parent->child));
+ assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->prev));
+ lyd_free_all(tree);
+
+ *state = NULL;
+}
+
int main(void)
{
const struct CMUnitTest tests[] = {
@@ -365,6 +391,7 @@
cmocka_unit_test_setup_teardown(test_dup, setup, teardown),
cmocka_unit_test_setup_teardown(test_target, setup, teardown),
cmocka_unit_test_setup_teardown(test_list_pos, setup, teardown),
+ cmocka_unit_test_setup_teardown(test_first_sibling, setup, teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);