yin parser CHANGE add support for typedef element
diff --git a/src/parser_yin.c b/src/parser_yin.c
index a94edd3..8532b9a 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -1215,6 +1215,46 @@
}
/**
+ * @brief Parse typedef 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] typedef_meta Meta information about node parent and typedefs to add to.
+ *
+ * @return LY_ERR values.
+ */
+static LY_ERR
+yin_parse_typedef(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
+ struct typedef_meta *typedef_meta)
+{
+ struct lysp_tpdf *tpdf;
+ LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *typedef_meta->typedefs, tpdf, LY_EMEM);
+
+ /* parse argument */
+ LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, YANG_TYPEDEF));
+
+ /* parse content */
+ struct yin_subelement subelems[7] = {
+ {YANG_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
+ {YANG_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
+ {YANG_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
+ {YANG_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
+ {YANG_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
+ {YANG_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
+ {YANG_CUSTOM, NULL, 0},
+ };
+ LY_CHECK_RET(yin_parse_content(ctx, subelems, 7, data, YANG_TYPEDEF, NULL, &tpdf->exts));
+
+ /* store data for collision check */
+ if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
+ ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0);
+ }
+
+ return LY_SUCCESS;
+}
+
+/**
* @brief Map keyword type to substatement info.
*
* @param[in] kw Keyword type.
@@ -1569,6 +1609,7 @@
ret = yin_parse_type(ctx, attrs, data, type);
break;
case YANG_TYPEDEF:
+ ret = yin_parse_typedef(ctx, attrs, data, (struct typedef_meta *)subelem->dest);
break;
case YANG_UNIQUE:
ret = yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subelem->dest,
diff --git a/src/parser_yin.h b/src/parser_yin.h
index b356991..a8674b9 100644
--- a/src/parser_yin.h
+++ b/src/parser_yin.h
@@ -57,8 +57,10 @@
};
struct yin_parser_ctx {
- struct lyxml_context xml_ctx; /**< context for xml parser */
+ struct ly_set tpdfs_nodes;
+ struct ly_set grps_nodes;
uint8_t mod_version; /**< module's version */
+ struct lyxml_context xml_ctx; /**< context for xml parser */
};
/* flags to encode cardinality of subelement */
@@ -88,12 +90,18 @@
const char **argument; /**< Argument value */
};
-/* Maet information passed to functions working with tree schema such as yin_parse_any */
+/* Meta 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 */
};
+/* Meta information passed to yin_parse_typedef */
+struct typedef_meta {
+ struct lysp_node *parent; /**< parent node */
+ struct lysp_tpdf **typedefs; /**< [Sized array](@ref sizedarrays) of typedefs */
+};
+
/**
* @brief Match argument name.
*
diff --git a/src/tree_schema_free.c b/src/tree_schema_free.c
index f6aa3d3..ae23ff0 100644
--- a/src/tree_schema_free.c
+++ b/src/tree_schema_free.c
@@ -155,7 +155,7 @@
}
}
-static void
+void
lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
{
FREE_STRING(ctx, tpdf->name);
diff --git a/src/tree_schema_internal.h b/src/tree_schema_internal.h
index 76e5413..e90f8cc 100644
--- a/src/tree_schema_internal.h
+++ b/src/tree_schema_internal.h
@@ -89,12 +89,12 @@
* @brief internal context for schema parsers
*/
struct lys_parser_ctx {
- struct ly_ctx *ctx;
- uint64_t line; /**< line number */
struct ly_set tpdfs_nodes;
struct ly_set grps_nodes;
- uint64_t indent; /**< current position on the line for YANG indentation */
uint8_t mod_version; /**< module's version */
+ struct ly_ctx *ctx;
+ uint64_t line; /**< line number */
+ uint64_t indent; /**< current position on the line for YANG indentation */
};
/**
diff --git a/tests/src/test_parser_yin.c b/tests/src/test_parser_yin.c
index 5aeb660..1494e64 100644
--- a/tests/src/test_parser_yin.c
+++ b/tests/src/test_parser_yin.c
@@ -33,6 +33,7 @@
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);
+void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
struct state {
struct ly_ctx *ctx;
@@ -2191,6 +2192,50 @@
st->finished_correctly = true;
}
+static void
+test_typedef_elem(void **state)
+{
+ struct state *st = *state;
+ const char *data;
+ struct lysp_tpdf *tpdfs = NULL;
+ struct typedef_meta typdef_meta = {NULL, &tpdfs};
+
+ data = ELEMENT_WRAPPER_START
+ "<typedef name=\"tpdf-name\">"
+ "<default value=\"def-val\"/>"
+ "<description><text>desc-text</text></description>"
+ "<reference><text>ref-text</text></reference>"
+ "<status value=\"current\"/>"
+ "<type name=\"type\"/>"
+ "<units name=\"uni\"/>"
+ "</typedef>"
+ ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
+ assert_string_equal(tpdfs[0].dflt, "def-val");
+ assert_string_equal(tpdfs[0].dsc, "desc-text");
+ assert_null(tpdfs[0].exts);
+ assert_string_equal(tpdfs[0].name, "tpdf-name");
+ assert_string_equal(tpdfs[0].ref, "ref-text");
+ assert_string_equal(tpdfs[0].type.name, "type");
+ assert_string_equal(tpdfs[0].units, "uni");
+ assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
+ FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
+ tpdfs = NULL;
+
+ data = ELEMENT_WRAPPER_START
+ "<typedef name=\"tpdf-name\">"
+ "<type name=\"type\"/>"
+ "</typedef>"
+ ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
+ assert_string_equal(tpdfs[0].name, "tpdf-name");
+ assert_string_equal(tpdfs[0].type.name, "type");
+ FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
+ tpdfs = NULL;
+
+ st->finished_correctly = true;
+}
+
int
main(void)
{
@@ -2244,6 +2289,7 @@
cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
+ cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
};