yin parser CHANGE add support for leaf element
diff --git a/src/parser_yin.c b/src/parser_yin.c
index c921150..ea010fb 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -1079,7 +1079,7 @@
}
/* parser argument */
- yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw);
+ LY_CHECK_RET(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},
@@ -1096,6 +1096,58 @@
}
/**
+ * @brief parse leaf 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] node_meta Meta information about node parent and siblings.
+ *
+ * @return LY_ERR values.
+ */
+static LY_ERR
+yin_parse_leaf(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
+ struct tree_node_meta *node_meta)
+{
+ struct lysp_node *iter;
+ struct lysp_node_leaf *leaf;
+
+ /* create structure */
+ leaf = calloc(1, sizeof *leaf);
+ LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
+ leaf->nodetype = LYS_LEAF;
+ leaf->parent = node_meta->parent;
+
+ /* insert into siblings */
+ if (!*(node_meta->siblings)) {
+ *node_meta->siblings = (struct lysp_node *)leaf;
+ } else {
+ for (iter = *node_meta->siblings; iter->next; iter = iter->next);
+ iter->next = (struct lysp_node *)leaf;
+ }
+
+ /* parser argument */
+ LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, YANG_LEAF));
+
+ /* parse content */
+ struct yin_subelement subelems[12] = {
+ {YANG_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
+ {YANG_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
+ {YANG_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
+ {YANG_IF_FEATURE, &leaf->iffeatures, 0},
+ {YANG_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
+ {YANG_MUST, &leaf->musts, 0},
+ {YANG_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
+ {YANG_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
+ {YANG_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
+ {YANG_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
+ {YANG_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
+ {YANG_CUSTOM, NULL, 0},
+ };
+ return yin_parse_content(ctx, subelems, 12, data, YANG_LEAF, NULL, &leaf->exts);
+}
+
+/**
* @brief Map keyword type to substatement info.
*
* @param[in] kw Keyword type.
@@ -1346,6 +1398,7 @@
case YANG_KEY:
break;
case YANG_LEAF:
+ ret = yin_parse_leaf(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
break;
case YANG_LEAF_LIST:
break;
diff --git a/tests/src/test_parser_yin.c b/tests/src/test_parser_yin.c
index 41065fc..e58b399 100644
--- a/tests/src/test_parser_yin.c
+++ b/tests/src/test_parser_yin.c
@@ -47,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 0
+#define ENABLE_LOGGER_CHECKING 1
#if ENABLE_LOGGER_CHECKING
static void
@@ -1926,6 +1926,64 @@
st->finished_correctly = true;
}
+static void
+test_leaf_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_leaf *parsed = NULL;
+
+ /* max elements */
+ data = ELEMENT_WRAPPER_START
+ "<leaf name=\"leaf\">"
+ "<config value=\"true\" />"
+ "<default value=\"def-val\"/>"
+ "<description><text>desc</text></description>"
+ "<if-feature name=\"feature\" />"
+ "<mandatory value=\"true\" />"
+ "<must condition=\"must-cond\" />"
+ "<reference><text>ref</text></reference>"
+ "<status value=\"deprecated\"/>"
+ "<type name=\"type\"/>"
+ "<units name=\"uni\"/>"
+ "<when condition=\"when-cond\"/>"
+ "</leaf>"
+ ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
+ parsed = (struct lysp_node_leaf *)siblings;
+ assert_null(parsed->parent);
+ assert_int_equal(parsed->nodetype, LYS_LEAF);
+ 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, "leaf");
+ 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);
+ assert_string_equal(parsed->musts->arg, "must-cond");
+ assert_string_equal(parsed->type.name, "type");
+ assert_string_equal(parsed->units, "uni");
+ assert_string_equal(parsed->dflt, "def-val");
+ lysp_node_free(st->ctx, siblings);
+ siblings = NULL;
+
+ /* min elements */
+ data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
+ parsed = (struct lysp_node_leaf *)siblings;
+ assert_string_equal(parsed->name, "leaf");
+ assert_string_equal(parsed->type.name, "type");
+ lysp_node_free(st->ctx, siblings);
+ siblings = NULL;
+
+ st->finished_correctly = true;
+}
+
int
main(void)
{
@@ -1975,6 +2033,8 @@
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),
+ cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
+
};
return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);