plugin types REFACTOR remove STORE and CANONIZE flags
The behavior was changed as if both of these
flags are always set. Also, types now always
include their canonical/JSON string value.
Finally, default value resolution was also
refactored to avoid some code duplication.
This change was most evident in deviations,
which were then significantly refactored.
diff --git a/src/diff.c b/src/diff.c
index 1ddeee4..5b75499 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -236,7 +236,6 @@
char **orig_value, char **key, char **orig_key)
{
const struct lysc_node *schema;
- int dynamic;
size_t buflen, bufused, first_pos, second_pos;
struct lyd_diff_userord *userord_item;
@@ -307,11 +306,8 @@
/* value */
if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
if (second_pos) {
- *value = (char *)lyd_value2str((struct lyd_node_term *)userord_item->inst[second_pos - 1], &dynamic);
- if (!dynamic) {
- *value = strdup(*value);
- LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
- }
+ *value = strdup(LYD_CANONICAL(userord_item->inst[second_pos - 1]));
+ LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
} else {
*value = strdup("");
LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
@@ -321,11 +317,8 @@
/* orig-value */
if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
if (first_pos) {
- *orig_value = (char *)lyd_value2str((struct lyd_node_term *)userord_item->inst[first_pos - 1], &dynamic);
- if (!dynamic) {
- *orig_value = strdup(*orig_value);
- LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
- }
+ *orig_value = strdup(LYD_CANONICAL(userord_item->inst[first_pos - 1]));
+ LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
} else {
*orig_value = strdup("");
LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
@@ -405,7 +398,6 @@
const char **orig_default, char **orig_value)
{
const struct lysc_node *schema;
- int dynamic;
assert(first || second);
@@ -475,11 +467,8 @@
/* orig-value */
if ((schema->nodetype == LYS_LEAF) && (*op == LYD_DIFF_OP_REPLACE)) {
/* leaf */
- *orig_value = (char *)lyd_value2str((struct lyd_node_term *)first, &dynamic);
- if (!dynamic) {
- *orig_value = strdup(*orig_value);
- LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
- }
+ *orig_value = strdup(LYD_CANONICAL(first));
+ LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
}
return LY_SUCCESS;
@@ -748,13 +737,11 @@
struct lyd_meta *meta = NULL;
const struct lyd_node *diff_parent;
const char *str;
- int dynamic;
for (diff_parent = diff_node; diff_parent; diff_parent = (struct lyd_node *)diff_parent->parent) {
LY_LIST_FOR(diff_parent->meta, meta) {
if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
- str = lyd_meta2str(meta, &dynamic);
- assert(!dynamic);
+ str = meta->value.canonical;
if ((str[0] == 'r') && (diff_parent != diff_node)) {
/* we do not care about this operation if it's in our parent */
continue;
@@ -871,7 +858,6 @@
const char *str_val;
enum lyd_diff_op op;
struct lyd_meta *meta;
- int dynamic;
const struct ly_ctx *ctx = LYD_NODE_CTX(diff_node);
/* read all the valid attributes */
@@ -890,7 +876,7 @@
/* get "key" or "value" metadata string value */
meta = lyd_find_meta(diff_node->meta, NULL, diff_node->schema->nodetype == LYS_LIST ? "yang:key" : "yang:value");
LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
- str_val = lyd_meta2str(meta, &dynamic);
+ str_val = meta->value.canonical;
/* insert/move the node */
if (str_val[0]) {
@@ -898,9 +884,6 @@
} else {
ret = lyd_diff_insert(first_node, parent_node, match, NULL);
}
- if (dynamic) {
- free((char *)str_val);
- }
if (ret) {
if (op == LYD_DIFF_OP_CREATE) {
lyd_free_tree(match);
@@ -969,11 +952,7 @@
lyd_diff_find_node(*first_node, diff_node, &match);
/* update its value */
- str_val = lyd_value2str((struct lyd_node_term *)diff_node, &dynamic);
- ret = lyd_change_term(match, str_val);
- if (dynamic) {
- free((char *)str_val);
- }
+ ret = lyd_change_term(match, LYD_CANONICAL(diff_node));
if (ret && (ret != LY_EEXIST)) {
LOGINT_RET(ctx);
}
@@ -1108,7 +1087,6 @@
lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
{
LY_ERR ret;
- int dynamic;
const char *str_val, *meta_name;
struct lyd_meta *meta;
const struct lys_module *mod;
@@ -1140,26 +1118,16 @@
LOGINT_RET(LYD_NODE_CTX(src_diff));
}
- /* get leaf value */
- str_val = lyd_value2str((struct lyd_node_term *)src_diff, &dynamic);
-
/* modify the node value */
- ret = lyd_change_term(diff_match, str_val);
- if (dynamic) {
- free((char *)str_val);
- }
- if (ret) {
+ if (lyd_change_term(diff_match, LYD_CANONICAL(src_diff))) {
LOGINT_RET(LYD_NODE_CTX(src_diff));
}
/* compare values whether there is any change at all */
meta = lyd_find_meta(diff_match->meta, mod, "orig-value");
LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(diff_match)), LY_EINT);
- str_val = lyd_meta2str(meta, &dynamic);
+ str_val = meta->value.canonical;
ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val), NULL);
- if (dynamic) {
- free((char *)str_val);
- }
if (!ret) {
/* values are the same, remove orig-value meta and set oper to NONE */
lyd_free_meta_single(meta);
@@ -1222,9 +1190,6 @@
{
struct lyd_node *child;
const struct lysc_node_leaf *sleaf;
- const char *str_val;
- int dynamic;
- LY_ERR ret;
switch (cur_op) {
case LYD_DIFF_OP_DELETE:
@@ -1261,20 +1226,10 @@
if (lyd_compare_single(diff_match, src_diff, 0)) {
/* current value is the previous one (meta) */
- str_val = lyd_value2str((struct lyd_node_term *)diff_match, &dynamic);
- ret = lyd_new_meta(diff_match, NULL, "yang:orig-value", str_val, NULL);
- if (dynamic) {
- free((char *)str_val);
- }
- LY_CHECK_RET(ret);
+ LY_CHECK_RET(lyd_new_meta(diff_match, NULL, "yang:orig-value", LYD_CANONICAL(diff_match), NULL));
/* update the value itself */
- str_val = lyd_value2str((struct lyd_node_term *)src_diff, &dynamic);
- ret = lyd_change_term(diff_match, str_val);
- if (dynamic) {
- free((char *)str_val);
- }
- LY_CHECK_RET(ret);
+ LY_CHECK_RET(lyd_change_term(diff_match, LYD_CANONICAL(src_diff)));
}
if (diff_match->schema->nodetype & LYD_NODE_TERM) {
@@ -1365,7 +1320,6 @@
struct lyd_node *child;
const struct lys_module *mod;
const char *str;
- int dynamic;
assert(diff);
@@ -1405,8 +1359,7 @@
/* check whether at least the default flags are different */
meta = lyd_find_meta(diff->meta, mod, "orig-default");
assert(meta);
- str = lyd_meta2str(meta, &dynamic);
- assert(!dynamic);
+ str = meta->value.canonical;
/* if previous and current dflt flags are the same, this node is redundant */
if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) {
@@ -1557,17 +1510,18 @@
{
LY_ERR ret = LY_SUCCESS;
struct lyd_meta *meta;
- const char *val1 = NULL, *val2 = NULL;
- int dyn1 = 0, dyn2 = 0, flags;
+ const char *val1 = NULL;
+ char *val2;
+ int flags;
meta = lyd_find_meta(leaf->meta, mod, "orig-value");
LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(leaf)), LY_EINT);
/* orig-value */
- val1 = lyd_meta2str(meta, &dyn1);
+ val1 = meta->value.canonical;
/* current value */
- val2 = lyd_value2str((struct lyd_node_term *)leaf, &dyn2);
+ val2 = strdup(LYD_CANONICAL(leaf));
/* switch values, keep default flag */
flags = leaf->flags;
@@ -1576,12 +1530,7 @@
LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup);
cleanup:
- if (dyn1) {
- free((char *)val1);
- }
- if (dyn2) {
- free((char *)val2);
- }
+ free(val2);
return ret;
}
@@ -1589,8 +1538,7 @@
lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod)
{
struct lyd_meta *meta;
- const char *val;
- int dyn, flag1, flag2;
+ int flag1, flag2;
meta = lyd_find_meta(node->meta, mod, "orig-default");
if (!meta) {
@@ -1599,9 +1547,7 @@
}
/* orig-default */
- val = lyd_meta2str(meta, &dyn);
- assert(!dyn);
- if (!strcmp(val, "true")) {
+ if (meta->value.boolean) {
flag1 = LYD_DEFAULT;
} else {
flag1 = 0;
@@ -1623,8 +1569,8 @@
{
LY_ERR ret = LY_SUCCESS;
struct lyd_meta *meta1, *meta2;
- const char *val1 = NULL, *val2 = NULL;
- int dyn1 = 0, dyn2 = 0;
+ const char *val1 = NULL;
+ char *val2 = NULL;
meta1 = lyd_find_meta(node->meta, mod, name1);
LY_CHECK_ERR_RET(!meta1, LOGINT(LYD_NODE_CTX(node)), LY_EINT);
@@ -1633,22 +1579,17 @@
LY_CHECK_ERR_RET(!meta2, LOGINT(LYD_NODE_CTX(node)), LY_EINT);
/* value1 */
- val1 = lyd_meta2str(meta1, &dyn1);
+ val1 = meta1->value.canonical;
/* value2 */
- val2 = lyd_meta2str(meta2, &dyn2);
+ val2 = strdup(meta2->value.canonical);
/* switch values */
LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup);
LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup);
cleanup:
- if (dyn1) {
- free((char *)val1);
- }
- if (dyn2) {
- free((char *)val2);
- }
+ free(val2);
return ret;
}