yin parser CHANGE add support for mandatory element
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 08b63e7..0ffa7ab 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -686,6 +686,7 @@
case YANG_LIST:
break;
case YANG_MANDATORY:
+ ret = yin_parse_mandatory(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
break;
case YANG_MAX_ELEMENTS:
break;
@@ -864,16 +865,34 @@
}
LY_ERR
-yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
+yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
+ struct lysp_ext_instance **exts)
+{
+ const char *temp_val = NULL;
+ struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
+
+ LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY));
+ if (strcmp(temp_val, "true") == 0) {
+ *flags |= LYS_MAND_TRUE;
+ } else if (strcmp(temp_val, "false") == 0) {
+ *flags |= LYS_MAND_FALSE;
+ } else {
+ LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
+ FREE_STRING(ctx->xml_ctx.ctx, temp_val);
+ return LY_EVALID;
+ }
+ FREE_STRING(ctx->xml_ctx.ctx, temp_val);
+
+ return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
+}
+
+LY_ERR
+yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags,
+ struct lysp_ext_instance **exts)
{
const char *value = NULL;
struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
- if (*flags & LYS_STATUS_MASK) {
- LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUPELEM, "status");
- return LY_EVALID;
- }
-
LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &value, Y_STR_ARG, YANG_STATUS));
if (strcmp(value, "current") == 0) {
*flags |= LYS_STATUS_CURR;
diff --git a/src/parser_yin.h b/src/parser_yin.h
index 82c4e9b..b4ef46b 100644
--- a/src/parser_yin.h
+++ b/src/parser_yin.h
@@ -158,6 +158,20 @@
const char *prefix, size_t prefix_len);
/**
+ * @brief Parse mandatory 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 status element.
+ * @param[in,out] data Data to read from, always moved to currently handled character.
+ * @param[in,out] flags Flags to add to.
+ * @param[in,out] exts Extension instances to add to.
+ *
+ * @return LY_ERR values.
+ */
+LY_ERR yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
+ uint16_t *flags, struct lysp_ext_instance **exts);
+
+/**
* @brief Parse status element.
*
* @param[in,out] ctx Yin parser context for logging and to store current state.
diff --git a/tests/src/test_parser_yin.c b/tests/src/test_parser_yin.c
index df90385..c6edd89 100644
--- a/tests/src/test_parser_yin.c
+++ b/tests/src/test_parser_yin.c
@@ -450,16 +450,6 @@
LY_ARRAY_FREE(args);
args = NULL;
- /* duplicit definition (no reset_state() call) */
- data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
- lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
- yin_load_attributes(st->yin_ctx, &data, &args);
- ret = yin_parse_status(st->yin_ctx, &args, &data, &flags, &exts);
- assert_int_equal(ret, LY_EVALID);
- logbuf_assert("Duplicate element \"status\". Line number 1.");
- LY_ARRAY_FREE(args);
- args = NULL;
-
/* invalid status value */
st = reset_state(state);
flags = 0;
@@ -574,6 +564,7 @@
lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
ret = yin_parse_element_generic(st->yin_ctx, name, name_len, prefix, prefix_len, &data, &exts.child);
assert_int_equal(ret, LY_SUCCESS);
+ assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
assert_string_equal(exts.child->stmt, "elem");
assert_string_equal(exts.child->arg, "text_value");
assert_string_equal(exts.child->child->stmt, "attr");
@@ -694,6 +685,7 @@
{YIN_TEXT, &value, 0}};
ret = yin_parse_content(st->yin_ctx, subelems, 5, &data, YANG_PREFIX, NULL, &exts);
assert_int_equal(ret, LY_SUCCESS);
+ assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
assert_string_equal(exts->name, "custom");
assert_string_equal(exts->argument, "totally amazing extension");
assert_string_equal(value, "wsefsdf");
@@ -780,6 +772,7 @@
yin_load_attributes(st->yin_ctx, &data, &attrs);
ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL);
assert_int_equal(LY_SUCCESS, ret);
+ assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
assert_true(version == LYS_VERSION_1_0);
assert_true(st->yin_ctx->mod_version == LYS_VERSION_1_0);
LY_ARRAY_FREE(attrs);
@@ -792,6 +785,7 @@
yin_load_attributes(st->yin_ctx, &data, &attrs);
ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL);
assert_int_equal(LY_SUCCESS, ret);
+ assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
assert_true(version == LYS_VERSION_1_1);
assert_true(st->yin_ctx->mod_version == LYS_VERSION_1_1);
LY_ARRAY_FREE(attrs);
@@ -804,9 +798,77 @@
yin_load_attributes(st->yin_ctx, &data, &attrs);
ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL);
assert_int_equal(ret, LY_EVALID);
+ logbuf_assert("Invalid value \"randomvalue\" of \"yang-version\". Line number 1.");
LY_ARRAY_FREE(attrs);
attrs = NULL;
- logbuf_assert("Invalid value \"randomvalue\" of \"yang-version\". Line number 1.");
+ st = reset_state(state);
+
+ data = "<yang-version xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
+ "</yang-version>";
+ lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
+ yin_load_attributes(st->yin_ctx, &data, &attrs);
+ ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL);
+ assert_int_equal(ret, LY_EVALID);
+ LY_ARRAY_FREE(attrs);
+ attrs = NULL;
+ logbuf_assert("Missing mandatory attribute value of yang-version element. Line number 1.");
+ st->finished_correctly = true;
+}
+
+static void
+test_yin_parse_mandatory(void **state)
+{
+ struct state *st = *state;
+ LY_ERR ret = LY_SUCCESS;
+ struct sized_string name, prefix;
+ struct yin_arg_record *attrs = NULL;
+ uint16_t man = 0;
+
+ const char *data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"true\">\n"
+ "</mandatory>";
+ lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
+ yin_load_attributes(st->yin_ctx, &data, &attrs);
+ ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL);
+ assert_int_equal(LY_SUCCESS, ret);
+ assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
+ assert_true(man == LYS_MAND_TRUE);
+ LY_ARRAY_FREE(attrs);
+ attrs = NULL;
+ man = 0;
+ st = reset_state(state);
+
+ data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"false\" />";
+ lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
+ yin_load_attributes(st->yin_ctx, &data, &attrs);
+ ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL);
+ assert_int_equal(LY_SUCCESS, ret);
+ assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
+ assert_true(man == LYS_MAND_FALSE);
+ LY_ARRAY_FREE(attrs);
+ attrs = NULL;
+ man = 0;
+ st = reset_state(state);
+
+ data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"randomvalue\">\n"
+ "</mandatory>";
+ lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
+ yin_load_attributes(st->yin_ctx, &data, &attrs);
+ ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL);
+ assert_int_equal(ret, LY_EVALID);
+ LY_ARRAY_FREE(attrs);
+ logbuf_assert("Invalid value \"randomvalue\" of \"mandatory\". Line number 1.");
+ attrs = NULL;
+ man = 0;
+ st = reset_state(state);
+
+ data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
+ "</mandatory>";
+ lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
+ yin_load_attributes(st->yin_ctx, &data, &attrs);
+ ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL);
+ assert_int_equal(ret, LY_EVALID);
+ LY_ARRAY_FREE(attrs);
+ logbuf_assert("Missing mandatory attribute value of mandatory element. Line number 1.");
st->finished_correctly = true;
}
@@ -826,6 +888,7 @@
cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_yin_parse_yangversion, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(test_yin_parse_mandatory, setup_f, teardown_f),
cmocka_unit_test(test_yin_match_argument_name),
};