yin parser CHANGE add support for anydata and anyxml elements
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 4dc25e0..c921150 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -1047,6 +1047,55 @@
}
/**
+ * @brief parse any-data or any-xml element.
+ *
+ * @param[in,out] ctx YIN parser context for logging and to store current state.
+ * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
+ * @param[in,out] data Data to read from, always moved to currently handled character.
+ * @param[in] any_kw Identification of current element, can be set to YANG_ANY_DATA or YANG_ANY_XML
+ * @param[in] node_meta Meta information about node parent and siblings.
+ *
+ * @return LY_ERR values.
+ */
+static LY_ERR
+yin_parse_any(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
+ enum yang_keyword any_kw, struct tree_node_meta *node_meta)
+{
+ struct lysp_node *iter;
+ struct lysp_node_anydata *any;
+
+ /* create structure */
+ any = calloc(1, sizeof *any);
+ LY_CHECK_ERR_RET(!any, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
+ any->nodetype = (any_kw == YANG_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
+ any->parent = node_meta->parent;
+
+ /* insert into siblings */
+ if (!*(node_meta->siblings)) {
+ *(node_meta->siblings) = (struct lysp_node *)any;
+ } else {
+ for (iter = *(node_meta->siblings); iter->next; iter = iter->next);
+ iter->next = (struct lysp_node *)any;
+ }
+
+ /* parser argument */
+ yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw);
+
+ struct yin_subelement subelems[9] = {
+ {YANG_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
+ {YANG_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
+ {YANG_IF_FEATURE, &any->iffeatures, 0},
+ {YANG_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
+ {YANG_MUST, &any->musts, 0},
+ {YANG_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
+ {YANG_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
+ {YANG_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
+ {YANG_CUSTOM, NULL, 0},
+ };
+ return yin_parse_content(ctx, subelems, 9, data, any_kw, NULL, &any->exts);
+}
+
+/**
* @brief Map keyword type to substatement info.
*
* @param[in] kw Keyword type.
@@ -1212,8 +1261,8 @@
case YANG_ACTION:
break;
case YANG_ANYDATA:
- break;
case YANG_ANYXML:
+ ret = yin_parse_any(ctx, attrs, data, kw, (struct tree_node_meta *)subelem->dest);
break;
case YANG_ARGUMENT:
ret = yin_parse_argument_element(ctx, attrs, data, (struct yin_argument_meta *)subelem->dest, exts);
diff --git a/src/parser_yin.h b/src/parser_yin.h
index 8ae07d8..b356991 100644
--- a/src/parser_yin.h
+++ b/src/parser_yin.h
@@ -88,6 +88,12 @@
const char **argument; /**< Argument value */
};
+/* Maet information passed to functions working with tree schema such as yin_parse_any */
+struct tree_node_meta {
+ struct lysp_node *parent; /**< parent node */
+ struct lysp_node **siblings; /**< linked list of siblings */
+};
+
/**
* @brief Match argument name.
*
diff --git a/tests/src/test_parser_yin.c b/tests/src/test_parser_yin.c
index 8bfd09b..41065fc 100644
--- a/tests/src/test_parser_yin.c
+++ b/tests/src/test_parser_yin.c
@@ -32,6 +32,7 @@
void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext);
void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
+void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
struct state {
struct ly_ctx *ctx;
@@ -46,7 +47,7 @@
int store = -1; /* negative for infinite logging, positive for limited logging */
/* set to 0 to printing error messages to stderr instead of checking them in code */
-#define ENABLE_LOGGER_CHECKING 1
+#define ENABLE_LOGGER_CHECKING 0
#if ENABLE_LOGGER_CHECKING
static void
@@ -1842,6 +1843,89 @@
st->finished_correctly = true;
}
+static void
+test_any_elem(void **state)
+{
+ struct state *st = *state;
+ const char *data;
+ struct lysp_node *siblings = NULL;
+ struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
+ struct lysp_node_anydata *parsed = NULL;
+
+ /* anyxml max subelems */
+ data = ELEMENT_WRAPPER_START
+ "<anyxml name=\"any-name\">"
+ "<config value=\"true\" />"
+ "<description><text>desc</text></description>"
+ "<if-feature name=\"feature\" />"
+ "<mandatory value=\"true\" />"
+ "<must condition=\"must-cond\" />"
+ "<reference><text>ref</text></reference>"
+ "<status value=\"deprecated\"/>"
+ "<when condition=\"when-cond\"/>"
+ "</anyxml>"
+ ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
+ parsed = (struct lysp_node_anydata *)siblings;
+ assert_null(parsed->parent);
+ assert_int_equal(parsed->nodetype, LYS_ANYXML);
+ assert_true(parsed->flags | LYS_CONFIG_W);
+ assert_true(parsed->flags | LYS_MAND_TRUE);
+ assert_true(parsed->flags | LYS_STATUS_DEPRC);
+ assert_null(parsed->next);
+ assert_string_equal(parsed->name, "any-name");
+ assert_string_equal(parsed->dsc, "desc");
+ assert_string_equal(parsed->ref, "ref");
+ assert_string_equal(parsed->when->cond, "when-cond");
+ assert_string_equal(*parsed->iffeatures, "feature");
+ assert_null(parsed->exts);
+ lysp_node_free(st->ctx, siblings);
+ siblings = NULL;
+
+ /* anydata max subelems */
+ data = ELEMENT_WRAPPER_START
+ "<anydata name=\"any-name\">"
+ "<config value=\"true\" />"
+ "<description><text>desc</text></description>"
+ "<if-feature name=\"feature\" />"
+ "<mandatory value=\"true\" />"
+ "<must condition=\"must-cond\" />"
+ "<reference><text>ref</text></reference>"
+ "<status value=\"deprecated\"/>"
+ "<when condition=\"when-cond\"/>"
+ "</anydata>"
+ ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
+ parsed = (struct lysp_node_anydata *)siblings;
+ assert_null(parsed->parent);
+ assert_int_equal(parsed->nodetype, LYS_ANYDATA);
+ assert_true(parsed->flags | LYS_CONFIG_W);
+ assert_true(parsed->flags | LYS_MAND_TRUE);
+ assert_true(parsed->flags | LYS_STATUS_DEPRC);
+ assert_null(parsed->next);
+ assert_string_equal(parsed->name, "any-name");
+ assert_string_equal(parsed->dsc, "desc");
+ assert_string_equal(parsed->ref, "ref");
+ assert_string_equal(parsed->when->cond, "when-cond");
+ assert_string_equal(*parsed->iffeatures, "feature");
+ assert_null(parsed->exts);
+ lysp_node_free(st->ctx, siblings);
+ siblings = NULL;
+
+ /* min subelems */
+ node_meta.parent = (void *)0x10;
+ data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
+ parsed = (struct lysp_node_anydata *)siblings;
+ assert_ptr_equal(parsed->parent, node_meta.parent);
+ assert_int_equal(parsed->nodetype, LYS_ANYDATA);
+ assert_null(parsed->next);
+ assert_null(parsed->exts);
+ lysp_node_free(st->ctx, siblings);
+
+ st->finished_correctly = true;
+}
+
int
main(void)
{
@@ -1890,6 +1974,7 @@
cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
+ cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
};
return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);