Skip to content

Commit

Permalink
Reworked according to the comments on PR
Browse files Browse the repository at this point in the history
This patch adjust the logic as follows:
 - introduces LY_CTX_BASIC_PLUGINS_ONLY flag instead of LY_CTX_STORE_INVALID_DATA
 - introduces LYPLG_TYPE_STORE_ONLY flag on plugin level
 - introduces store_only option on many lyd_new_* APIs
 - Adjust the LYD_PARSE_ONLY usage to imply usage of LYPLG_TYPE_STORE_ONLY
  • Loading branch information
steweg committed Feb 16, 2024
1 parent 2ae2037 commit 8e216c9
Show file tree
Hide file tree
Showing 29 changed files with 319 additions and 301 deletions.
50 changes: 24 additions & 26 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ ly_ctx_new(const char *search_dir, uint16_t options, struct ly_ctx **new_ctx)
struct ly_in *in = NULL;
LY_ERR rc = LY_SUCCESS;
struct lys_glob_unres unres = {0};
ly_bool no_advanced_builtin = 0;
ly_bool no_advanced_builtin;

LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL);

Expand All @@ -285,9 +285,7 @@ ly_ctx_new(const char *search_dir, uint16_t options, struct ly_ctx **new_ctx)
lydict_init(&ctx->dict);

/* plugins */
if (options & LY_CTX_STORE_INVALID_DATA) {
no_advanced_builtin = 1;
}
no_advanced_builtin = (options & LY_CTX_BASIC_PLUGINS_ONLY) ? 1 : 0;
LY_CHECK_ERR_GOTO(lyplg_init(no_advanced_builtin), LOGINT(NULL); rc = LY_EINT, cleanup);

if (options & LY_CTX_LEAFREF_LINKING) {
Expand Down Expand Up @@ -1089,7 +1087,7 @@ ylib_feature(struct lyd_node *parent, const struct lysp_module *pmod)
continue;
}

LY_CHECK_RET(lyd_new_term(parent, NULL, "feature", f->name, 0, NULL));
LY_CHECK_RET(lyd_new_term(parent, NULL, "feature", f->name, 0, 0, NULL));
}

LY_ARRAY_FOR(pmod->includes, u) {
Expand All @@ -1098,7 +1096,7 @@ ylib_feature(struct lyd_node *parent, const struct lysp_module *pmod)
continue;
}

LY_CHECK_RET(lyd_new_term(parent, NULL, "feature", f->name, 0, NULL));
LY_CHECK_RET(lyd_new_term(parent, NULL, "feature", f->name, 0, 0, NULL));
}
}

