Skip to content

Commit fb48e81

Browse files
committed
diff BUGFIX reverse userord metadata
1 parent 106e64f commit fb48e81

File tree

1 file changed

+75
-26
lines changed

1 file changed

+75
-26
lines changed

src/diff.c

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,27 +2975,28 @@ lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const
29752975
}
29762976

29772977
/**
2978-
* @brief Remove specific operation from all the nodes in a subtree.
2978+
* @brief Rename diff meta by deleting the old meta and creating a new one.
29792979
*
2980-
* @param[in] diff Diff subtree to process.
2981-
* @param[in] op Only expected operation.
2980+
* @param[in] node Parent meta node.
2981+
* @param[in] mod Meta module.
2982+
* @param[in] src_name Current meta name.
2983+
* @param[in] trg_name New meta name.
29822984
* @return LY_ERR value.
29832985
*/
29842986
static LY_ERR
2985-
lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op)
2987+
lyd_diff_rename_meta(struct lyd_node *node, const struct lys_module *mod, const char *src_name, const char *trg_name)
29862988
{
2987-
struct lyd_node *elem;
29882989
struct lyd_meta *meta;
29892990

2990-
LYD_TREE_DFS_BEGIN(diff, elem) {
2991-
meta = lyd_find_meta(elem->meta, NULL, "yang:operation");
2992-
if (meta) {
2993-
LY_CHECK_ERR_RET(lyd_diff_str2op(lyd_get_meta_value(meta)) != op, LOGINT(LYD_CTX(diff)), LY_EINT);
2994-
lyd_free_meta_single(meta);
2995-
}
2991+
/* find the old meta */
2992+
meta = lyd_find_meta(node->meta, mod, src_name);
2993+
LY_CHECK_ERR_RET(!meta, LOGERR_META(LYD_CTX(node), src_name, node), LY_EINVAL);
29962994

2997-
LYD_TREE_DFS_END(diff, elem);
2998-
}
2995+
/* create the new meta */
2996+
LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), node, mod, trg_name, lyd_get_meta_value(meta), LYD_NEW_VAL_STORE_ONLY, NULL));
2997+
2998+
/* delete the old meta */
2999+
lyd_free_meta_single(meta);
29993000

