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