Expand All @@ -1120,9 +1118,9 @@ ylib_deviation(struct lyd_node *parent, const struct lys_module *cur_mod, ly_boo
mod = cur_mod->deviated_by[i];

if (bis) {
LY_CHECK_RET(lyd_new_term(parent, NULL, "deviation", mod->name, 0, NULL));
LY_CHECK_RET(lyd_new_term(parent, NULL, "deviation", mod->name, 0, 0, NULL));
} else {
LY_CHECK_RET(lyd_new_list(parent, NULL, "deviation", 0, NULL, mod->name,
LY_CHECK_RET(lyd_new_list(parent, NULL, "deviation", 0, 0, NULL, mod->name,
(mod->parsed->revs ? mod->parsed->revs[0].date : "")));
}
}
Expand All @@ -1144,21 +1142,21 @@ ylib_submodules(struct lyd_node *parent, const struct lysp_module *pmod, ly_bool
submod = pmod->includes[i].submodule;

if (bis) {
LY_CHECK_RET(lyd_new_list(parent, NULL, "submodule", 0, &cont, submod->name));
LY_CHECK_RET(lyd_new_list(parent, NULL, "submodule", 0, 0, &cont, submod->name));

if (submod->revs) {
LY_CHECK_RET(lyd_new_term(cont, NULL, "revision", submod->revs[0].date, 0, NULL));
LY_CHECK_RET(lyd_new_term(cont, NULL, "revision", submod->revs[0].date, 0, 0, NULL));
}
} else {
LY_CHECK_RET(lyd_new_list(parent, NULL, "submodule", 0, &cont, submod->name,
LY_CHECK_RET(lyd_new_list(parent, NULL, "submodule", 0, 0, &cont, submod->name,
(submod->revs ? submod->revs[0].date : "")));
}

if (submod->filepath) {
r = asprintf(&str, "file://%s", submod->filepath);
LY_CHECK_ERR_RET(r == -1, LOGMEM(pmod->mod->ctx), LY_EMEM);

ret = lyd_new_term(cont, NULL, bis ? "location" : "schema", str, 0, NULL);
ret = lyd_new_term(cont, NULL, bis ? "location" : "schema", str, 0, 0, NULL);
free(str);
LY_CHECK_RET(ret);
}
Expand Down Expand Up @@ -1197,7 +1195,7 @@ ly_ctx_get_yanglib_data(const struct ly_ctx *ctx, struct lyd_node **root_p, cons

if (bis) {
LY_CHECK_GOTO(ret = lyd_new_inner(NULL, mod, "yang-library", 0, &root_bis), error);
LY_CHECK_GOTO(ret = lyd_new_list(root_bis, NULL, "module-set", 0, &set_bis, "complete"), error);
LY_CHECK_GOTO(ret = lyd_new_list(root_bis, NULL, "module-set", 0, 0, &set_bis, "complete"), error);
}

for (i = 0; i < ctx->list.count; ++i) {
Expand All @@ -1210,21 +1208,21 @@ ly_ctx_get_yanglib_data(const struct ly_ctx *ctx, struct lyd_node **root_p, cons
/*
* deprecated legacy
*/
LY_CHECK_GOTO(ret = lyd_new_list(root, NULL, "module", 0, &cont, mod->name,
LY_CHECK_GOTO(ret = lyd_new_list(root, NULL, "module", 0, 0, &cont, mod->name,
(mod->parsed->revs ? mod->parsed->revs[0].date : "")), error);

/* schema */
if (mod->filepath) {
r = asprintf(&str, "file://%s", mod->filepath);
LY_CHECK_ERR_GOTO(r == -1, LOGMEM(ctx); ret = LY_EMEM, error);

ret = lyd_new_term(cont, NULL, "schema", str, 0, NULL);
ret = lyd_new_term(cont, NULL, "schema", str, 0, 0, NULL);
free(str);
LY_CHECK_GOTO(ret, error);
}

/* namespace */
LY_CHECK_GOTO(ret = lyd_new_term(cont, NULL, "namespace", mod->ns, 0, NULL), error);
LY_CHECK_GOTO(ret = lyd_new_term(cont, NULL, "namespace", mod->ns, 0, 0, NULL), error);

/* feature leaf-list */
LY_CHECK_GOTO(ret = ylib_feature(cont, mod->parsed), error);
Expand All @@ -1234,7 +1232,7 @@ ly_ctx_get_yanglib_data(const struct ly_ctx *ctx, struct lyd_node **root_p, cons

/* conformance-type */
LY_CHECK_GOTO(ret = lyd_new_term(cont, NULL, "conformance-type", mod->implemented ? "implement" : "import", 0,
NULL), error);
0, NULL), error);

/* submodule list */
LY_CHECK_GOTO(ret = ylib_submodules(cont, mod->parsed, 0), error);
Expand All @@ -1245,25 +1243,25 @@ ly_ctx_get_yanglib_data(const struct ly_ctx *ctx, struct lyd_node **root_p, cons
if (bis) {
/* name and revision */
if (mod->implemented) {
LY_CHECK_GOTO(ret = lyd_new_list(set_bis, NULL, "module", 0, &cont, mod->name), error);
LY_CHECK_GOTO(ret = lyd_new_list(set_bis, NULL, "module", 0, 0, &cont, mod->name), error);

if (mod->parsed->revs) {
LY_CHECK_GOTO(ret = lyd_new_term(cont, NULL, "revision", mod->parsed->revs[0].date, 0, NULL), error);
LY_CHECK_GOTO(ret = lyd_new_term(cont, NULL, "revision", mod->parsed->revs[0].date, 0, 0, NULL), error);
}
} else {
LY_CHECK_GOTO(ret = lyd_new_list(set_bis, NULL, "import-only-module", 0, &cont, mod->name,
LY_CHECK_GOTO(ret = lyd_new_list(set_bis, NULL, "import-only-module", 0, 0, &cont, mod->name,
(mod->parsed->revs ? mod->parsed->revs[0].date : "")), error);
}

/* namespace */
LY_CHECK_GOTO(ret = lyd_new_term(cont, NULL, "namespace", mod->ns, 0, NULL), error);
LY_CHECK_GOTO(ret = lyd_new_term(cont, NULL, "namespace", mod->ns, 0, 0, NULL), error);

/* location */
if (mod->filepath) {
r = asprintf(&str, "file://%s", mod->filepath);
LY_CHECK_ERR_GOTO(r == -1, LOGMEM(ctx); ret = LY_EMEM, error);

ret = lyd_new_term(cont, NULL, "location", str, 0, NULL);
ret = lyd_new_term(cont, NULL, "location", str, 0, 0, NULL);
free(str);
LY_CHECK_GOTO(ret, error);
}
Expand All @@ -1284,17 +1282,17 @@ ly_ctx_get_yanglib_data(const struct ly_ctx *ctx, struct lyd_node **root_p, cons
r = vasprintf(&str, content_id_format, ap);
va_end(ap);
LY_CHECK_ERR_GOTO(r == -1, LOGMEM(ctx); ret = LY_EMEM, error);
ret = lyd_new_term(root, NULL, "module-set-id", str, 0, NULL);
ret = lyd_new_term(root, NULL, "module-set-id", str, 0, 0, NULL);
LY_CHECK_ERR_GOTO(ret, free(str), error);

if (bis) {
/* create one complete schema */
LY_CHECK_ERR_GOTO(ret = lyd_new_list(root_bis, NULL, "schema", 0, &cont, "complete"), free(str), error);
LY_CHECK_ERR_GOTO(ret = lyd_new_list(root_bis, NULL, "schema", 0, 0, &cont, "complete"), free(str), error);

LY_CHECK_ERR_GOTO(ret = lyd_new_term(cont, NULL, "module-set", "complete", 0, NULL), free(str), error);
LY_CHECK_ERR_GOTO(ret = lyd_new_term(cont, NULL, "module-set", "complete", 0, 0, NULL), free(str), error);

/* content-id */
LY_CHECK_ERR_GOTO(ret = lyd_new_term(root_bis, NULL, "content-id", str, 0, NULL), free(str), error);
LY_CHECK_ERR_GOTO(ret = lyd_new_term(root_bis, NULL, "content-id", str, 0, 0, NULL), free(str), error);
}
free(str);

Expand Down
11 changes: 4 additions & 7 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,10 @@ struct ly_ctx;
'require-instance false;'. It also enables usage of
[lyd_leafref_get_links](@ref lyd_leafref_get_links) and
[lyd_leafref_link_node_tree](@ref lyd_leafref_link_node_tree) APIs. */
#define LY_CTX_STORE_INVALID_DATA 0x0800 /**< By default, storing of value of given data node is subject to build-in validations
as patterns, length, ranges etc. This option bypass these validations, and allow user
to validate the values manually by additional calls as lyd_validate.
It also supresses usage of advanced built-in plugin types (ipv4-address, ipv6-address etc.),
which expects that value is already valid during storage operation. Instead the value is
processed by basic plungin (usually string) with all its behavioral implications as hex-string
comparison becoming case-sensitive.
#define LY_CTX_BASIC_PLUGINS_ONLY 0x0800 /**< By default, context loads all available built-in plugins. This options prohibits
loading of advanced built-in plugin types (ipv4-address, ipv6-address, etc.). Instead the value is
processed by basic built-in pluings (usually string) with all its behavioral implications as
hex-string comparison becoming case-sensitive.
Change of this flag during the lifetime of context is not fully supported. Once the advanced
plugins are loaded during context creation, they will be used regardless of this flag.
Therefore it is recommended to set this flag during the first context creation. */
Expand Down
34 changes: 17 additions & 17 deletions src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ lyd_diff_add_create_nested_userord(struct lyd_node *node)
}

/* create the metadata */
LY_CHECK_GOTO(rc = lyd_new_meta(NULL, node, NULL, meta_name, meta_val, 0, NULL), cleanup);
LY_CHECK_GOTO(rc = lyd_new_meta(NULL, node, NULL, meta_name, meta_val, 0, 1, NULL), cleanup);

cleanup:
free(dyn);
Expand Down Expand Up @@ -361,7 +361,7 @@ lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_
}

/* set the none operation */
LY_CHECK_RET(lyd_new_meta(NULL, elem, NULL, "yang:operation", "none", 0, NULL));
LY_CHECK_RET(lyd_new_meta(NULL, elem, NULL, "yang:operation", "none", 0, 1, NULL));
}

dup = diff_parent;
Expand Down Expand Up @@ -403,12 +403,12 @@ lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_

/* add parent operation, if any */
if (diff_parent && (diff_parent != dup)) {
LY_CHECK_RET(lyd_new_meta(NULL, diff_parent, NULL, "yang:operation", "none", 0, NULL));
LY_CHECK_RET(lyd_new_meta(NULL, diff_parent, NULL, "yang:operation", "none", 0, 1, NULL));
}
}

/* add subtree operation */
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL));
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:operation", lyd_diff_op2str(op), 0, 1, NULL));

if (op == LYD_DIFF_OP_CREATE) {
/* all nested user-ordered (leaf-)lists need special metadata for create op */
Expand All @@ -422,37 +422,37 @@ lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_

/* orig-default */
if (orig_default) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-default", orig_default, 0, NULL));
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-default", orig_default, 0, 1, NULL));
}

/* orig-value */
if (orig_value) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-value", orig_value, 0, NULL));
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-value", orig_value, 0, 1, NULL));
}

/* key */
if (key) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:key", key, 0, NULL));
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:key", key, 0, 1, NULL));
}

/* value */
if (value) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:value", value, 0, NULL));
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:value", value, 0, 1, NULL));
}

/* position */
if (position) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:position", position, 0, NULL));
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:position", position, 0, 1, NULL));
}

/* orig-key */
if (orig_key) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-key", orig_key, 0, NULL));
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-key", orig_key, 0, 1, NULL));
}

/* orig-position */
if (orig_position) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-position", orig_position, 0, NULL));
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-position", orig_position, 0, 1, NULL));
}

return LY_SUCCESS;
Expand Down Expand Up @@ -1439,7 +1439,7 @@ lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op)
lyd_diff_del_meta(node, "operation");

if (node->schema) {
return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL);
return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, 1, NULL);
} else {
return lyd_new_attr(node, "yang", "operation", lyd_diff_op2str(op), NULL);
}
Expand Down Expand Up @@ -1664,7 +1664,7 @@ lyd_diff_merge_create(struct lyd_node **diff_match, struct lyd_node **diff, enum

/* current value is the previous one (meta) */
LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), *diff_match, NULL, "yang:orig-value",
lyd_get_value(*diff_match), 0, NULL));
lyd_get_value(*diff_match), 0, 1, NULL));

