plugins exts CHANGE ext parsing isolated into a callback
Lots of refactoring and finishing up included.
diff --git a/src/plugins_exts/metadata.c b/src/plugins_exts/metadata.c
index f6e1580..fc49e2c 100644
--- a/src/plugins_exts/metadata.c
+++ b/src/plugins_exts/metadata.c
@@ -1,9 +1,10 @@
/**
* @file metadata.c
* @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Michal Vasko <mvasko@cesnet.cz>
* @brief libyang extension plugin - Metadata (RFC 7952)
*
- * Copyright (c) 2019 CESNET, z.s.p.o.
+ * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -21,93 +22,150 @@
#include "libyang.h"
#include "plugins_exts.h"
-/**
- * @brief Representation of the compiled metadata substatements - simplify storage for the items available via
- * ::lysc_ext_substmt.
- */
-struct lyext_metadata {
- struct lysc_type *type; /**< type of the metadata (mandatory) */
+struct lysp_ext_metadata {
+ struct lysp_type *type; /**< type of the metadata (mandatory) */
const char *units; /**< units of the leaf's type */
- struct lysc_iffeature *iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
+ struct lysp_qname *iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
const char *dsc; /**< description */
const char *ref; /**< reference */
uint16_t flags; /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */
};
+struct lysc_ext_metadata {
+ struct lysc_type *type; /**< type of the metadata (mandatory) */
+ const char *units; /**< units of the leaf's type */
+ const char *dsc; /**< description */
+ const char *ref; /**< reference */
+ uint16_t flags; /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */
+};
+
+/**
+ * @brief Parse annotation extension instances.
+ *
+ * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
+ */
+static LY_ERR
+annotation_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
+{
+ LY_ERR r;
+ struct lysp_ext_metadata *ann_pdata;
+ struct lysp_module *pmod;
+ LY_ARRAY_COUNT_TYPE u;
+
+ /* annotations can appear only at the top level of a YANG module or submodule */
+ if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
+ lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is allowed only at the top level of a YANG module or "
+ "submodule, but it is placed in \"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt));
+ return LY_EVALID;
+ }
+
+ pmod = ext->parent;
+
+ /* check for duplication */
+ LY_ARRAY_FOR(pmod->exts, u) {
+ if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) {
+ /* duplication of the same annotation extension in a single module */
+ lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name);
+ return LY_EVALID;
+ }
+ }
+
+ /* parse annotation substatements */
+ ext->parsed = ann_pdata = calloc(1, sizeof *ann_pdata);
+ if (!ann_pdata) {
+ goto emem;
+ }
+ LY_ARRAY_CREATE_GOTO(lyplg_extp_cur_pmod(pctx)->mod->ctx, ext->substmts, 6, r, emem);
+
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[0].stmt = LY_STMT_IF_FEATURE;
+ ext->substmts[0].storage = &ann_pdata->iffeatures;
+
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[1].stmt = LY_STMT_UNITS;
+ ext->substmts[1].storage = &ann_pdata->units;
+
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[2].stmt = LY_STMT_STATUS;
+ ext->substmts[2].storage = &ann_pdata->flags;
+
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[3].stmt = LY_STMT_TYPE;
+ ext->substmts[3].storage = &ann_pdata->type;
+
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[4].stmt = LY_STMT_DESCRIPTION;
+ ext->substmts[4].storage = &ann_pdata->dsc;
+
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[5].stmt = LY_STMT_REFERENCE;
+ ext->substmts[5].storage = &ann_pdata->ref;
+
+ if ((r = lyplg_ext_parse_extension_instance(pctx, ext))) {
+ return r;
+ }
+
+ /* check for mandatory substatements */
+ if (!ann_pdata->type) {
+ lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Missing mandatory keyword \"type\" as a child of \"%s %s\".",
+ ext->name, ext->argument);
+ return LY_EVALID;
+ }
+
+ return LY_SUCCESS;
+
+emem:
+ lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
+ return LY_EMEM;
+}
+
/**
* @brief Compile annotation extension instances.
*
* Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
*/
static LY_ERR
-annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
+annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext)
{
LY_ERR ret;
- struct lyext_metadata *annotation;
- struct lysc_module *mod_c;
- LY_ARRAY_COUNT_TYPE u;
-
- /* annotations can appear only at the top level of a YANG module or submodule */
- if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) {
- lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
- "Extension %s is allowed only at the top level of a YANG module or submodule, but it is placed in \"%s\" statement.",
- p_ext->name, ly_stmt2str(c_ext->parent_stmt));
- return LY_EVALID;
- }
- /* check mandatory argument */
- if (!c_ext->argument) {
- lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
- "Extension %s is instantiated without mandatory argument representing metadata name.", p_ext->name);
- return LY_EVALID;
- }
-
- mod_c = (struct lysc_module *)c_ext->parent;
-
- /* check for duplication */
- LY_ARRAY_FOR(mod_c->exts, u) {
- if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) {
- /* duplication of the same annotation extension in a single module */
- lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s is instantiated multiple times.", p_ext->name);
- return LY_EVALID;
- }
- }
+ struct lysc_ext_metadata *ann_cdata;
/* compile annotation substatements */
- c_ext->data = annotation = calloc(1, sizeof *annotation);
- if (!annotation) {
+ ext->compiled = ann_cdata = calloc(1, sizeof *ann_cdata);
+ if (!ann_cdata) {
goto emem;
}
- LY_ARRAY_CREATE_GOTO(lysc_ctx_get_ctx(cctx), c_ext->substmts, 6, ret, emem);
+ LY_ARRAY_CREATE_GOTO(lysc_ctx_get_ctx(cctx), ext->substmts, 6, ret, emem);
- LY_ARRAY_INCREMENT(c_ext->substmts);
- c_ext->substmts[ANNOTATION_SUBSTMT_IFF].stmt = LY_STMT_IF_FEATURE;
- c_ext->substmts[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures;
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[0].stmt = LY_STMT_IF_FEATURE;
+ ext->substmts[0].storage = NULL;
- LY_ARRAY_INCREMENT(c_ext->substmts);
- c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].stmt = LY_STMT_UNITS;
- c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units;
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[1].stmt = LY_STMT_UNITS;
+ ext->substmts[1].storage = &ann_cdata->units;
- LY_ARRAY_INCREMENT(c_ext->substmts);
- c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].stmt = LY_STMT_STATUS;
- c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags;
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[2].stmt = LY_STMT_STATUS;
+ ext->substmts[2].storage = &ann_cdata->flags;
- LY_ARRAY_INCREMENT(c_ext->substmts);
- c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].stmt = LY_STMT_TYPE;
- c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type;
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[3].stmt = LY_STMT_TYPE;
+ ext->substmts[3].storage = &ann_cdata->type;
- LY_ARRAY_INCREMENT(c_ext->substmts);
- c_ext->substmts[ANNOTATION_SUBSTMT_DSC].stmt = LY_STMT_DESCRIPTION;
- c_ext->substmts[ANNOTATION_SUBSTMT_DSC].storage = &annotation->dsc;
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[4].stmt = LY_STMT_DESCRIPTION;
+ ext->substmts[4].storage = &ann_cdata->dsc;
- LY_ARRAY_INCREMENT(c_ext->substmts);
- c_ext->substmts[ANNOTATION_SUBSTMT_REF].stmt = LY_STMT_REFERENCE;
- c_ext->substmts[ANNOTATION_SUBSTMT_REF].storage = &annotation->ref;
+ LY_ARRAY_INCREMENT(ext->substmts);
+ ext->substmts[5].stmt = LY_STMT_REFERENCE;
+ ext->substmts[5].storage = &ann_cdata->ref;
- ret = lys_compile_extension_instance(cctx, p_ext, c_ext);
+ ret = lyplg_ext_compile_extension_instance(cctx, extp, ext);
return ret;
emem:
- lyplg_ext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__);
+ lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
return LY_EMEM;
}
@@ -117,27 +175,43 @@
* Implementation of ::lyplg_ext_schema_printer_clb set as ::lyext_plugin::sprinter
*/
static LY_ERR
-annotation_schema_printer(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
+annotation_sprinter(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
{
- lysc_print_extension_instance(ctx, ext, flag);
+ lyplg_ext_print_extension_instance(ctx, ext, flag);
return LY_SUCCESS;
}
/**
- * @brief Free annotation extension instances' data.
+ * @brief Free parsed annotation extension instance data.
*
- * Implementation of ::lyplg_ext_free_clb callback set as ::lyext_plugin::free.
+ * Implementation of ::lyplg_ext_parse_free_clb callback set as ::lyext_plugin::pfree.
*/
static void
-annotation_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
+annotation_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext)
{
if (!ext->substmts) {
return;
}
- lyplg_ext_instance_substatements_free(ctx, ext->substmts);
- free(ext->data);
+ lyplg_ext_pfree_instance_substatements(ctx, ext->substmts);
+ free(ext->parsed);
+}
+
+/**
+ * @brief Free compiled annotation extension instance data.
+ *
+ * Implementation of ::lyplg_ext_compile_free_clb callback set as ::lyext_plugin::cfree.
+ */
+static void
+annotation_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
+{
+ if (!ext->substmts) {
+ return;
+ }
+
+ lyplg_ext_cfree_instance_substatements(ctx, ext->substmts);
+ free(ext->compiled);
}
/**
@@ -153,13 +227,15 @@
.revision = "2016-08-05",
.name = "annotation",
- .plugin.id = "libyang 2 - metadata, version 1",
- .plugin.compile = &annotation_compile,
- .plugin.sprinter = &annotation_schema_printer,
- .plugin.free = annotation_free,
+ .plugin.id = "ly2 metadata v1",
+ .plugin.parse = annotation_parse,
+ .plugin.compile = annotation_compile,
+ .plugin.sprinter = annotation_sprinter,
.plugin.node = NULL,
.plugin.snode = NULL,
- .plugin.validate = NULL
+ .plugin.validate = NULL,
+ .plugin.pfree = annotation_pfree,
+ .plugin.cfree = annotation_cfree,
},
{0} /* terminating zeroed record */
};