printer tree FEATURE printing of node name prefix
The prefix (from <prefix>:<name>) can be only printed if the
LY_CTX_SET_PRIV_PARSED flag is set.
diff --git a/src/printer_tree.c b/src/printer_tree.c
index 0eff496..a49b84c 100644
--- a/src/printer_tree.c
+++ b/src/printer_tree.c
@@ -357,7 +357,8 @@
*/
struct trt_node_name {
trt_node_type type; /**< Type of the node relevant for printing. */
- const char *module_prefix; /**< Prefix defined in the module where the node is defined. */
+ const char *module_prefix; /**< If the node is augmented into the tree from another module,
+ so this is the prefix of that module. */
const char *str; /**< Name of the node. */
};
@@ -2812,7 +2813,10 @@
/* set type of the node */
ret.name.type = trop_resolve_node_type(pn, ca.last_list);
- /* TODO: ret.name.module_prefix is not supported right now. */
+ /* The parsed tree is not compiled, so no node can be augmented
+ * from another module. This means that nodes from the parsed tree
+ * will never have the prefix.
+ */
ret.name.module_prefix = NULL;
/* set node's name */
@@ -3144,6 +3148,29 @@
}
/**
+ * @brief Resolve prefix (<prefix>:<name>) of node that has been
+ * placed from another module via an augment statement.
+ *
+ * @param[in] cn is current compiled node.
+ * @param[in] current_compiled_module is module whose nodes are
+ * currently being printed.
+ * @return Prefix of foreign module or NULL.
+ */
+static const char *
+troc_resolve_node_prefix(const struct lysc_node *cn, const struct lysc_module *current_compiled_module)
+{
+ const struct lys_module *node_module;
+ const char *ret = NULL;
+
+ node_module = cn->module;
+ if (node_module->compiled != current_compiled_module) {
+ ret = node_module->prefix;
+ }
+
+ return ret;
+}
+
+/**
* @brief Transformation of current lysc_node to struct trt_node.
* @param[in] ca is not used.
* @param[in] tc is context of the tree.
@@ -3172,8 +3199,8 @@
/* set type of the node */
ret.name.type = troc_resolve_node_type(cn->nodetype, cn->flags);
- /* TODO: ret.name.module_prefix is not supported right now. */
- ret.name.module_prefix = NULL;
+ /* <prefix> */
+ ret.name.module_prefix = troc_resolve_node_prefix(cn, tc->cmod);
/* set node's name */
ret.name.str = cn->name;
diff --git a/tests/utests/schema/test_printer_tree.c b/tests/utests/schema/test_printer_tree.c
index e678cbf..bf141fe 100644
--- a/tests/utests/schema/test_printer_tree.c
+++ b/tests/utests/schema/test_printer_tree.c
@@ -37,6 +37,7 @@
base_sections(void **state)
{
TEST_LOCAL_SETUP;
+ const struct lys_module *modxx;
orig =
"module a01xx {\n"
@@ -47,7 +48,7 @@
" container d;\n"
"}\n";
- UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
+ UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &modxx);
/* module with import statement */
orig =
@@ -128,6 +129,21 @@
TEST_LOCAL_PRINT(mod, 72);
assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
assert_string_equal(printed, expect);
+
+ ly_out_reset(UTEST_OUT);
+
+ /* from pyang */
+ expect =
+ "module: a01xx\n"
+ " +--rw c\n"
+ " | +--rw x:e\n"
+ " +--rw d\n"
+ " +--rw x:f\n";
+
+ TEST_LOCAL_PRINT(modxx, 72);
+ assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
+ assert_string_equal(printed, expect);
+
ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
TEST_LOCAL_TEARDOWN;