[PATCH v5 4/9] lib: introduce json_path_cmp routine
Vladimir Davydov
vdavydov.dev at gmail.com
Fri Nov 30 13:46:01 MSK 2018
On Mon, Nov 26, 2018 at 01:49:38PM +0300, Kirill Shcherbatov wrote:
> diff --git a/src/lib/json/json.h b/src/lib/json/json.h
> index dd09f5a..7d46601 100644
> --- a/src/lib/json/json.h
> +++ b/src/lib/json/json.h
> @@ -137,6 +137,17 @@ json_lexer_create(struct json_lexer *lexer, const char *src, int src_len)
> int
> json_lexer_next_token(struct json_lexer *lexer, struct json_token *token);
>
> +/**
> + * Compare two JSON paths using Lexer class.
> + * - @a path must be valid
> + * - at the case of paths that have same token-sequence prefix,
> + * the path having more tokens is assumed to be greater
> + * - when @b path contains an error, the path "a" is assumed to
> + * be greater
> + */
> +int
> +json_path_cmp(const char *a, uint32_t a_len, const char *b, uint32_t b_len);
> +
One typically expects cmp(a, b) to be equivalent to -cmp(b, a).
Can't we make json_path_cmp satisfy this property, for example, by
requiring both strings to be valid json paths with an assertion?
> /** Create a JSON tree object to manage data relations. */
> int
> json_tree_create(struct json_tree *tree);
> diff --git a/test/unit/json_path.c b/test/unit/json_path.c
> index f6b0472..35c2164 100644
> --- a/test/unit/json_path.c
> +++ b/test/unit/json_path.c
> @@ -352,15 +352,44 @@ test_tree()
> footer();
> }
>
> +void
> +test_path_cmp()
> +{
> + const char *a = "Data[1][\"FIO\"].fname";
> + uint32_t a_len = strlen(a);
> + const struct path_and_errpos rc[] = {
> + {a, 0},
> + {"[\"Data\"][1].FIO[\"fname\"]", 0},
> + {"Data[[1][\"FIO\"].fname", 6},
> + {"Data[1]", 1},
> + {"Data[1][\"FIO\"].fname[1]", -2},
> + {"Data[1][\"Info\"].fname[1]", -1},
Should we reorder values of json_token_type enum, and we would have to
fix the test. Let's please rewrite the test so that it doesn't depend on
the json_lexer implementation, in other words just check that the sign
of the return value is correct.
> + };
> + header();
> + plan(lengthof(rc));
> +
> + for (size_t i = 0; i < lengthof(rc); ++i) {
> + const char *path = rc[i].path;
> + int errpos = rc[i].errpos;
> + int rc = json_path_cmp(a, a_len, path, strlen(path));
> + is(rc, errpos, "path cmp result \"%s\" with \"%s\": "
> + "have %d, expected %d", a, path, rc, errpos);
> + }
> +
> + check_plan();
> + footer();
> +}
More information about the Tarantool-patches
mailing list