data tree FEATURE lyd_new_meta2 function
For creating metadata from opaque attributes.
diff --git a/src/tree_data.c b/src/tree_data.c
index e8b598d..41d021f 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -996,7 +996,40 @@
NULL, LYD_HINT_DATA, clear_dflt, NULL);
}
+API LY_ERR
+lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
+ struct lyd_meta **meta)
+{
+ const struct lys_module *mod;
+
+ LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
+
+ if (parent && !parent->schema) {
+ LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
+ return LY_EINVAL;
}
+
+ switch (attr->format) {
+ case LY_PREF_XML:
+ mod = ly_ctx_get_module_implemented_ns(ctx, attr->prefix.module_ns);
+ if (!mod) {
+ LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->prefix.module_ns);
+ return LY_ENOTFOUND;
+ }
+ break;
+ case LY_PREF_JSON:
+ mod = ly_ctx_get_module_implemented(ctx, attr->prefix.module_name);
+ if (!mod) {
+ LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->prefix.module_name);
+ return LY_ENOTFOUND;
+ }
+ break;
+ default:
+ LOGINT_RET(ctx);
+ }
+
+ return lyd_create_meta(parent, meta, mod, attr->name, strlen(attr->name), attr->value, strlen(attr->value),
+ NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
}
API LY_ERR
diff --git a/src/tree_data.h b/src/tree_data.h
index 0dbb3c7..81b85aa 100644
--- a/src/tree_data.h
+++ b/src/tree_data.h
@@ -923,6 +923,21 @@
const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta);
/**
+ * @brief Create new metadata from an opaque node attribute if possible.
+ *
+ * @param[in] ctx libyang context.
+ * @param[in] parent Optional parent node for the metadata being created. Must be set if @p meta is NULL.
+ * @param[in] clear_dflt Whether to clear the default flag starting from @p parent, recursively all NP containers.
+ * @param[in] attr Opaque node attribute to parse into metadata.
+ * @param[out] meta Optional created metadata. Must be set if @p parent is NULL.
+ * @return LY_SUCCESS on success.
+ * @return LY_ENOT if the attribute could not be parsed into any metadata.
+ * @return LY_ERR on error.
+ */
+LY_ERR lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
+ struct lyd_meta **meta);
+
+/**
* @brief Create a new opaque node in the data tree.
*
* @param[in] parent Parent node for the node beaing created. NULL in case of creating a top level element.