Skip to content

Commit

Permalink
union: propagate apptag error if all non-NULL errors use the same apptag
Browse files Browse the repository at this point in the history
If a union references various different leafref paths, and all leafref
paths are invalid, it is nice to have the apptag returned as
instance-required.  There may be other use cases...

Signed-off-by: Brad House <[email protected]>
  • Loading branch information
bradh352 committed Feb 25, 2025
1 parent 76a439f commit 135e140
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/plugins_types/union.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ union_find_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, struct
}

if (u == LY_ARRAY_COUNT(type_u->types)) {
const char *apptag = NULL;
ly_bool use_apptag = 0;

/* create the full error */
if (subvalue->format == LY_VALUE_LYB) {
msg_len = asprintf(&msg, "Invalid LYB union value - no matching subtype found:\n");
Expand All @@ -306,12 +309,22 @@ union_find_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, struct
continue;
}

/* Capture an apptag if one exists. We will propagate it if all set
* apptags match, ignoring any NULL apptags. */
if (apptag == NULL) {
use_apptag = 1;
apptag = errs[u]->apptag;
} else if (apptag && errs[u]->apptag && (strcmp(errs[u]->apptag, apptag) != 0)) {
use_apptag = 0;
apptag = NULL;
}

msg = ly_realloc(msg, msg_len + 4 + strlen(type_u->types[u]->plugin->id) + 2 + strlen(errs[u]->msg) + 2);
LY_CHECK_ERR_GOTO(!msg, ret = LY_EMEM, cleanup);
msg_len += sprintf(msg + msg_len, " %s: %s\n", type_u->types[u]->plugin->id, errs[u]->msg);
}

ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "%s", msg);
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, use_apptag ? strdup(apptag) : NULL, "%s", msg);
} else if (type_idx) {
*type_idx = u;
}
Expand Down

0 comments on commit 135e140

Please sign in to comment.