Skip to content

Commit acce685

Browse files
committed
tree data UPDATE refactor validating values
Dedicated function for default value validation. Refs #2433
1 parent f202e7f commit acce685

File tree

11 files changed

+47
-52
lines changed

11 files changed

+47
-52
lines changed

doc/transition_3_4.dox

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* and the other for YANG data that can be written to normally. The main consequence of this is that comparing
1515
* directly (the addresses) of strings from these 2 dictionaries will not work. Next, all plugin references were
1616
* replaced by an `uintptr_t` value but there are functions ::lysc_get_type_plugin() and ::lysc_get_ext_plugin() for
17-
* getting the actual plugin structures. Finally, some new callbacks were required for extension-instance plugins
18-
* so that they can be printed with the context as well.
17+
* getting the actual plugin structures. Then, default values are stored as strings in the schema nodes. To learn their
18+
* real type or get their canonical value, use ::lyd_value_validate_dflt(). Finally, some new callbacks were required
19+
* for extension-instance plugins so that they can be printed with the context as well.
1920
*
2021
* Other notable changes include major LYB binary data format optimizations. The effect in API is that wherever a size
2122
* of a value is required, it is now expected in bits instead of bytes. Next, there are extension plugin API

src/parser_common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ lyd_parser_notif_eventtime_validate(const struct lyd_node *node)
9494

