data tree FEATURE lyd_find_path function
diff --git a/src/tree.h b/src/tree.h
index cc8799b..a6f1463 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -85,6 +85,7 @@
* - ::lyd_new_path()
* - ::lyd_new_path2()
* - ::lyd_path()
+ * - ::lyd_find_path()
* - ::lys_find_path()
*
*/
diff --git a/src/tree_data.c b/src/tree_data.c
index c904370..c7532b3 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -3545,6 +3545,35 @@
return ret;
}
+API LY_ERR
+lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lyxp_expr *expr = NULL;
+ struct ly_path *lypath = NULL;
+
+ LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
+
+ /* parse the path */
+ ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
+ LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
+ LY_CHECK_GOTO(ret, cleanup);
+
+ /* compile the path */
+ ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, expr, LY_PATH_LREF_FALSE,
+ output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, LY_PREF_JSON,
+ (void *)LYD_CTX(ctx_node), NULL, &lypath);
+ LY_CHECK_GOTO(ret, cleanup);
+
+ /* evaluate the path */
+ ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
+
+cleanup:
+ lyxp_expr_free(LYD_CTX(ctx_node), expr);
+ ly_path_free(LYD_CTX(ctx_node), lypath);
+ return ret;
+}
+
API uint32_t
lyd_list_pos(const struct lyd_node *instance)
{
diff --git a/src/tree_data.h b/src/tree_data.h
index 5ac2530..83dc68a 100644
--- a/src/tree_data.h
+++ b/src/tree_data.h
@@ -89,6 +89,7 @@
* - ::lyd_parent()
* - ::lyd_owner_module()
* - ::lyd_find_xpath()
+ * - ::lyd_find_path()
* - ::lyd_find_sibling_val()
* - ::lyd_find_sibling_first()
* - ::lyd_find_meta()
@@ -1663,16 +1664,13 @@
/**
* @brief Search in the given data for instances of nodes matching the provided XPath.
*
- * The expected format of the expression is ::LYD_JSON, meaning the first node in every path
- * must have its module name as prefix or be the special `*` value for all the nodes.
- *
* If a list instance is being selected with all its key values specified (but not necessarily ordered)
* in the form `list[key1='val1'][key2='val2'][key3='val3']` or a leaf-list instance in the form
* `leaf-list[.='val']`, these instances are found using hashes with constant (*O(1)*) complexity
* (unless they are defined in top-level). Other predicates can still follow the aforementioned ones.
*
* @param[in] ctx_node XPath context node.
- * @param[in] xpath Data XPath expression filtering the matching nodes. ::LYD_JSON format is expected.
+ * @param[in] xpath [XPath](@ref howtoXPath) to select.
* @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean,
* the returned set is empty.
* @return LY_SUCCESS on success, @p set is returned.
@@ -1680,6 +1678,20 @@
*/
LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set);
+/**
+ * @brief Search in given data for a node uniquely identifier by a path.
+ *
+ * @param[in] ctx_node Path context node.
+ * @param[in] path [Path](@ref howtoXPath) to find.
+ * @param[in] output Whether to search in RPC/action output nodes or in input nodes.
+ * @param[out] match Can be NULL, otherwise the found data node.
+ * @return LY_SUCCESS on success, @p match is set to the found node.
+ * @return LY_EINCOMPLETE if only a parent of the node was found, @p match is set to this parent node.
+ * @return LY_ENOTFOUND if no nodes in the path were found.
+ * @return LY_ERR on other errors.
+ */
+LY_ERR lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match);
+
#ifdef __cplusplus
}
#endif