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