9595
/* validate the value, in JSON format */
9696
value = lyd_get_value(node);
97-
LY_CHECK_RET(lyd_value_validate(LYD_CTX(node), schema, value, strlen(value), NULL, NULL, NULL));
97+
LY_CHECK_RET(lyd_value_validate(schema, value, strlen(value), NULL, NULL, NULL));
9898
} else {
9999
LYSC_CTX_INIT_CTX(cctx, ctx);
100100

src/path.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,8 @@ ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_
716716

717717
/* do not store the canonical value, only validate */
718718
LOG_LOCSET(key, NULL);
719-
r = lyd_value_validate3(ctx_node->module->ctx, key, val, val_len, format, prefix_data, LYD_HINT_DATA,
720-
NULL, top_ext, NULL, &p->value);
719+
r = lyd_value_validate3(key, val, val_len, format, prefix_data, LYD_HINT_DATA, NULL, top_ext, 1, NULL,
720+
&p->value);
721721
LOG_LOCBACK(1, 0);
722722
LY_CHECK_ERR_GOTO(r && (r != LY_EINCOMPLETE), rc = r, cleanup);
723723

@@ -775,8 +775,8 @@ ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_
775775

776776
/* do not store the value, only validate */
777777
LOG_LOCSET(ctx_node, NULL);
778-
r = lyd_value_validate3(ctx_node->module->ctx, ctx_node, val, val_len, format, prefix_data, LYD_HINT_DATA, NULL,
779-
top_ext, NULL, &p->value);
778+
r = lyd_value_validate3(ctx_node, val, val_len, format, prefix_data, LYD_HINT_DATA, NULL, top_ext, 1, NULL,
779+
&p->value);
780780
LOG_LOCBACK(1, 0);
781781
LY_CHECK_ERR_GOTO(r && (r != LY_EINCOMPLETE), rc = r, cleanup);
782782

src/tree_data.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,6 @@ LIBYANG_API_DECL void lyd_free_attr_siblings(const struct ly_ctx *ctx, struct ly
18521852
*
18531853
* The given node is not modified in any way - it is just checked if the @p value can be set to the node.
18541854
*
1855-
* @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
18561855
* @param[in] schema Schema node of the @p value.
18571856
* @param[in] value String value to be checked, it is expected to be in JSON format.
18581857
* @param[in] value_len Length of the given @p value (mandatory).
@@ -1865,20 +1864,17 @@ LIBYANG_API_DECL void lyd_free_attr_siblings(const struct ly_ctx *ctx, struct ly
18651864
* (e.g. due to require-instance).
18661865
* @return LY_ERR value if an error occurred.
18671866
*/
1868-
LIBYANG_API_DECL LY_ERR lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value,
1869-
uint32_t value_len, const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical);
1867+
LIBYANG_API_DECL LY_ERR lyd_value_validate(const struct lysc_node *schema, const char *value, uint32_t value_len,
1868+
const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical);
18701869

18711870
/**
18721871
* @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
18731872
*
18741873
* The given node is not modified in any way - it is just checked if the @p value can be set to the node.
18751874
*
1876-
* @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
18771875
* @param[in] schema Schema node of the @p value.
1878-
* @param[in] value String value to be checked, it is expected to be in JSON format.
1879-
* @param[in] value_len Length of the given @p value (mandatory).
1880-
* @param[in] hints Value hints, bitmap of @ref lydvalhints. Use ::LYD_HINT_DATA for generic data value and
1881-
* ::LYD_HINT_SCHEMA for a schema value (default value, for example).
1876+
* @param[in] value String default value to be checked, in a ::lysc_value.
1877+
* @param[in] prefixes Stored prefixes of @p value, in a ::lysc_value.
18821878
* @param[in] ctx_node Optional data tree context node for the value (leafref target, instance-identifier).
18831879
* If not set and is required for the validation to complete, ::LY_EINCOMPLETE is be returned.
18841880
* @param[out] realtype Optional real type of @p value.
@@ -1888,8 +1884,8 @@ LIBYANG_API_DECL LY_ERR lyd_value_validate(const struct ly_ctx *ctx, const struc
18881884
* (e.g. due to require-instance).
18891885
* @return LY_ERR value if an error occurred.
18901886
*/
1891-
LIBYANG_API_DECL LY_ERR lyd_value_validate2(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value,
1892-
uint32_t value_len, uint32_t hints, const struct lyd_node *ctx_node, const struct lysc_type **realtype,
1887+
LIBYANG_API_DECL LY_ERR lyd_value_validate_dflt(const struct lysc_node *schema, const char *value,
1888+
struct lysc_prefix *prefixes, const struct lyd_node *ctx_node, const struct lysc_type **realtype,
18931889
const char **canonical);
18941890

18951891
/**

src/tree_data_common.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -620,41 +620,39 @@ ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const
620620
}
621621

622622
LIBYANG_API_DEF LY_ERR
623-
lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, uint32_t value_len,
623+
lyd_value_validate(const struct lysc_node *schema, const char *value, uint32_t value_len,
624624
const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
625625
{
626-
LY_CHECK_ARG_RET(ctx, schema, !value_len || value, LY_EINVAL);
626+
LY_CHECK_ARG_RET(NULL, schema, !value_len || value, LY_EINVAL);
627627

628-
return lyd_value_validate3(ctx, schema, value, value_len, LY_VALUE_JSON, NULL, LYD_HINT_DATA, ctx_node, NULL,
629-
realtype, canonical);
628+
return lyd_value_validate3(schema, value, value_len, LY_VALUE_JSON, NULL, LYD_HINT_DATA, ctx_node, NULL,
629+
1, realtype, canonical);
630630
}
631631

632632
LIBYANG_API_DEF LY_ERR
633-
lyd_value_validate2(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, uint32_t value_len,
634-
uint32_t hints, const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
633+
lyd_value_validate_dflt(const struct lysc_node *schema, const char *value, struct lysc_prefix *prefixes,
634+
const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
635635
{
636-
LY_CHECK_ARG_RET(ctx, schema, !value_len || value, LY_EINVAL);
636+
LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
637637

638-
return lyd_value_validate3(ctx, schema, value, value_len, LY_VALUE_JSON, NULL, hints, ctx_node, NULL,
639-
realtype, canonical);
638+
return lyd_value_validate3(schema, value, value ? strlen(value) : 0, LY_VALUE_SCHEMA_RESOLVED, prefixes,
639+
LYD_HINT_SCHEMA, ctx_node, NULL, 1, realtype, canonical);
640640
}
641641

642642
LY_ERR
643-
lyd_value_validate3(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
644-
LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lyd_node *ctx_node,
645-
const struct lysc_ext_instance *top_ext, const struct lysc_type **realtype, const char **canonical)
643+
lyd_value_validate3(const struct lysc_node *schema, const char *value, size_t value_len, LY_VALUE_FORMAT format,
644+
void *prefix_data, uint32_t hints, const struct lyd_node *ctx_node, const struct lysc_ext_instance *top_ext,
645+
int log, const struct lysc_type **realtype, const char **canonical)
646646
{
647647
LY_ERR rc;
648+
const struct ly_ctx *ctx;
648649
struct ly_err_item *err = NULL;
649650
struct lysc_type *type;
650651
struct lyd_value val = {0};
651-
ly_bool stored = 0, log = 1;
652+
ly_bool stored = 0;
652653
struct lyplg_type *type_plg;
653654

654-
if (!ctx) {
655-
ctx = schema->module->ctx;
656-
log = 0;
657-
}
655+
ctx = schema->module->ctx;
658656
if (!value_len) {
659657
value = "";
660658
}

src/tree_data_internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,6 @@ LY_ERR ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node,
641641
*
642642
* The given node is not modified in any way - it is just checked if the @p value can be set to the node.
643643
*
644-
* @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
645644
* @param[in] schema Schema node of the @p value.
646645
* @param[in] value String value to be checked, it is expected to be in JSON format.
647646
* @param[in] value_len Length of the given @p value (mandatory).
@@ -651,16 +650,17 @@ LY_ERR ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node,
651650
* @param[in] ctx_node Optional data tree context node for the value (leafref target, instance-identifier).
652651
* If not set and is required for the validation to complete, ::LY_EINCOMPLETE is be returned.
653652
* @param[in] top_ext Extension instance containing the definition of the data being validated.
653+
* @param[in] log Whether to log errors or not.
654654
* @param[out] realtype Optional real type of @p value.
655655
* @param[out] canonical Optional canonical value of @p value (in the dictionary).
656656
* @return LY_SUCCESS on success
657657
* @return LY_EINCOMPLETE in case the @p ctx_node is not provided and it was needed to finish the validation
658658
* (e.g. due to require-instance).
659659
* @return LY_ERR value if an error occurred.
660660
*/
661-
LY_ERR lyd_value_validate3(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
661+
LY_ERR lyd_value_validate3(const struct lysc_node *schema, const char *value, size_t value_len,
662662
LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lyd_node *ctx_node,
663-
const struct lysc_ext_instance *top_ext, const struct lysc_type **realtype, const char **canonical);
663+
const struct lysc_ext_instance *top_ext, int log, const struct lysc_type **realtype, const char **canonical);
664664

665665
/**
666666
* @defgroup datahash Data nodes hash manipulation

src/tree_data_new.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,8 +1579,8 @@ lyd_new_path_check_find_lypath(struct ly_path *path, const struct lysc_ext_insta
15791579
}
15801580
if (!r) {
15811581
/* validate the value and store the canonical value */
1582-
r = lyd_value_validate3(NULL, schema, value, LYPLG_BITS2BYTES(value_size_bits), format, NULL,
1583-
LYD_HINT_DATA, NULL, ext, NULL, &canon);
1582+
r = lyd_value_validate3(schema, value, LYPLG_BITS2BYTES(value_size_bits), format, NULL, LYD_HINT_DATA,
1583+
NULL, ext, 0, NULL, &canon);
15841584
LY_CHECK_RET(r && r != LY_EINCOMPLETE, r);
15851585

15861586
/* store the new predicate so that it is used when searching for this instance */

src/tree_schema_common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,8 +2861,8 @@ lysc_value_cmp(const struct lysc_node *schema, const struct lyd_node *ctx_node,
28612861
int cmp;
28622862
const char *canon;
28632863

2864-
r = lyd_value_validate3(schema->module->ctx, schema, val->str, strlen(val->str), LY_VALUE_SCHEMA_RESOLVED,
2865-
val->prefixes, LYD_HINT_SCHEMA, ctx_node, NULL, NULL, &canon);
2864+
r = lyd_value_validate3(schema, val->str, strlen(val->str), LY_VALUE_SCHEMA_RESOLVED, val->prefixes,
2865+
LYD_HINT_SCHEMA, ctx_node, NULL, 1, NULL, &canon);
28662866
if (r && (r != LY_EINCOMPLETE)) {
28672867
return -1;
28682868
}

src/validation.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,9 +1336,9 @@ lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *c
13361336
val1 = lyd_get_value(diter);
13371337
} else if (slist->uniques[u][v]->dflt.str) {
13381338
/* use canonical default value */
1339-
lyd_value_validate3(ctx, first->schema, slist->uniques[u][v]->dflt.str,
1340-
strlen(slist->uniques[u][v]->dflt.str), LY_VALUE_SCHEMA_RESOLVED,
1341-
slist->uniques[u][v]->dflt.prefixes, LYD_HINT_SCHEMA, NULL, NULL, NULL, &canon2);
1339+
lyd_value_validate3(first->schema, slist->uniques[u][v]->dflt.str, strlen(slist->uniques[u][v]->dflt.str),
1340+
LY_VALUE_SCHEMA_RESOLVED, slist->uniques[u][v]->dflt.prefixes, LYD_HINT_SCHEMA, NULL, NULL, 1,
1341+
NULL, &canon2);
13421342
val1 = canon1;
13431343
}
13441344

@@ -1350,9 +1350,9 @@ lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *c
13501350
val2 = lyd_get_value(diter);
13511351
} else if (slist->uniques[u][v]->dflt.str) {
13521352
/* use canonical default value */
1353-
lyd_value_validate3(ctx, first->schema, slist->uniques[u][v]->dflt.str,
1354-
strlen(slist->uniques[u][v]->dflt.str), LY_VALUE_SCHEMA_RESOLVED,
1355-
slist->uniques[u][v]->dflt.prefixes, LYD_HINT_SCHEMA, NULL, NULL, NULL, &canon2);
1353+
lyd_value_validate3(first->schema, slist->uniques[u][v]->dflt.str, strlen(slist->uniques[u][v]->dflt.str),
1354+
LY_VALUE_SCHEMA_RESOLVED, slist->uniques[u][v]->dflt.prefixes, LYD_HINT_SCHEMA, NULL, NULL, 1,
1355+
NULL, &canon2);
13561356
val2 = canon2;
13571357
}
13581358

@@ -1480,8 +1480,8 @@ lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode,
14801480
val = lyd_get_value(diter);
14811481
} else if (uniques[u][v]->dflt.str) {
14821482
/* use canonical default value */
1483-
ret = lyd_value_validate3(ctx, snode, uniques[u][v]->dflt.str, strlen(uniques[u][v]->dflt.str),
1484-
LY_VALUE_SCHEMA_RESOLVED, uniques[u][v]->dflt.prefixes, LYD_HINT_SCHEMA, NULL, NULL,
1483+
ret = lyd_value_validate3(snode, uniques[u][v]->dflt.str, strlen(uniques[u][v]->dflt.str),
1484+
LY_VALUE_SCHEMA_RESOLVED, uniques[u][v]->dflt.prefixes, LYD_HINT_SCHEMA, NULL, NULL, 1,
14851485
NULL, &val);
14861486
LY_CHECK_GOTO(ret, cleanup);
14871487
}

src/xpath.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,8 +1709,8 @@ set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
17091709
}
17101710

17111711
/* print canonized string, ignore errors, the value may not satisfy schema constraints */
1712-
r = lyd_value_validate3(NULL, xp_node->node->schema, set->val.str, strlen(set->val.str), set->format,
1713-
set->prefix_data, LYD_HINT_DATA, NULL, set->ext, NULL, &canon);
1712+
r = lyd_value_validate3(xp_node->node->schema, set->val.str, strlen(set->val.str), set->format, set->prefix_data,
1713+
LYD_HINT_DATA, NULL, set->ext, 0, NULL, &canon);
17141714
if (r && (r != LY_EINCOMPLETE)) {
17151715
/* invalid value, fine */
17161716
return LY_SUCCESS;

0 commit comments

Comments
 (0)