30003001
return LY_SUCCESS;
30013002
}
@@ -3149,7 +3150,6 @@ lyd_diff_reverse_siblings_r(struct lyd_node *sibling, const struct lys_module *y
31493150
struct lyd_node *iter, *iter2, **userord = NULL;
31503151
const struct lysc_node *userord_schema = NULL;
31513152
enum lyd_diff_op op;
3152-
ly_bool recursive;
31533153

31543154
LY_LIST_FOR(sibling, iter) {
31553155
/* skip all keys */
@@ -3166,30 +3166,81 @@ lyd_diff_reverse_siblings_r(struct lyd_node *sibling, const struct lys_module *y
31663166
/* find operation attribute, if any */
31673167
LY_CHECK_GOTO(rc = lyd_diff_get_op(iter, &op, NULL), cleanup);
31683168

3169-
recursive = 1;
31703169
switch (op) {
31713170
case LYD_DIFF_OP_CREATE:
31723171
/* reverse create to delete */
31733172
LY_CHECK_GOTO(rc = lyd_diff_change_op(iter, LYD_DIFF_OP_DELETE), cleanup);
31743173

3175-
/* check all the children for the same operation, nothing else is expected */
3176-
LY_LIST_FOR(lyd_child(iter), iter2) {
3177-
lyd_diff_reverse_remove_op_r(iter2, LYD_DIFF_OP_CREATE);
3174+
switch (iter->schema->nodetype) {
3175+
case LYS_LEAF:
3176+
case LYS_ANYXML:
3177+
case LYS_ANYDATA:
3178+
/* nothing to do */
3179+
break;
3180+
case LYS_LEAFLIST:
3181+
/* leaf-list create -> delete */
3182+
if (lysc_is_dup_inst_list(iter->schema)) {
3183+
LY_CHECK_GOTO(rc = lyd_diff_rename_meta(iter, yang_mod, "position", "orig-position"), cleanup);
3184+
} else {
3185+
LY_CHECK_GOTO(rc = lyd_diff_rename_meta(iter, yang_mod, "value", "orig-value"), cleanup);
3186+
}
3187+
break;
3188+
case LYS_LIST:
3189+
/* list create -> delete */
3190+
if (lysc_is_dup_inst_list(iter->schema)) {
3191+
LY_CHECK_GOTO(rc = lyd_diff_rename_meta(iter, yang_mod, "position", "orig-position"), cleanup);
3192+
} else {
3193+
LY_CHECK_GOTO(rc = lyd_diff_rename_meta(iter, yang_mod, "key", "orig-key"), cleanup);
3194+
}
3195+
break;
3196+
default:
3197+
LOGINT(LYD_CTX(iter));
3198+
rc = LY_EINT;
3199+
goto cleanup;
31783200
}
31793201

3180-
recursive = 0;
3202+
/* keep the operation for all the children, handled recursively */
3203+
LY_LIST_FOR(lyd_child_no_keys(iter), iter2) {
3204+
LY_CHECK_GOTO(rc = lyd_diff_change_op(iter2, LYD_DIFF_OP_CREATE), cleanup);
3205+
}
31813206
break;
31823207

31833208
case LYD_DIFF_OP_DELETE:
31843209
/* reverse delete to create */
31853210
LY_CHECK_GOTO(rc = lyd_diff_change_op(iter, LYD_DIFF_OP_CREATE), cleanup);
31863211

3187-
/* check all the children for the same operation, nothing else is expected */
3188-
LY_LIST_FOR(lyd_child(iter), iter2) {
3189-
lyd_diff_reverse_remove_op_r(iter2, LYD_DIFF_OP_DELETE);
3212+
switch (iter->schema->nodetype) {
3213+
case LYS_LEAF:
3214+
case LYS_ANYXML:
3215+
case LYS_ANYDATA:
3216+
/* nothing to do */
3217+
break;
3218+
case LYS_LEAFLIST:
3219+
/* leaf-list delete -> create */
3220+
if (lysc_is_dup_inst_list(iter->schema)) {
3221+
LY_CHECK_GOTO(rc = lyd_diff_rename_meta(iter, yang_mod, "orig-position", "position"), cleanup);
3222+
} else {
3223+
LY_CHECK_GOTO(rc = lyd_diff_rename_meta(iter, yang_mod, "orig-value", "value"), cleanup);
3224+
}
3225+
break;
3226+
case LYS_LIST:
3227+
/* list dlete -> create */
3228+
if (lysc_is_dup_inst_list(iter->schema)) {
3229+
LY_CHECK_GOTO(rc = lyd_diff_rename_meta(iter, yang_mod, "orig-position", "position"), cleanup);
3230+
} else {
3231+
LY_CHECK_GOTO(rc = lyd_diff_rename_meta(iter, yang_mod, "orig-key", "key"), cleanup);
3232+
}
3233+
break;
3234+
default:
3235+
LOGINT(LYD_CTX(iter));
3236+
rc = LY_EINT;
3237+
goto cleanup;
31903238
}
31913239

3192-
recursive = 0;
3240+
/* keep the operation for all the children, handled recursively */
3241+
LY_LIST_FOR(lyd_child_no_keys(iter), iter2) {
3242+
LY_CHECK_GOTO(rc = lyd_diff_change_op(iter2, LYD_DIFF_OP_DELETE), cleanup);
3243+
}
31933244
break;
31943245

31953246
case LYD_DIFF_OP_REPLACE:
@@ -3246,9 +3297,7 @@ lyd_diff_reverse_siblings_r(struct lyd_node *sibling, const struct lys_module *y
32463297
LY_CHECK_GOTO(rc = lyd_diff_reverse_metadata_diff(iter), cleanup);
32473298

32483299
/* revursively reverse all descendants */
3249-
if (recursive) {
3250-
LY_CHECK_GOTO(rc = lyd_diff_reverse_siblings_r(lyd_child(iter), yang_mod), cleanup);
3251-
}
3300+
LY_CHECK_GOTO(rc = lyd_diff_reverse_siblings_r(lyd_child(iter), yang_mod), cleanup);
32523301

32533302
if (lysc_is_userordered(iter->schema)) {
32543303
/* special user-ordered nodes processing */

0 commit comments

Comments
 (0)