/* update the value itself */
LY_CHECK_RET(lyd_change_term(*diff_match, lyd_get_value(src_diff)));
Expand All @@ -1678,10 +1678,10 @@ lyd_diff_merge_create(struct lyd_node **diff_match, struct lyd_node **diff, enum
to_free = *diff_match;
*diff_match = src_dup;

r = lyd_new_meta(ctx, src_dup, NULL, "yang:orig-value", lyd_get_value(to_free), 0, NULL);
r = lyd_new_meta(ctx, src_dup, NULL, "yang:orig-value", lyd_get_value(to_free), 0, 1, NULL);
lyd_free_tree(to_free);
LY_CHECK_RET(r);
LY_CHECK_RET(lyd_new_meta(ctx, src_dup, NULL, "yang:operation", lyd_diff_op2str(LYD_DIFF_OP_REPLACE), 0, NULL));
LY_CHECK_RET(lyd_new_meta(ctx, src_dup, NULL, "yang:operation", lyd_diff_op2str(LYD_DIFF_OP_REPLACE), 0, 1, NULL));
}
} else {
/* deleted + created -> operation NONE */
Expand All @@ -1692,7 +1692,7 @@ lyd_diff_merge_create(struct lyd_node **diff_match, struct lyd_node **diff, enum
if ((*diff_match)->schema->nodetype & LYD_NODE_TERM) {
/* add orig-dflt metadata */
LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), *diff_match, NULL, "yang:orig-default",
trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
trg_flags & LYD_DEFAULT ? "true" : "false", 0, 1, NULL));

