data tree REFACTOR add canonical getter for lyd_value
- inline other canonical getter functions.
Signed-off-by: Christian Hopps <chopps@labn.net>
diff --git a/src/tree_data.h b/src/tree_data.h
index 263a15d..06ee812 100644
--- a/src/tree_data.h
+++ b/src/tree_data.h
@@ -978,12 +978,37 @@
int lyd_lyb_data_length(const char *data);
/**
+ * @brief Get the (canonical) value of a lyd_value.
+ *
+ * @param[in] ctx Context for the value
+ * @param[in] value value node to use.
+ * @return Canonical value.
+ */
+const char *lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value);
+
+/**
* @brief Get the (canonical) value of a data node.
*
* @param[in] node Data node to use.
* @return Canonical value.
*/
-const char *lyd_get_value(const struct lyd_node *node);
+static inline const char *
+lyd_get_value(const struct lyd_node *node)
+{
+ if (!node) {
+ return NULL;
+ }
+
+ if (!node->schema) {
+ return ((struct lyd_node_opaq *)node)->value;
+ } else if (node->schema->nodetype & LYD_NODE_TERM) {
+ const struct lyd_value *value = &((struct lyd_node_term *)node)->value;
+ return value->_canonical
+ ? value->_canonical
+ : lyd_value_get_canonical(LYD_CTX(node), value);
+ }
+ return NULL;
+}
/**
* @brief Get the (canonical) value of a metadata node.
@@ -991,7 +1016,17 @@
* @param[in] meta Metadata node to use.
* @return Canonical value.
*/
-const char *lyd_get_meta_value(const struct lyd_meta *meta);
+static inline const char *
+lyd_get_meta_value(const struct lyd_meta *meta)
+{
+ if (meta) {
+ const struct lyd_value *value = &meta->value;
+ return value->_canonical
+ ? value->_canonical
+ : lyd_value_get_canonical(meta->annotation->module->ctx, value);
+ }
+ return NULL;
+}
/**
* @brief Get anydata string value.