Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_json.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 5 | * @brief JSON data parser for libyang |
| 6 | * |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 7 | * Copyright (c) 2020 - 2022 CESNET, z.s.p.o. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 17 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 19 | #include <stdint.h> |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 23 | #include "common.h" |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 24 | #include "compat.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 25 | #include "context.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 26 | #include "dict.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 27 | #include "in_internal.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 28 | #include "json.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 29 | #include "log.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 30 | #include "parser_data.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 31 | #include "parser_internal.h" |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 32 | #include "plugins_exts.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 33 | #include "set.h" |
| 34 | #include "tree.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 35 | #include "tree_data.h" |
| 36 | #include "tree_data_internal.h" |
| 37 | #include "tree_schema.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 38 | #include "tree_schema_internal.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 39 | #include "validation.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 40 | |
| 41 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 42 | * @brief Free the JSON data parser context. |
| 43 | * |
| 44 | * JSON implementation of lyd_ctx_free_clb(). |
| 45 | */ |
| 46 | static void |
| 47 | lyd_json_ctx_free(struct lyd_ctx *lydctx) |
| 48 | { |
| 49 | struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx; |
| 50 | |
| 51 | if (lydctx) { |
| 52 | lyd_ctx_free(lydctx); |
| 53 | lyjson_ctx_free(ctx->jsonctx); |
| 54 | free(ctx); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 59 | * @brief Pass the responsibility for releasing the dynamic values to @p dst. |
aPiecek | d2c3937 | 2021-06-16 14:03:12 +0200 | [diff] [blame] | 60 | * |
| 61 | * @param[in] jsonctx JSON context which contains the dynamic value. |
| 62 | * @param[in,out] dst Pointer to which the responsibility will be submited. |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 63 | * If the pointer is already pointing to some allocated memory, it is released beforehand. |
aPiecek | d2c3937 | 2021-06-16 14:03:12 +0200 | [diff] [blame] | 64 | */ |
| 65 | static void |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 66 | lyjson_ctx_give_dynamic_value(struct lyjson_ctx *jsonctx, char **dst) |
aPiecek | d2c3937 | 2021-06-16 14:03:12 +0200 | [diff] [blame] | 67 | { |
| 68 | assert(jsonctx && dst); |
| 69 | |
| 70 | if (!jsonctx->dynamic) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if (dst) { |
| 75 | free(*dst); |
| 76 | } |
| 77 | *dst = NULL; |
| 78 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 79 | /* give the dynamic value */ |
aPiecek | d2c3937 | 2021-06-16 14:03:12 +0200 | [diff] [blame] | 80 | *dst = (char *)jsonctx->value; |
| 81 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 82 | /* responsibility for the release is now passed to dst */ |
aPiecek | d2c3937 | 2021-06-16 14:03:12 +0200 | [diff] [blame] | 83 | jsonctx->dynamic = 0; |
| 84 | } |
| 85 | |
| 86 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 87 | * @brief Parse JSON member-name as [\@][prefix:][name] |
| 88 | * |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 89 | * \@ - metadata flag, maps to 1 in @p is_meta_p |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 90 | * prefix - name of the module of the data node |
| 91 | * name - name of the data node |
| 92 | * |
| 93 | * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller. |
| 94 | * |
| 95 | * @param[in] value String to parse |
| 96 | * @param[in] value_len Length of the @p str. |
| 97 | * @param[out] name_p Pointer to the beginning of the parsed name. |
| 98 | * @param[out] name_len_p Pointer to the length of the parsed name. |
| 99 | * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL. |
| 100 | * @param[out] prefix_len_p Pointer to the length of the parsed prefix. If the member-name does not contain prefix, result is 0. |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 101 | * @param[out] is_meta_p Pointer to the metadata flag, set to 1 if the member-name contains \@, 0 otherwise. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 102 | */ |
| 103 | static void |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 104 | lydjson_parse_name(const char *value, size_t value_len, const char **name_p, size_t *name_len_p, const char **prefix_p, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 105 | size_t *prefix_len_p, ly_bool *is_meta_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 106 | { |
| 107 | const char *name, *prefix = NULL; |
| 108 | size_t name_len, prefix_len = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 109 | ly_bool is_meta = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 110 | |
| 111 | name = memchr(value, ':', value_len); |
| 112 | if (name != NULL) { |
| 113 | prefix = value; |
| 114 | if (*prefix == '@') { |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 115 | is_meta = 1; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 116 | prefix++; |
| 117 | } |
| 118 | prefix_len = name - prefix; |
| 119 | name++; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 120 | name_len = value_len - (prefix_len + 1) - is_meta; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 121 | } else { |
| 122 | name = value; |
| 123 | if (name[0] == '@') { |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 124 | is_meta = 1; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 125 | name++; |
| 126 | } |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 127 | name_len = value_len - is_meta; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 130 | *name_p = name; |
| 131 | *name_len_p = name_len; |
| 132 | *prefix_p = prefix; |
| 133 | *prefix_len_p = prefix_len; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 134 | *is_meta_p = is_meta; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @brief Get correct prefix (module_name) inside the @p node. |
| 139 | * |
| 140 | * @param[in] node Data node to get inherited prefix. |
| 141 | * @param[in] local_prefix Local prefix to replace the inherited prefix. |
| 142 | * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken. |
| 143 | * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix |
| 144 | * and no context @p node to get inherited prefix. |
| 145 | * @param[out] prefix_len_p Pointer to the length of the resulting @p prefix_p string. Note that the result can be 0 in case |
| 146 | * of no local prefix and no context @p node to get inherited prefix. |
| 147 | * @return LY_ERR value. |
| 148 | */ |
| 149 | static LY_ERR |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 150 | lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t local_prefix_len, const char **prefix_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 151 | size_t *prefix_len_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 152 | { |
| 153 | struct lyd_node_opaq *onode; |
| 154 | const char *module_name = NULL; |
| 155 | |
| 156 | assert(prefix_p && prefix_len_p); |
| 157 | |
| 158 | if (local_prefix_len) { |
| 159 | *prefix_p = local_prefix; |
| 160 | *prefix_len_p = local_prefix_len; |
| 161 | return LY_SUCCESS; |
| 162 | } |
| 163 | |
| 164 | *prefix_p = NULL; |
| 165 | while (node) { |
| 166 | if (node->schema) { |
| 167 | *prefix_p = node->schema->module->name; |
| 168 | break; |
| 169 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 170 | onode = (struct lyd_node_opaq *)node; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 171 | if (onode->name.module_name) { |
| 172 | *prefix_p = onode->name.module_name; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 173 | break; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 174 | } else if (onode->name.prefix) { |
| 175 | *prefix_p = onode->name.prefix; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 176 | break; |
| 177 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 178 | node = lyd_parent(node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 179 | } |
| 180 | *prefix_len_p = ly_strlen(module_name); |
| 181 | |
| 182 | return LY_SUCCESS; |
| 183 | } |
| 184 | |
| 185 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 186 | * @brief Skip the currently open JSON object/array |
| 187 | * @param[in] jsonctx JSON context with the input data to skip. |
| 188 | * @return LY_ERR value. |
| 189 | */ |
| 190 | static LY_ERR |
| 191 | lydjson_data_skip(struct lyjson_ctx *jsonctx) |
| 192 | { |
| 193 | enum LYJSON_PARSER_STATUS status, current; |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 194 | uint32_t sublevels, prev_depth; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 195 | |
| 196 | status = lyjson_ctx_status(jsonctx, 0); |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 197 | if (status == LYJSON_OBJECT) { |
| 198 | sublevels = 1; |
| 199 | } else { |
| 200 | sublevels = 0; |
| 201 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 202 | |
| 203 | /* skip after the content */ |
| 204 | do { |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 205 | prev_depth = jsonctx->depth; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 206 | LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); |
Juraj Vijtiuk | 2e88368 | 2021-09-20 17:00:56 +0200 | [diff] [blame] | 207 | |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 208 | if ((current == LYJSON_OBJECT) && (prev_depth < jsonctx->depth)) { |
Juraj Vijtiuk | 2e88368 | 2021-09-20 17:00:56 +0200 | [diff] [blame] | 209 | /* lyjson_ctx_next() can return LYSJON_OBJECT in two cases, either when |
| 210 | * a new object is encountered, or when it finishes parsing a value from a |
| 211 | * previous key-value pair. In the latter case the sublevel shouldn't increase. |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 212 | */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 213 | sublevels++; |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 214 | } else if (current == LYJSON_OBJECT_CLOSED) { |
Juraj Vijtiuk | 46eb15e | 2021-09-06 15:38:36 +0200 | [diff] [blame] | 215 | sublevels--; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 216 | } |
Michal Vasko | ef96467 | 2022-01-03 11:13:25 +0100 | [diff] [blame] | 217 | } while ((current != status + 1) || sublevels); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 218 | |
| 219 | return LY_SUCCESS; |
| 220 | } |
| 221 | |
| 222 | /** |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 223 | * @brief Get schema node corresponding to the input parameters. |
| 224 | * |
| 225 | * @param[in] lydctx JSON data parser context. |
| 226 | * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only. |
| 227 | * @param[in] prefix Requested node's prefix (module name). |
| 228 | * @param[in] prefix_len Length of the @p prefix. |
| 229 | * @param[in] name Requested node's name. |
| 230 | * @param[in] name_len Length of the @p name. |
| 231 | * @param[in] parent Parent of the node being processed, can be NULL in case of top-level. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 232 | * @param[out] snode Found schema node corresponding to the input parameters. If NULL, parse as an opaque node. |
| 233 | * @param[out] ext Extension instance that provided @p snode, if any. |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 234 | * @return LY_SUCCES on success. |
| 235 | * @return LY_ENOT if the whole object was parsed (skipped or as an extension). |
| 236 | * @return LY_ERR on error. |
| 237 | */ |
| 238 | static LY_ERR |
| 239 | lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 240 | size_t name_len, struct lyd_node *parent, const struct lysc_node **snode, struct lysc_ext_instance **ext) |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 241 | { |
| 242 | LY_ERR ret = LY_SUCCESS, r; |
| 243 | struct lys_module *mod = NULL; |
| 244 | uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
| 245 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 246 | *snode = NULL; |
| 247 | *ext = NULL; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 248 | |
| 249 | LOG_LOCSET(NULL, parent, NULL, NULL); |
| 250 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 251 | /* get the element module, prefer parent context because of extensions */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 252 | if (prefix_len) { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 253 | mod = ly_ctx_get_module_implemented2(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, prefix, prefix_len); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 254 | } else if (parent) { |
| 255 | if (parent->schema) { |
| 256 | mod = parent->schema->module; |
| 257 | } |
| 258 | } else { |
| 259 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.", |
| 260 | (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name); |
| 261 | ret = LY_EVALID; |
| 262 | goto cleanup; |
| 263 | } |
| 264 | if (!mod) { |
| 265 | /* check for extension data */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 266 | r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext); |
| 267 | if (r != LY_ENOT) { |
| 268 | /* success or error */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 269 | ret = r; |
| 270 | goto cleanup; |
| 271 | } |
| 272 | |
| 273 | /* unknown module */ |
| 274 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
| 275 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix); |
| 276 | ret = LY_EVALID; |
| 277 | goto cleanup; |
| 278 | } |
| 279 | if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
| 280 | /* skip element with children */ |
| 281 | ret = lydjson_data_skip(lydctx->jsonctx); |
| 282 | LY_CHECK_GOTO(ret, cleanup); |
| 283 | |
| 284 | ret = LY_ENOT; |
| 285 | goto cleanup; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /* get the schema node */ |
| 290 | if (mod && (!parent || parent->schema)) { |
| 291 | if (!parent && lydctx->ext) { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 292 | *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 293 | } else { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 294 | *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 295 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 296 | if (!*snode) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 297 | /* check for extension data */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 298 | r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext); |
| 299 | if (r != LY_ENOT) { |
| 300 | /* success or error */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 301 | ret = r; |
| 302 | goto cleanup; |
| 303 | } |
| 304 | |
| 305 | /* unknown data node */ |
| 306 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
| 307 | if (parent) { |
| 308 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.", |
| 309 | (int)name_len, name, LYD_NAME(parent)); |
| 310 | } else if (lydctx->ext) { |
| 311 | if (lydctx->ext->argument) { |
| 312 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, |
| 313 | "Node \"%.*s\" not found in the \"%s\" %s extension instance.", |
| 314 | (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name); |
| 315 | } else { |
| 316 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.", |
| 317 | (int)name_len, name, lydctx->ext->def->name); |
| 318 | } |
| 319 | } else { |
| 320 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.", |
| 321 | (int)name_len, name, mod->name); |
| 322 | } |
| 323 | ret = LY_EVALID; |
| 324 | goto cleanup; |
| 325 | } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
| 326 | /* skip element with children */ |
| 327 | ret = lydjson_data_skip(lydctx->jsonctx); |
| 328 | LY_CHECK_GOTO(ret, cleanup); |
| 329 | |
| 330 | ret = LY_ENOT; |
| 331 | goto cleanup; |
| 332 | } |
| 333 | } else { |
| 334 | /* check that schema node is valid and can be used */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 335 | ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | cleanup: |
| 340 | LOG_LOCBACK(0, parent ? 1 : 0, 0, 0); |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 345 | * @brief Check that the input data are parseable as the @p list. |
| 346 | * |
| 347 | * Checks for all the list's keys. Function does not revert the context state. |
| 348 | * |
| 349 | * @param[in] jsonctx JSON parser context. |
| 350 | * @param[in] list List schema node corresponding to the input data object. |
| 351 | * @return LY_SUCCESS in case the data are ok for the @p list |
| 352 | * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance. |
| 353 | */ |
| 354 | static LY_ERR |
| 355 | lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list) |
| 356 | { |
| 357 | LY_ERR ret = LY_SUCCESS; |
| 358 | enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0); |
| 359 | struct ly_set key_set = {0}; |
| 360 | const struct lysc_node *snode; |
| 361 | uint32_t i, status_count; |
| 362 | |
| 363 | assert(list && (list->nodetype == LYS_LIST)); |
| 364 | assert(status == LYJSON_OBJECT); |
| 365 | |
| 366 | /* get all keys into a set (keys do not have if-features or anything) */ |
| 367 | snode = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 368 | while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 369 | ret = ly_set_add(&key_set, (void *)snode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 370 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (status != LYJSON_OBJECT_EMPTY) { |
| 374 | status_count = jsonctx->status.count; |
| 375 | |
| 376 | while (key_set.count && status != LYJSON_OBJECT_CLOSED) { |
| 377 | const char *name, *prefix; |
| 378 | size_t name_len, prefix_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 379 | ly_bool is_attr; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 380 | |
| 381 | /* match the key */ |
| 382 | snode = NULL; |
| 383 | lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr); |
| 384 | |
| 385 | if (!is_attr && !prefix) { |
| 386 | for (i = 0; i < key_set.count; ++i) { |
| 387 | snode = (const struct lysc_node *)key_set.objs[i]; |
| 388 | if (!ly_strncmp(snode->name, name, name_len)) { |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | /* go into the item to a) process it as a key or b) start skipping it as another list child */ |
| 393 | ret = lyjson_ctx_next(jsonctx, &status); |
| 394 | LY_CHECK_GOTO(ret, cleanup); |
| 395 | |
| 396 | if (snode) { |
| 397 | /* we have the key, validate the value */ |
| 398 | if (status < LYJSON_NUMBER) { |
| 399 | /* not a terminal */ |
| 400 | ret = LY_ENOT; |
| 401 | goto cleanup; |
| 402 | } |
| 403 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 404 | ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 405 | LY_CHECK_GOTO(ret, cleanup); |
| 406 | |
| 407 | /* key with a valid value, remove from the set */ |
| 408 | ly_set_rm_index(&key_set, i, NULL); |
| 409 | } |
| 410 | } else { |
| 411 | /* start skipping the member we are not interested in */ |
| 412 | ret = lyjson_ctx_next(jsonctx, &status); |
| 413 | LY_CHECK_GOTO(ret, cleanup); |
| 414 | } |
| 415 | /* move to the next child */ |
| 416 | while (status_count < jsonctx->status.count) { |
| 417 | ret = lyjson_ctx_next(jsonctx, &status); |
| 418 | LY_CHECK_GOTO(ret, cleanup); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if (key_set.count) { |
| 424 | /* some keys are missing/did not validate */ |
| 425 | ret = LY_ENOT; |
| 426 | } |
| 427 | |
| 428 | cleanup: |
| 429 | ly_set_erase(&key_set, NULL); |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * @brief Get the hint for the data type parsers according to the current JSON parser context. |
| 435 | * |
| 436 | * @param[in] lydctx JSON data parser context. The context is supposed to be on a value. |
| 437 | * @param[in,out] status Pointer to the current context status, |
| 438 | * in some circumstances the function manipulates with the context so the status is updated. |
| 439 | * @param[out] type_hint_p Pointer to the variable to store the result. |
| 440 | * @return LY_SUCCESS in case of success. |
| 441 | * @return LY_EINVAL in case of invalid context status not referring to a value. |
| 442 | */ |
| 443 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 444 | lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 445 | { |
| 446 | *type_hint_p = 0; |
| 447 | |
| 448 | if (*status_p == LYJSON_ARRAY) { |
| 449 | /* only [null] */ |
| 450 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p)); |
| 451 | LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL); |
| 452 | |
| 453 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL)); |
| 454 | LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL); |
| 455 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 456 | *type_hint_p = LYD_VALHINT_EMPTY; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 457 | } else if (*status_p == LYJSON_STRING) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 458 | *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 459 | } else if (*status_p == LYJSON_NUMBER) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 460 | *type_hint_p = LYD_VALHINT_DECNUM; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 461 | } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 462 | *type_hint_p = LYD_VALHINT_BOOLEAN; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 463 | } else if (*status_p == LYJSON_NULL) { |
| 464 | *type_hint_p = 0; |
| 465 | } else { |
| 466 | return LY_EINVAL; |
| 467 | } |
| 468 | |
| 469 | return LY_SUCCESS; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * @brief Check in advance if the input data are parsable according to the provided @p snode. |
| 474 | * |
| 475 | * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking |
| 476 | * is naturally done when the data are really parsed. |
| 477 | * |
| 478 | * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state |
| 479 | * as before calling, despite it is necessary to process input data for checking. |
| 480 | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
| 481 | * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list. |
| 482 | * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled. |
| 483 | * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance |
| 484 | * @return LY_EINVAL in case of invalid leaf JSON encoding |
| 485 | * and they are expected to be parsed as opaq nodes. |
| 486 | */ |
| 487 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 488 | lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, uint32_t *type_hint_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 489 | { |
| 490 | LY_ERR ret = LY_SUCCESS; |
| 491 | struct lyjson_ctx *jsonctx = lydctx->jsonctx; |
| 492 | enum LYJSON_PARSER_STATUS status; |
| 493 | |
| 494 | assert(snode); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 495 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 496 | if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) { |
| 497 | /* can always be parsed as a data node if we have the schema node */ |
| 498 | return LY_SUCCESS; |
| 499 | } |
| 500 | |
Michal Vasko | 8543070 | 2021-07-21 14:29:56 +0200 | [diff] [blame] | 501 | /* backup parser */ |
| 502 | lyjson_ctx_backup(jsonctx); |
| 503 | status = lyjson_ctx_status(jsonctx, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 504 | |
Michal Vasko | 8543070 | 2021-07-21 14:29:56 +0200 | [diff] [blame] | 505 | if (lydctx->parse_opts & LYD_PARSE_OPAQ) { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 506 | /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed |
| 507 | * as an opaq node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 508 | switch (snode->nodetype) { |
| 509 | case LYS_LEAFLIST: |
| 510 | case LYS_LEAF: |
| 511 | /* value may not be valid in which case we parse it as an opaque node */ |
| 512 | ret = lydjson_value_type_hint(lydctx, &status, type_hint_p); |
| 513 | if (ret) { |
| 514 | break; |
| 515 | } |
| 516 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 517 | if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 518 | ret = LY_ENOT; |
| 519 | } |
| 520 | break; |
| 521 | case LYS_LIST: |
| 522 | /* lists may not have all its keys */ |
| 523 | if (lydjson_check_list(jsonctx, snode)) { |
| 524 | /* invalid list, parse as opaque if it missing/has invalid some keys */ |
| 525 | ret = LY_ENOT; |
| 526 | } |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 527 | break; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 528 | } |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 529 | } else if (snode->nodetype & LYD_NODE_TERM) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 530 | status = lyjson_ctx_status(jsonctx, 0); |
| 531 | ret = lydjson_value_type_hint(lydctx, &status, type_hint_p); |
| 532 | } |
| 533 | |
Michal Vasko | 8543070 | 2021-07-21 14:29:56 +0200 | [diff] [blame] | 534 | /* restore parser */ |
| 535 | lyjson_ctx_restore(jsonctx); |
| 536 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * @brief Join the forward-referencing metadata with their target data nodes. |
| 542 | * |
| 543 | * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists. |
| 544 | * |
| 545 | * @param[in] lydctx JSON data parser context. |
| 546 | * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node) |
| 547 | * as a starting point to search for the metadata's target data node |
| 548 | * @return LY_SUCCESS on success |
| 549 | * @return LY_EVALID in case there are some metadata with unresolved target data node instance |
| 550 | */ |
| 551 | static LY_ERR |
| 552 | lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p) |
| 553 | { |
| 554 | LY_ERR ret = LY_SUCCESS; |
aPiecek | 5057c2e | 2021-05-17 10:28:03 +0200 | [diff] [blame] | 555 | struct lyd_node *node, *attr, *next, *meta_iter; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 556 | struct lysc_ext_instance *ext; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 557 | uint64_t instance = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 558 | const char *prev = NULL; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 559 | uint32_t log_location_items = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 560 | |
| 561 | /* finish linking metadata */ |
| 562 | LY_LIST_FOR_SAFE(*first_p, next, attr) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 563 | struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 564 | uint64_t match = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 565 | ly_bool is_attr; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 566 | const char *name, *prefix; |
| 567 | size_t name_len, prefix_len; |
| 568 | const struct lysc_node *snode; |
| 569 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 570 | if (attr->schema || (meta_container->name.name[0] != '@')) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 571 | /* not an opaq metadata node */ |
| 572 | continue; |
| 573 | } |
| 574 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 575 | LOG_LOCSET(NULL, attr, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 576 | log_location_items++; |
| 577 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 578 | if (prev != meta_container->name.name) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 579 | /* metas' names are stored in dictionary, so checking pointers must works */ |
| 580 | lydict_remove(lydctx->jsonctx->ctx, prev); |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 581 | LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 582 | instance = 1; |
| 583 | } else { |
| 584 | instance++; |
| 585 | } |
| 586 | |
aPiecek | 5057c2e | 2021-05-17 10:28:03 +0200 | [diff] [blame] | 587 | /* find the corresponding data node */ |
| 588 | LY_LIST_FOR(*first_p, node) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 589 | if (!node->schema) { |
| 590 | /* opaq node - we are going to put into it just a generic attribute. */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 591 | if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 592 | continue; |
| 593 | } |
| 594 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 595 | if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 596 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.", |
| 597 | ((struct lyd_node_opaq *)node)->name.name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 598 | ret = LY_EVALID; |
| 599 | goto cleanup; |
| 600 | } |
| 601 | |
| 602 | /* match */ |
| 603 | match++; |
| 604 | if (match != instance) { |
| 605 | continue; |
| 606 | } |
| 607 | |
| 608 | LY_LIST_FOR(meta_container->child, meta_iter) { |
| 609 | /* convert opaq node to a attribute of the opaq node */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 610 | struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter; |
Radek Krejci | bb27df3 | 2020-11-13 15:39:16 +0100 | [diff] [blame] | 611 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 612 | ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name), |
| 613 | meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 614 | ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON, |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 615 | NULL, meta->hints); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 616 | LY_CHECK_GOTO(ret, cleanup); |
| 617 | } |
| 618 | |
| 619 | /* done */ |
| 620 | break; |
| 621 | } else { |
| 622 | /* this is the second time we are resolving the schema node, so it must succeed, |
| 623 | * but remember that snode can be still NULL */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 624 | lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len, |
| 625 | &prefix, &prefix_len, &is_attr); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 626 | assert(is_attr); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 627 | lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, lyd_parent(*first_p), &snode, &ext); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 628 | |
| 629 | if (snode != node->schema) { |
| 630 | continue; |
| 631 | } |
| 632 | |
| 633 | /* match */ |
| 634 | match++; |
| 635 | if (match != instance) { |
| 636 | continue; |
| 637 | } |
| 638 | |
| 639 | LY_LIST_FOR(meta_container->child, meta_iter) { |
| 640 | /* convert opaq node to a metadata of the node */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 641 | struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 642 | struct lys_module *mod = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 643 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 644 | mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 645 | if (mod) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 646 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 647 | meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value), |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 648 | NULL, LY_VALUE_JSON, NULL, meta->hints, node->schema); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 649 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 650 | } else if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Michal Vasko | 4eab562 | 2021-07-22 15:36:45 +0200 | [diff] [blame] | 651 | if (meta->name.prefix) { |
| 652 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, |
| 653 | "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".", |
| 654 | meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "", |
| 655 | meta->name.name); |
| 656 | } else { |
| 657 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".", |
| 658 | meta->name.name); |
| 659 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 660 | ret = LY_EVALID; |
| 661 | goto cleanup; |
| 662 | } |
| 663 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 664 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 665 | /* add/correct flags */ |
| 666 | ret = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, ext); |
| 667 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 668 | break; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | if (match != instance) { |
| 673 | /* there is no corresponding data node for the metadata */ |
| 674 | if (instance > 1) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 675 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, |
| 676 | "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.", |
Michal Vasko | 54ba891 | 2021-07-23 12:46:23 +0200 | [diff] [blame] | 677 | instance, meta_container->name.name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 678 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 679 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.", |
| 680 | meta_container->name.name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 681 | } |
| 682 | ret = LY_EVALID; |
| 683 | } else { |
| 684 | /* remove the opaq attr */ |
| 685 | if (attr == (*first_p)) { |
aPiecek | 5057c2e | 2021-05-17 10:28:03 +0200 | [diff] [blame] | 686 | *first_p = attr->next; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 687 | } |
| 688 | lyd_free_tree(attr); |
| 689 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 690 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 691 | LOG_LOCBACK(0, log_location_items, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 692 | log_location_items = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | cleanup: |
| 696 | lydict_remove(lydctx->jsonctx->ctx, prev); |
| 697 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 698 | LOG_LOCBACK(0, log_location_items, 0, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * @brief Parse a metadata member. |
| 704 | * |
| 705 | * @param[in] lydctx JSON data parser context. |
| 706 | * @param[in] snode Schema node of the metadata parent. |
| 707 | * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM) |
| 708 | * so the data node does not exists. In such a case the metadata is stored in the context for the later |
| 709 | * processing by lydjson_metadata_finish(). |
| 710 | * @return LY_SUCCESS on success |
| 711 | * @return Various LY_ERR values in case of failure. |
| 712 | */ |
| 713 | static LY_ERR |
| 714 | lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node) |
| 715 | { |
| 716 | LY_ERR ret = LY_SUCCESS; |
| 717 | enum LYJSON_PARSER_STATUS status; |
| 718 | const char *expected; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 719 | ly_bool in_parent = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 720 | const char *name, *prefix = NULL; |
aPiecek | 303fb25 | 2021-06-16 11:54:26 +0200 | [diff] [blame] | 721 | char *dynamic_prefname = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 722 | size_t name_len, prefix_len = 0; |
| 723 | struct lys_module *mod; |
| 724 | struct lyd_meta *meta = NULL; |
| 725 | const struct ly_ctx *ctx = lydctx->jsonctx->ctx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 726 | ly_bool is_attr = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 727 | struct lyd_node *prev = node; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 728 | uint32_t instance = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 729 | uint16_t nodetype; |
| 730 | |
| 731 | assert(snode || node); |
| 732 | |
| 733 | nodetype = snode ? snode->nodetype : LYS_CONTAINER; |
| 734 | |
| 735 | /* move to the second item in the name/X pair */ |
| 736 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 737 | LY_CHECK_GOTO(ret, cleanup); |
| 738 | |
| 739 | /* check attribute encoding */ |
| 740 | switch (nodetype) { |
| 741 | case LYS_LEAFLIST: |
| 742 | expected = "@name/array of objects/nulls"; |
| 743 | |
| 744 | LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error); |
| 745 | |
| 746 | next_entry: |
| 747 | instance++; |
| 748 | |
| 749 | /* move into array / next entry */ |
| 750 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 751 | LY_CHECK_GOTO(ret, cleanup); |
| 752 | |
| 753 | if (status == LYJSON_ARRAY_CLOSED) { |
| 754 | /* we are done, move after the array */ |
| 755 | ret = lyjson_ctx_next(lydctx->jsonctx, NULL); |
| 756 | goto cleanup; |
| 757 | } |
| 758 | LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error); |
| 759 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 760 | if (!node || (node->schema != prev->schema)) { |
Michal Vasko | 54ba891 | 2021-07-23 12:46:23 +0200 | [diff] [blame] | 761 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance #%u of %s:%s to be coupled with metadata.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 762 | instance, prev->schema->module->name, prev->schema->name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 763 | ret = LY_EVALID; |
| 764 | goto cleanup; |
| 765 | } |
| 766 | |
| 767 | if (status == LYJSON_NULL) { |
| 768 | /* continue with the next entry in the leaf-list array */ |
| 769 | prev = node; |
| 770 | node = node->next; |
| 771 | goto next_entry; |
| 772 | } |
| 773 | break; |
| 774 | case LYS_LEAF: |
| 775 | case LYS_ANYXML: |
| 776 | expected = "@name/object"; |
| 777 | |
aPiecek | 5b115fc | 2021-05-24 14:32:42 +0200 | [diff] [blame] | 778 | LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 779 | break; |
| 780 | case LYS_CONTAINER: |
| 781 | case LYS_LIST: |
| 782 | case LYS_ANYDATA: |
| 783 | case LYS_NOTIF: |
| 784 | case LYS_ACTION: |
| 785 | case LYS_RPC: |
| 786 | in_parent = 1; |
| 787 | expected = "@/object"; |
| 788 | LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error); |
| 789 | break; |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 790 | default: |
| 791 | LOGINT_RET(ctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* process all the members inside a single metadata object */ |
| 795 | assert(status == LYJSON_OBJECT); |
| 796 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 797 | LOG_LOCSET(snode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 798 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 799 | while (status != LYJSON_OBJECT_CLOSED) { |
aPiecek | d2c3937 | 2021-06-16 14:03:12 +0200 | [diff] [blame] | 800 | lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 801 | lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname); |
aPiecek | 303fb25 | 2021-06-16 11:54:26 +0200 | [diff] [blame] | 802 | |
Michal Vasko | 69d0ca1 | 2021-07-23 10:22:03 +0200 | [diff] [blame] | 803 | if (!name_len) { |
| 804 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name); |
| 805 | ret = LY_EVALID; |
| 806 | goto cleanup; |
| 807 | } else if (!prefix_len) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 808 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 809 | (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 810 | ret = LY_EVALID; |
| 811 | goto cleanup; |
| 812 | } else if (is_attr) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 813 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 814 | (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 815 | ret = LY_EVALID; |
| 816 | goto cleanup; |
| 817 | } |
| 818 | |
| 819 | /* get the element module */ |
| 820 | mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len); |
| 821 | if (!mod) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 822 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 823 | LOGVAL(ctx, LYVE_REFERENCE, "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 824 | (int)prefix_len, prefix, (int)name_len, name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 825 | ret = LY_EVALID; |
| 826 | goto cleanup; |
| 827 | } |
Michal Vasko | a556f16 | 2022-01-17 14:08:30 +0100 | [diff] [blame] | 828 | if (node->schema) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 829 | /* skip element with children */ |
| 830 | ret = lydjson_data_skip(lydctx->jsonctx); |
| 831 | LY_CHECK_GOTO(ret, cleanup); |
| 832 | status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 833 | /* end of the item */ |
| 834 | continue; |
| 835 | } |
Michal Vasko | a556f16 | 2022-01-17 14:08:30 +0100 | [diff] [blame] | 836 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /* get the value */ |
| 840 | ret = lyjson_ctx_next(lydctx->jsonctx, NULL); |
| 841 | LY_CHECK_GOTO(ret, cleanup); |
| 842 | |
| 843 | if (node->schema) { |
| 844 | /* create metadata */ |
| 845 | meta = NULL; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 846 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 847 | lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 848 | LY_CHECK_GOTO(ret, cleanup); |
| 849 | |
| 850 | /* add/correct flags */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 851 | ret = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, NULL); |
| 852 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 853 | } else { |
| 854 | /* create attribute */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 855 | const char *module_name; |
| 856 | size_t module_name_len; |
| 857 | |
| 858 | lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len); |
| 859 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 860 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 861 | ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, |
| 862 | module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 863 | LY_VALUE_JSON, NULL, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 864 | LY_CHECK_GOTO(ret, cleanup); |
| 865 | } |
| 866 | /* next member */ |
| 867 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 868 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | de3c692 | 2021-05-24 14:42:10 +0200 | [diff] [blame] | 869 | LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_OBJECT_CLOSED), representation_error); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | if (nodetype == LYS_LEAFLIST) { |
| 873 | /* continue by processing another metadata object for the following |
| 874 | * leaf-list instance since they are allways instantiated in JSON array */ |
| 875 | prev = node; |
| 876 | node = node->next; |
| 877 | goto next_entry; |
| 878 | } |
| 879 | |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 880 | /* success */ |
| 881 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 882 | |
| 883 | representation_error: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 884 | LOGVAL(ctx, LYVE_SYNTAX_JSON, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 885 | "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.", |
Michal Vasko | 5722c1b | 2022-01-24 07:47:49 +0100 | [diff] [blame] | 886 | lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status), |
| 887 | in_parent ? "" : "name"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 888 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 889 | ret = LY_EVALID; |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 890 | |
| 891 | cleanup: |
aPiecek | 303fb25 | 2021-06-16 11:54:26 +0200 | [diff] [blame] | 892 | free(dynamic_prefname); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 893 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 894 | return ret; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /** |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 898 | * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing |
| 899 | * to the first child node. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 900 | * |
| 901 | * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p). |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 902 | * @param[in,out] first_p Pointer to the first sibling node in case of top-level. |
| 903 | * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL. |
| 904 | * @param[in] last If set, always insert at the end. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 905 | * @param[in] ext Extension instance of @p node_p, if any. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 906 | */ |
| 907 | static void |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 908 | lydjson_maintain_children(struct lyd_node *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last, |
| 909 | struct lysc_ext_instance *ext) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 910 | { |
| 911 | if (*node_p) { |
| 912 | /* insert, keep first pointer correct */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 913 | if (ext) { |
| 914 | lyd_insert_ext(parent, *node_p); |
| 915 | } else { |
| 916 | lyd_insert_node(parent, first_p, *node_p, last); |
| 917 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 918 | if (first_p) { |
| 919 | if (parent) { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 920 | *first_p = lyd_child(parent); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 921 | } else { |
| 922 | while ((*first_p)->prev->next) { |
| 923 | *first_p = (*first_p)->prev; |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | *node_p = NULL; |
| 928 | } |
| 929 | } |
| 930 | |
aPiecek | 4bf4721 | 2021-05-27 10:55:47 +0200 | [diff] [blame] | 931 | /** |
| 932 | * @brief Wrapper for ::lyd_create_opaq(). |
| 933 | * |
| 934 | * @param[in] lydctx JSON data parser context. |
| 935 | * @param[in] name Name of the opaq node to create. |
| 936 | * @param[in] name_len Length of the @p name string. |
| 937 | * @param[in] prefix Prefix of the opaq node to create. |
| 938 | * @param[in] prefix_len Length of the @p prefx string. |
| 939 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 940 | * but must be set if @p first is not. |
| 941 | * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone |
| 942 | * context status of the array content. Otherwise, it is supposed to be the same as @p status_p. |
| 943 | * @param[out] node_p Pointer to the created opaq node. |
| 944 | * @return LY_ERR value. |
| 945 | */ |
| 946 | static LY_ERR |
| 947 | lydjson_create_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len, |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 948 | struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p) |
aPiecek | 4bf4721 | 2021-05-27 10:55:47 +0200 | [diff] [blame] | 949 | { |
| 950 | LY_ERR ret = LY_SUCCESS; |
| 951 | const char *value = NULL, *module_name; |
| 952 | size_t value_len = 0, module_name_len = 0; |
| 953 | ly_bool dynamic = 0; |
| 954 | uint32_t type_hint = 0; |
| 955 | |
| 956 | if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) { |
| 957 | /* prepare for creating opaq node with a value */ |
| 958 | value = lydctx->jsonctx->value; |
| 959 | value_len = lydctx->jsonctx->value_len; |
| 960 | dynamic = lydctx->jsonctx->dynamic; |
| 961 | lydctx->jsonctx->dynamic = 0; |
| 962 | |
| 963 | LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint)); |
| 964 | } |
| 965 | |
| 966 | /* create node */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 967 | lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len); |
aPiecek | 4bf4721 | 2021-05-27 10:55:47 +0200 | [diff] [blame] | 968 | ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value, |
| 969 | value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p); |
| 970 | if (dynamic) { |
| 971 | free((char *)value); |
| 972 | } |
| 973 | |
| 974 | return ret; |
| 975 | } |
| 976 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 977 | static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, |
| 978 | struct ly_set *parsed); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 979 | |
| 980 | /** |
| 981 | * @brief Parse opaq node from the input. |
| 982 | * |
| 983 | * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array. |
| 984 | * |
| 985 | * @param[in] lydctx JSON data parser context. |
| 986 | * @param[in] name Name of the opaq node to create. |
| 987 | * @param[in] name_len Length of the @p name string. |
| 988 | * @param[in] prefix Prefix of the opaq node to create. |
| 989 | * @param[in] prefix_len Length of the @p prefx string. |
| 990 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 991 | * but must be set if @p first is not. |
| 992 | * @param[in,out] status_p Pointer to the current status of the parser context, |
| 993 | * since the function manipulates with the context and process the input, the status can be updated. |
| 994 | * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone |
| 995 | * context status of the array content. Otherwise, it is supposed to be the same as @p status_p. |
| 996 | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
| 997 | * @param[out] node_p Pointer to the created opaq node. |
| 998 | * @return LY_ERR value. |
| 999 | */ |
| 1000 | static LY_ERR |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1001 | lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len, |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1002 | struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p, |
| 1003 | struct lyd_node **first_p, struct lyd_node **node_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1004 | { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1005 | LY_CHECK_RET(lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1006 | |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 1007 | if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) { |
| 1008 | /* special array null value */ |
| 1009 | ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY; |
| 1010 | |
| 1011 | /* must be the only item */ |
| 1012 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p)); |
| 1013 | if (*status_inner_p != LYJSON_ARRAY_CLOSED) { |
| 1014 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member."); |
| 1015 | return LY_EVALID; |
| 1016 | } |
| 1017 | |
| 1018 | goto finish; |
| 1019 | } |
| 1020 | |
aPiecek | dbb8baf | 2021-05-27 11:01:51 +0200 | [diff] [blame] | 1021 | while ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1022 | /* process another instance of the same node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1023 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1024 | if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) { |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 1025 | /* array with objects, list */ |
| 1026 | ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST; |
| 1027 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1028 | /* but first process children of the object in the array */ |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 1029 | while ((*status_inner_p != LYJSON_OBJECT_CLOSED) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1030 | LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1031 | *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 1032 | } |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 1033 | } else { |
| 1034 | /* array with values, leaf-list */ |
| 1035 | ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p)); |
aPiecek | dbb8baf | 2021-05-27 11:01:51 +0200 | [diff] [blame] | 1039 | if (*status_inner_p == LYJSON_ARRAY_CLOSED) { |
| 1040 | goto finish; |
| 1041 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1042 | |
| 1043 | /* continue with the next instance */ |
aPiecek | dbb8baf | 2021-05-27 11:01:51 +0200 | [diff] [blame] | 1044 | assert(node_p); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1045 | lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, NULL); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1046 | LY_CHECK_RET(lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p)); |
aPiecek | dbb8baf | 2021-05-27 11:01:51 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) { |
| 1050 | /* process children */ |
| 1051 | while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) { |
| 1052 | LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL)); |
| 1053 | *status_p = lyjson_ctx_status(lydctx->jsonctx, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | |
aPiecek | dbb8baf | 2021-05-27 11:01:51 +0200 | [diff] [blame] | 1057 | finish: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1058 | /* finish linking metadata */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1059 | return lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /** |
aPiecek | 6cee5d0 | 2021-05-12 10:32:00 +0200 | [diff] [blame] | 1063 | * @brief Move to the second item in the name/X pair and parse opaq node from the input. |
| 1064 | * |
| 1065 | * This function is basically the wrapper of the ::lydjson_parse_opaq(). |
| 1066 | * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter |
| 1067 | * for ::lydjson_parse_opaq(). |
| 1068 | * |
| 1069 | * @param[in] lydctx JSON data parser context. |
| 1070 | * @param[in] name Name of the opaq node to create. |
| 1071 | * @param[in] name_len Length of the @p name string. |
| 1072 | * @param[in] prefix Prefix of the opaq node to create. |
| 1073 | * @param[in] prefix_len Length of the @p prefx string. |
| 1074 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 1075 | * but must be set if @p first is not. |
| 1076 | * @param[in,out] status_p Pointer to the current status of the parser context, |
| 1077 | * since the function manipulates with the context and process the input, the status can be updated. |
| 1078 | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
| 1079 | * @param[out] node_p Pointer to the created opaq node. |
| 1080 | * @return LY_ERR value. |
| 1081 | */ |
| 1082 | static LY_ERR |
| 1083 | lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1084 | const char *prefix, size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, |
aPiecek | 6cee5d0 | 2021-05-12 10:32:00 +0200 | [diff] [blame] | 1085 | struct lyd_node **first_p, struct lyd_node **node_p) |
| 1086 | { |
| 1087 | enum LYJSON_PARSER_STATUS status_inner = 0; |
| 1088 | |
| 1089 | /* move to the second item in the name/X pair */ |
| 1090 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p)); |
| 1091 | |
| 1092 | if (*status_p == LYJSON_ARRAY) { |
| 1093 | /* move into the array */ |
| 1094 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner)); |
| 1095 | } else { |
| 1096 | /* just a flag to pass correct parameters into lydjson_parse_opaq() */ |
| 1097 | status_inner = LYJSON_ERROR; |
| 1098 | } |
| 1099 | |
| 1100 | if (status_inner == LYJSON_ERROR) { |
| 1101 | status_inner = *status_p; |
| 1102 | } |
| 1103 | |
| 1104 | /* parse opaq node from the input */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1105 | LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner, |
| 1106 | first_p, node_p)); |
aPiecek | 6cee5d0 | 2021-05-12 10:32:00 +0200 | [diff] [blame] | 1107 | |
| 1108 | return LY_SUCCESS; |
| 1109 | } |
| 1110 | |
| 1111 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1112 | * @brief Process the attribute container (starting by @) |
| 1113 | * |
| 1114 | * @param[in] lydctx JSON data parser context. |
| 1115 | * @param[in] attr_node The data node referenced by the attribute container, if already known. |
| 1116 | * @param[in] snode The schema node of the data node referenced by the attribute container, if known. |
| 1117 | * @param[in] name Name of the opaq node to create. |
| 1118 | * @param[in] name_len Length of the @p name string. |
| 1119 | * @param[in] prefix Prefix of the opaq node to create. |
| 1120 | * @param[in] prefix_len Length of the @p prefx string. |
| 1121 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 1122 | * but must be set if @p first is not. |
| 1123 | * @param[in,out] status_p Pointer to the current status of the parser context, |
| 1124 | * since the function manipulates with the context and process the input, the status can be updated. |
| 1125 | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
| 1126 | * @param[out] node_p Pointer to the created opaq node. |
| 1127 | * @return LY_ERR value. |
| 1128 | */ |
| 1129 | static LY_ERR |
| 1130 | lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode, |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1131 | const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent, |
| 1132 | enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1133 | { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1134 | LY_ERR r; |
| 1135 | const char *opaq_name; |
| 1136 | size_t opaq_name_len; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1137 | |
| 1138 | /* parse as an attribute to a node */ |
| 1139 | if (!attr_node && snode) { |
| 1140 | /* try to find the instance */ |
| 1141 | for (struct lyd_node *iter = *first_p; iter; iter = iter->next) { |
| 1142 | if (iter->schema == snode) { |
| 1143 | attr_node = iter; |
| 1144 | break; |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | if (!attr_node) { |
| 1149 | /* parse just as an opaq node with the name beginning with @, |
| 1150 | * later we have to check that it belongs to a standard node |
| 1151 | * and it is supposed to be converted to a metadata */ |
| 1152 | uint32_t prev_opts; |
| 1153 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1154 | /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1155 | prev_opts = lydctx->parse_opts; |
| 1156 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
| 1157 | lydctx->parse_opts |= LYD_PARSE_OPAQ; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1158 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1159 | opaq_name = prefix ? prefix - 1 : name - 1; |
| 1160 | opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1; |
| 1161 | r = lydjson_ctx_next_parse_opaq(lydctx, opaq_name, opaq_name_len, NULL, 0, parent, status_p, first_p, node_p); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1162 | |
| 1163 | /* restore the parser options */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1164 | lydctx->parse_opts = prev_opts; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1165 | LY_CHECK_RET(r); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1166 | } else { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1167 | LY_CHECK_RET(lydjson_metadata(lydctx, snode, attr_node)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1168 | } |
| 1169 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1170 | return LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | /** |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 1174 | * @brief Parse a single anydata/anyxml node. |
| 1175 | * |
| 1176 | * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state |
| 1177 | * as before calling, despite it is necessary to process input data for checking. |
| 1178 | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1179 | * @param[in] ext Extension instance of @p snode, if any. |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 1180 | * @param[in,out] status JSON parser status, is updated. |
| 1181 | * @param[out] node Parsed data (or opaque) node. |
| 1182 | * @return LY_SUCCESS if a node was successfully parsed, |
| 1183 | * @return LY_ENOT in case of invalid JSON encoding, |
| 1184 | * @return LY_ERR on other errors. |
| 1185 | */ |
| 1186 | static LY_ERR |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1187 | lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext, |
| 1188 | enum LYJSON_PARSER_STATUS *status, struct lyd_node **node) |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 1189 | { |
| 1190 | LY_ERR r; |
| 1191 | uint32_t prev_parse_opts, prev_int_opts; |
| 1192 | struct ly_in in_start; |
| 1193 | char *val; |
| 1194 | struct lyd_node *tree = NULL; |
| 1195 | |
| 1196 | assert(snode->nodetype & LYD_NODE_ANY); |
| 1197 | |
| 1198 | /* status check according to allowed JSON types */ |
| 1199 | if (snode->nodetype == LYS_ANYXML) { |
| 1200 | LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY) && (*status != LYJSON_ARRAY) && |
| 1201 | (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) && (*status != LYJSON_FALSE) && |
| 1202 | (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT); |
| 1203 | } else { |
| 1204 | LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY) && (*status != LYJSON_ARRAY), LY_ENOT); |
| 1205 | } |
| 1206 | |
| 1207 | if ((snode->nodetype == LYS_ANYDATA) && (*status == LYJSON_ARRAY)) { |
| 1208 | /* only special anydata [null] allowed, 2 more moves are needed */ |
| 1209 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status)); |
| 1210 | LY_CHECK_RET(*status != LYJSON_NULL, LY_ENOT); |
| 1211 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status)); |
| 1212 | LY_CHECK_RET(*status != LYJSON_ARRAY_CLOSED, LY_ENOT); |
| 1213 | |
| 1214 | return lyd_create_any(snode, "[null]", LYD_ANYDATA_JSON, 0, node); |
| 1215 | } |
| 1216 | |
| 1217 | /* create any node */ |
| 1218 | switch (*status) { |
| 1219 | case LYJSON_OBJECT: |
| 1220 | /* parse any data tree with correct options, first backup the current options and then make the parser |
| 1221 | * process data as opaq nodes */ |
| 1222 | prev_parse_opts = lydctx->parse_opts; |
| 1223 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1224 | lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0); |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 1225 | prev_int_opts = lydctx->int_opts; |
| 1226 | lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS; |
| 1227 | |
| 1228 | /* process the anydata content */ |
| 1229 | while (*status != LYJSON_OBJECT_CLOSED) { |
| 1230 | LY_CHECK_RET(lydjson_subtree_r(lydctx, NULL, &tree, NULL)); |
| 1231 | *status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 1232 | } |
| 1233 | |
| 1234 | /* restore parser options */ |
| 1235 | lydctx->parse_opts = prev_parse_opts; |
| 1236 | lydctx->int_opts = prev_int_opts; |
| 1237 | |
| 1238 | /* finish linking metadata */ |
| 1239 | LY_CHECK_RET(lydjson_metadata_finish(lydctx, &tree)); |
| 1240 | |
| 1241 | LY_CHECK_RET(lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node)); |
| 1242 | break; |
| 1243 | case LYJSON_ARRAY: |
| 1244 | /* skip until the array end */ |
| 1245 | in_start = *lydctx->jsonctx->in; |
| 1246 | LY_CHECK_RET(lydjson_data_skip(lydctx->jsonctx)); |
| 1247 | |
| 1248 | /* make a copy of the whole array and store it */ |
| 1249 | if (asprintf(&val, "[%.*s", (int)(lydctx->jsonctx->in->current - in_start.current), in_start.current) == -1) { |
| 1250 | LOGMEM(lydctx->jsonctx->ctx); |
| 1251 | return LY_EMEM; |
| 1252 | } |
| 1253 | r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node); |
| 1254 | if (r) { |
| 1255 | free(val); |
| 1256 | return r; |
| 1257 | } |
| 1258 | break; |
| 1259 | case LYJSON_STRING: |
| 1260 | /* string value */ |
| 1261 | if (lydctx->jsonctx->dynamic) { |
| 1262 | LY_CHECK_RET(lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node)); |
| 1263 | lydctx->jsonctx->dynamic = 0; |
| 1264 | } else { |
| 1265 | val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len); |
| 1266 | LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM); |
| 1267 | |
| 1268 | r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node); |
| 1269 | if (r) { |
| 1270 | free(val); |
| 1271 | return r; |
| 1272 | } |
| 1273 | } |
| 1274 | break; |
| 1275 | case LYJSON_NUMBER: |
| 1276 | case LYJSON_FALSE: |
| 1277 | case LYJSON_TRUE: |
| 1278 | /* JSON value */ |
| 1279 | assert(!lydctx->jsonctx->dynamic); |
| 1280 | val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len); |
| 1281 | LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM); |
| 1282 | |
| 1283 | r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node); |
| 1284 | if (r) { |
| 1285 | free(val); |
| 1286 | return r; |
| 1287 | } |
| 1288 | break; |
| 1289 | case LYJSON_NULL: |
| 1290 | /* no value */ |
| 1291 | LY_CHECK_RET(lyd_create_any(snode, NULL, LYD_ANYDATA_JSON, 1, node)); |
| 1292 | break; |
| 1293 | case LYJSON_OBJECT_EMPTY: |
| 1294 | /* empty object */ |
| 1295 | LY_CHECK_RET(lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node)); |
| 1296 | break; |
| 1297 | default: |
| 1298 | LOGINT_RET(lydctx->jsonctx->ctx); |
| 1299 | } |
| 1300 | |
| 1301 | return LY_SUCCESS; |
| 1302 | } |
| 1303 | |
| 1304 | /** |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1305 | * @brief Parse a single instance of a node. |
| 1306 | * |
| 1307 | * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state |
| 1308 | * as before calling, despite it is necessary to process input data for checking. |
| 1309 | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
| 1310 | * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not. |
| 1311 | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1312 | * @param[in] ext Extension instance of @p snode, if any. |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1313 | * @param[in] name Parsed JSON node name. |
| 1314 | * @param[in] name_len Lenght of @p name. |
| 1315 | * @param[in] prefix Parsed JSON node prefix. |
| 1316 | * @param[in] prefix_len Length of @p prefix. |
| 1317 | * @param[in,out] status JSON parser status, is updated. |
| 1318 | * @param[out] node Parsed data (or opaque) node. |
| 1319 | * @return LY_SUCCESS if a node was successfully parsed, |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1320 | * @return LY_ENOT in case of invalid JSON encoding, |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1321 | * @return LY_ERR on other errors. |
| 1322 | */ |
| 1323 | static LY_ERR |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1324 | lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1325 | const struct lysc_node *snode, struct lysc_ext_instance *ext, const char *name, size_t name_len, |
| 1326 | const char *prefix, size_t prefix_len, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node) |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1327 | { |
| 1328 | LY_ERR ret; |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 1329 | uint32_t type_hints = 0; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1330 | uint32_t prev_parse_opts; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1331 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1332 | ret = lydjson_data_check_opaq(lydctx, snode, &type_hints); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1333 | if (ret == LY_SUCCESS) { |
| 1334 | assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY)); |
| 1335 | if (snode->nodetype & LYD_NODE_TERM) { |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1336 | LY_CHECK_RET((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) && |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1337 | (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT); |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1338 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1339 | /* create terminal node */ |
| 1340 | ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1341 | lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1342 | type_hints, node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1343 | LY_CHECK_RET(ret); |
| 1344 | |
| 1345 | /* move JSON parser */ |
Michal Vasko | 8543070 | 2021-07-21 14:29:56 +0200 | [diff] [blame] | 1346 | if (*status == LYJSON_ARRAY) { |
| 1347 | /* only [null], 2 more moves are needed */ |
| 1348 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status)); |
| 1349 | assert(*status == LYJSON_NULL); |
| 1350 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status)); |
| 1351 | assert(*status == LYJSON_ARRAY_CLOSED); |
| 1352 | } |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1353 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 1354 | /* create inner node */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1355 | LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1356 | |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 1357 | LY_CHECK_RET(lyd_create_inner(snode, node)); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1358 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1359 | LOG_LOCSET(snode, *node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1360 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1361 | prev_parse_opts = lydctx->parse_opts; |
| 1362 | if (ext) { |
| 1363 | /* only parse these extension data and validate afterwards */ |
| 1364 | lydctx->parse_opts |= LYD_PARSE_ONLY; |
| 1365 | } |
| 1366 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1367 | /* process children */ |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1368 | while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1369 | ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1370 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1371 | *status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 1372 | } |
| 1373 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1374 | /* restore options */ |
| 1375 | lydctx->parse_opts = prev_parse_opts; |
| 1376 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1377 | /* finish linking metadata */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1378 | ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node)); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1379 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1380 | |
| 1381 | if (snode->nodetype == LYS_LIST) { |
| 1382 | /* check all keys exist */ |
| 1383 | ret = lyd_parse_check_keys(*node); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1384 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1385 | } |
| 1386 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1387 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1388 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1389 | ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1390 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1391 | |
| 1392 | /* add any missing default children */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1393 | ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 1394 | &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1395 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1398 | LOG_LOCBACK(1, 1, 0, 0); |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 1399 | } else { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1400 | /* create any node */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1401 | LY_CHECK_RET(lydjson_parse_any(lydctx, snode, ext, status, node)); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1402 | } |
Michal Vasko | caf608d | 2021-07-23 13:51:23 +0200 | [diff] [blame] | 1403 | |
| 1404 | /* add/correct flags */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1405 | lyd_parse_set_data_flags(*node, &(*node)->meta, (struct lyd_ctx *)lydctx, ext); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1406 | } else if (ret == LY_ENOT) { |
| 1407 | /* parse it again as an opaq node */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1408 | ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1409 | LY_CHECK_RET(ret); |
| 1410 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1411 | if (snode->nodetype == LYS_LIST) { |
| 1412 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST; |
| 1413 | } else if (snode->nodetype == LYS_LEAFLIST) { |
| 1414 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1418 | return LY_SUCCESS; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | /** |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1422 | * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1423 | * |
| 1424 | * @param[in] lydctx JSON data parser context. |
| 1425 | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
| 1426 | * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not. |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1427 | * @param[in,out] parsed Optional set to add all the parsed siblings into. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1428 | * @return LY_ERR value. |
| 1429 | */ |
| 1430 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1431 | lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1432 | { |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1433 | LY_ERR ret = LY_SUCCESS, r; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1434 | enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0); |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame] | 1435 | const char *name, *prefix = NULL, *expected = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1436 | size_t name_len, prefix_len = 0; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1437 | ly_bool is_meta = 0, parse_subtree; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1438 | const struct lysc_node *snode = NULL; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1439 | struct lysc_ext_instance *ext; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1440 | struct lyd_node *node = NULL, *attr_node = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1441 | const struct ly_ctx *ctx = lydctx->jsonctx->ctx; |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame] | 1442 | char *value = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1443 | |
| 1444 | assert(parent || first_p); |
| 1445 | assert(status == LYJSON_OBJECT); |
| 1446 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1447 | parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0; |
| 1448 | /* all descendants should be parsed */ |
| 1449 | lydctx->parse_opts &= ~LYD_PARSE_SUBTREE; |
| 1450 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1451 | /* process the node name */ |
aPiecek | d2c3937 | 2021-06-16 14:03:12 +0200 | [diff] [blame] | 1452 | lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1453 | lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1454 | |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1455 | if (!is_meta || name_len || prefix_len) { |
| 1456 | /* get the schema node */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1457 | r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1458 | if (r == LY_ENOT) { |
| 1459 | /* data parsed */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1460 | goto cleanup; |
| 1461 | } |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1462 | LY_CHECK_ERR_GOTO(r, ret = r, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1463 | |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1464 | if (!snode) { |
| 1465 | /* we will not be parsing it as metadata */ |
| 1466 | is_meta = 0; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | if (is_meta) { |
| 1471 | /* parse as metadata */ |
| 1472 | if (!name_len && !prefix_len) { |
| 1473 | /* parent's metadata without a name - use the schema from the parent */ |
| 1474 | if (!parent) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1475 | LOGVAL(ctx, LYVE_SYNTAX_JSON, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1476 | "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries."); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1477 | ret = LY_EVALID; |
| 1478 | goto cleanup; |
| 1479 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1480 | attr_node = parent; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1481 | snode = attr_node->schema; |
| 1482 | } |
| 1483 | ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1484 | first_p, &node); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1485 | LY_CHECK_GOTO(ret, cleanup); |
| 1486 | } else if (!snode) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1487 | /* parse as an opaq node */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1488 | assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1489 | |
aPiecek | b7b29e6 | 2021-05-11 10:02:43 +0200 | [diff] [blame] | 1490 | /* opaq node cannot have an empty string as the name. */ |
| 1491 | if (name_len == 0) { |
| 1492 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string."); |
| 1493 | ret = LY_EVALID; |
| 1494 | goto cleanup; |
| 1495 | } |
| 1496 | |
aPiecek | 6cee5d0 | 2021-05-12 10:32:00 +0200 | [diff] [blame] | 1497 | /* move to the second item in the name/X pair and parse opaq */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1498 | ret = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, &status, first_p, &node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1499 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1500 | } else { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1501 | /* parse as a standard lyd_node but it can still turn out to be an opaque node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1502 | |
| 1503 | /* move to the second item in the name/X pair */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1504 | LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1505 | |
| 1506 | /* first check the expected representation according to the nodetype and then continue with the content */ |
| 1507 | switch (snode->nodetype) { |
| 1508 | case LYS_LEAFLIST: |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1509 | case LYS_LIST: |
| 1510 | if (snode->nodetype == LYS_LEAFLIST) { |
| 1511 | expected = "name/array of values"; |
| 1512 | } else { |
| 1513 | expected = "name/array of objects"; |
| 1514 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1515 | |
Michal Vasko | 4e26adc | 2022-03-30 11:01:16 +0200 | [diff] [blame] | 1516 | if (status == LYJSON_ARRAY_EMPTY) { |
| 1517 | /* no instances, skip */ |
| 1518 | break; |
| 1519 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1520 | LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error); |
| 1521 | |
| 1522 | /* move into array */ |
| 1523 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 1524 | LY_CHECK_GOTO(ret, cleanup); |
| 1525 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1526 | /* process all the values/objects */ |
| 1527 | do { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1528 | ret = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len, |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1529 | &status, &node); |
| 1530 | if (ret == LY_ENOT) { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1531 | goto representation_error; |
| 1532 | } else if (ret) { |
| 1533 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1534 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1535 | lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1536 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1537 | /* move after the item(s) */ |
| 1538 | LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); |
| 1539 | } while (status != LYJSON_ARRAY_CLOSED); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1540 | |
| 1541 | break; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1542 | case LYS_LEAF: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1543 | case LYS_CONTAINER: |
| 1544 | case LYS_NOTIF: |
| 1545 | case LYS_ACTION: |
| 1546 | case LYS_RPC: |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1547 | case LYS_ANYDATA: |
| 1548 | case LYS_ANYXML: |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1549 | if (snode->nodetype & (LYS_LEAF | LYS_ANYXML)) { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1550 | if (status == LYJSON_ARRAY) { |
| 1551 | expected = "name/[null]"; |
| 1552 | } else { |
| 1553 | expected = "name/value"; |
| 1554 | } |
| 1555 | } else { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1556 | expected = "name/object"; |
| 1557 | } |
| 1558 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1559 | /* process the value/object */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1560 | ret = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len, |
| 1561 | &status, &node); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1562 | if (ret == LY_ENOT) { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1563 | goto representation_error; |
| 1564 | } else if (ret) { |
| 1565 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1566 | } |
| 1567 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1568 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1569 | /* rememeber the RPC/action/notification */ |
| 1570 | lydctx->op_node = node; |
| 1571 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1572 | break; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1573 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1576 | /* finally connect the parsed node */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1577 | lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1578 | |
| 1579 | /* rememeber a successfully parsed node */ |
Michal Vasko | 4e26adc | 2022-03-30 11:01:16 +0200 | [diff] [blame] | 1580 | if (parsed && node) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1581 | ly_set_add(parsed, node, 1, NULL); |
| 1582 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1583 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1584 | if (!parse_subtree) { |
| 1585 | /* move after the item(s) */ |
| 1586 | LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); |
| 1587 | } |
| 1588 | |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 1589 | /* success */ |
| 1590 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1591 | |
| 1592 | representation_error: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1593 | LOG_LOCSET(NULL, parent, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1594 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1595 | lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status)); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1596 | LOG_LOCBACK(0, parent ? 1 : 0, 0, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1597 | ret = LY_EVALID; |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 1598 | |
| 1599 | cleanup: |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame] | 1600 | free(value); |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 1601 | lyd_free_tree(node); |
| 1602 | return ret; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | /** |
| 1606 | * @brief Common start of JSON parser processing different types of the input data. |
| 1607 | * |
| 1608 | * @param[in] ctx libyang context |
| 1609 | * @param[in] in Input structure. |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1610 | * @param[in] parse_opts Options for parser, see @ref dataparseroptions. |
| 1611 | * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1612 | * @param[out] lydctx_p Data parser context to finish validation. |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1613 | * @param[out] status Storage for the current context's status |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1614 | * @return LY_ERR value. |
| 1615 | */ |
| 1616 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1617 | lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1618 | struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1619 | { |
| 1620 | LY_ERR ret = LY_SUCCESS; |
| 1621 | struct lyd_json_ctx *lydctx; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1622 | size_t i; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1623 | ly_bool subtree; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1624 | |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1625 | assert(lydctx_p); |
| 1626 | assert(status); |
| 1627 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1628 | /* init context */ |
| 1629 | lydctx = calloc(1, sizeof *lydctx); |
| 1630 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1631 | lydctx->parse_opts = parse_opts; |
| 1632 | lydctx->val_opts = val_opts; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1633 | lydctx->free = lyd_json_ctx_free; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1634 | |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1635 | /* starting top-level */ |
| 1636 | for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1637 | if (in->current[i] == '\n') { |
| 1638 | /* new line */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1639 | LY_IN_NEW_LINE(in); |
Michal Vasko | 61d7636 | 2020-10-07 10:47:30 +0200 | [diff] [blame] | 1640 | } |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1641 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1642 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1643 | subtree = (parse_opts & LYD_PARSE_SUBTREE) ? 1 : 0; |
| 1644 | LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, subtree, &lydctx->jsonctx), free(lydctx), ret); |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1645 | *status = lyjson_ctx_status(lydctx->jsonctx, 0); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1646 | |
Radek Krejci | bb27df3 | 2020-11-13 15:39:16 +0100 | [diff] [blame] | 1647 | if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) { |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1648 | *lydctx_p = lydctx; |
| 1649 | return LY_SUCCESS; |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1650 | } else { |
| 1651 | /* expecting top-level object */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1652 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(*status)); |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1653 | *lydctx_p = NULL; |
Michal Vasko | 9350901 | 2020-11-13 10:26:09 +0100 | [diff] [blame] | 1654 | lyd_json_ctx_free((struct lyd_ctx *)lydctx); |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1655 | return LY_EVALID; |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1656 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1660 | lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 1661 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1662 | struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1663 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1664 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1665 | struct lyd_json_ctx *lydctx = NULL; |
| 1666 | enum LYJSON_PARSER_STATUS status; |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1667 | uint32_t int_opts = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1668 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1669 | rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status); |
| 1670 | LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1671 | |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1672 | assert(status == LYJSON_OBJECT); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1673 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1674 | switch (data_type) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1675 | case LYD_TYPE_DATA_YANG: |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1676 | if (!(parse_opts & LYD_PARSE_SUBTREE)) { |
| 1677 | int_opts = LYD_INTOPT_WITH_SIBLINGS; |
| 1678 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1679 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1680 | case LYD_TYPE_RPC_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1681 | int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS; |
| 1682 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1683 | case LYD_TYPE_NOTIF_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1684 | int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS; |
| 1685 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1686 | case LYD_TYPE_REPLY_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1687 | int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS; |
| 1688 | break; |
| 1689 | default: |
| 1690 | LOGINT(ctx); |
| 1691 | rc = LY_EINT; |
| 1692 | goto cleanup; |
| 1693 | } |
| 1694 | lydctx->int_opts = int_opts; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1695 | lydctx->ext = ext; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1696 | |
| 1697 | /* find the operation node if it exists already */ |
| 1698 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
| 1699 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1700 | /* read subtree(s) */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1701 | while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) { |
| 1702 | rc = lydjson_subtree_r(lydctx, parent, first_p, parsed); |
| 1703 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1704 | |
| 1705 | status = lyjson_ctx_status(lydctx->jsonctx, 0); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1706 | |
| 1707 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1708 | break; |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] && |
| 1713 | (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) { |
| 1714 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
| 1715 | rc = LY_EVALID; |
| 1716 | goto cleanup; |
| 1717 | } |
| 1718 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
| 1719 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
| 1720 | rc = LY_EVALID; |
| 1721 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | /* finish linking metadata */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1725 | rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p); |
| 1726 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1727 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 1728 | if (parse_opts & LYD_PARSE_SUBTREE) { |
| 1729 | /* check for a sibling object */ |
| 1730 | assert(subtree_sibling); |
| 1731 | if (lydctx->jsonctx->in->current[0] == ',') { |
| 1732 | *subtree_sibling = 1; |
| 1733 | |
| 1734 | /* move to the next object */ |
| 1735 | ly_in_skip(lydctx->jsonctx->in, 1); |
| 1736 | } else { |
| 1737 | *subtree_sibling = 0; |
| 1738 | } |
| 1739 | } |
| 1740 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1741 | cleanup: |
| 1742 | /* there should be no unresolved types stored */ |
Michal Vasko | 58c8b56 | 2021-12-09 09:25:33 +0100 | [diff] [blame] | 1743 | assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 1744 | !lydctx->node_when.count)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1745 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1746 | if (rc) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1747 | lyd_json_ctx_free((struct lyd_ctx *)lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1748 | } else { |
| 1749 | *lydctx_p = (struct lyd_ctx *)lydctx; |
| 1750 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1751 | return rc; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1752 | } |