/* update dflt flag itself */
(*diff_match)->flags &= ~LYD_DEFAULT;
Expand Down Expand Up @@ -1742,7 +1742,7 @@ lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, cons
if (diff_match->schema->nodetype & LYD_NODE_TERM) {
/* add orig-default meta because it is expected */
LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
src_diff->flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
src_diff->flags & LYD_DEFAULT ? "true" : "false", 0, 1, NULL));
}
break;
case LYD_DIFF_OP_REPLACE:
Expand Down
11 changes: 9 additions & 2 deletions src/parser_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,12 @@ lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, c
{
LY_ERR r;
ly_bool incomplete;
ly_bool store_only;

if ((r = lyd_create_term(schema, value, value_len, 1, dynamic, format, prefix_data, hints, &incomplete, node))) {
store_only = (lydctx->parse_opts & LYD_PARSE_ONLY) ? 1 : 0;

if ((r = lyd_create_term(schema, value, value_len, 1, store_only, dynamic, format, prefix_data,
hints, &incomplete, node))) {
if (lydctx->data_ctx->ctx != schema->module->ctx) {
/* move errors to the main context */
ly_err_move(schema->module->ctx, (struct ly_ctx *)lydctx->data_ctx->ctx);
Expand All @@ -308,6 +312,7 @@ lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct l
char *dpath = NULL, *path = NULL;
ly_bool incomplete;
struct lyd_meta *first = NULL;
ly_bool store_only;

if (meta && *meta) {
/* remember the first metadata */
Expand All @@ -323,7 +328,9 @@ lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct l
}
LOG_LOCSET(NULL, NULL, path, NULL);

LY_CHECK_GOTO(rc = lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, 1, dynamic, format,
store_only = (lydctx->parse_opts & LYD_PARSE_ONLY) ? 1 : 0;

LY_CHECK_GOTO(rc = lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, 1, store_only, dynamic, format,
prefix_data, hints, ctx_node, 0, &incomplete), cleanup);

if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Expand Down
10 changes: 5 additions & 5 deletions src/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,8 @@ ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_

/* store the value */
LOG_LOCSET(key, NULL, NULL, NULL);
ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaf *)key)->type, val, val_len, 0, NULL,
format, prefix_data, LYD_HINT_DATA, key, NULL);
ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaf *)key)->type, val, val_len, 0, 0,
NULL, format, prefix_data, LYD_HINT_DATA, key, NULL);
LOG_LOCBACK(key ? 1 : 0, 0, 0, 0);
LY_CHECK_ERR_GOTO(ret, p->value.realtype = NULL, cleanup);

Expand Down Expand Up @@ -762,8 +762,8 @@ ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_

/* store the value */
LOG_LOCSET(ctx_node, NULL, NULL, NULL);
ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaflist *)ctx_node)->type, val, val_len, 0, NULL,
format, prefix_data, LYD_HINT_DATA, ctx_node, NULL);
ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaflist *)ctx_node)->type, val, val_len, 0, 0,
NULL, format, prefix_data, LYD_HINT_DATA, ctx_node, NULL);
LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0);
LY_CHECK_ERR_GOTO(ret, p->value.realtype = NULL, cleanup);
++(*tok_idx);
Expand Down Expand Up @@ -1340,7 +1340,7 @@ ly_path_eval_partial(const struct ly_path *path, const struct lyd_node *start, c
case LY_PATH_PREDTYPE_LIST_VAR:
case LY_PATH_PREDTYPE_LIST:
/* we will use hashes to find one list instance */
LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, vars, &target));
LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, vars, 1, &target));
lyd_find_sibling_first(start, target, &node);
lyd_free_tree(target);
break;
Expand Down
Loading

0 comments on commit 8e216c9

Please sign in to comment.