[PATCH v2 1/5] lib: introduce json_path_is_multikey helper
Kirill Shcherbatov
kshcherbatov at tarantool.org
Tue Mar 19 15:32:06 MSK 2019
Introduced a new json_path_is_multikey routine to test if
JSON path contains array index placeholder JSON_TOKEN_ANY and
return multikey_path_len if so.
Needed for #1257
---
src/lib/json/json.c | 21 +++++++++++++++++++++
src/lib/json/json.h | 8 ++++++++
test/unit/json.c | 9 ++++++++-
test/unit/json.result | 5 ++++-
4 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/src/lib/json/json.c b/src/lib/json/json.c
index 854158f63..568e9c246 100644
--- a/src/lib/json/json.c
+++ b/src/lib/json/json.c
@@ -321,6 +321,27 @@ json_path_validate(const char *path, int path_len, int index_base)
return rc;
}
+bool
+json_path_is_multikey(const char *path, int path_len, int *multikey_path_len,
+ int index_base)
+{
+ struct json_lexer lexer;
+ json_lexer_create(&lexer, path, path_len, index_base);
+ struct json_token token;
+ int rc, last_lexer_offset = 0;
+ while ((rc = json_lexer_next_token(&lexer, &token)) == 0) {
+ if (token.type == JSON_TOKEN_ANY) {
+ *multikey_path_len = last_lexer_offset;
+ return true;
+ } else if (token.type == JSON_TOKEN_END) {
+ break;
+ }
+ last_lexer_offset = lexer.offset;
+ }
+ assert(rc == 0);
+ return false;
+}
+
/**
* An snprint-style helper to print an individual token key.
*/
diff --git a/src/lib/json/json.h b/src/lib/json/json.h
index c1de3e579..4c1bc18bc 100644
--- a/src/lib/json/json.h
+++ b/src/lib/json/json.h
@@ -259,6 +259,14 @@ json_path_cmp(const char *a, int a_len, const char *b, int b_len,
int
json_path_validate(const char *path, int path_len, int index_base);
+/**
+ * Test if the passed JSON path contains array index placeholder
+ * [*] and return length of the subpath before first "[*]" token.
+ */
+bool
+json_path_is_multikey(const char *path, int path_len, int *multikey_path_len,
+ int index_base);
+
/**
* Test if a given JSON token is a JSON tree leaf, i.e.
* has no child nodes.
diff --git a/test/unit/json.c b/test/unit/json.c
index fd320c9eb..1db7e9364 100644
--- a/test/unit/json.c
+++ b/test/unit/json.c
@@ -479,7 +479,7 @@ test_path_cmp()
{"Data[1][\"Info\"].fname[1]", -1},
};
header();
- plan(lengthof(rc) + 3);
+ plan(lengthof(rc) + 6);
for (size_t i = 0; i < lengthof(rc); ++i) {
const char *path = rc[i].path;
int errpos = rc[i].errpos;
@@ -503,6 +503,13 @@ test_path_cmp()
ret = json_path_validate(invalid, strlen(invalid), INDEX_BASE);
is(ret, 6, "path %s error pos %d expected %d", invalid, ret, 6);
+ is(json_path_is_multikey(a, a_len, &ret, INDEX_BASE), false,
+ "test json_path_is_multikey: non-multikey path");
+ is(json_path_is_multikey(multikey_b, strlen(multikey_b), &ret,
+ INDEX_BASE), true,
+ "test json_path_is_multikey: multikey path");
+ is(ret, 8, "test json_path_is_multikey: multikey path %d/%d", ret, 8);
+
check_plan();
footer();
}
diff --git a/test/unit/json.result b/test/unit/json.result
index 3a15e84bb..0da6c8c6b 100644
--- a/test/unit/json.result
+++ b/test/unit/json.result
@@ -170,7 +170,7 @@ ok 2 - subtests
ok 3 - subtests
*** test_tree: done ***
*** test_path_cmp ***
- 1..8
+ 1..11
ok 1 - path cmp result "Data[1]["FIO"].fname" with "Data[1]["FIO"].fname": have 0, expected 0
ok 2 - path cmp result "Data[1]["FIO"].fname" with "["Data"][1].FIO["fname"]": have 0, expected 0
ok 3 - path cmp result "Data[1]["FIO"].fname" with "Data[1]": have 1, expected 1
@@ -179,6 +179,9 @@ ok 3 - subtests
ok 6 - path cmp result "Data[*]["FIO"].fname[*]" with "["Data"][*].FIO["fname"][*]": have 0, expected 0
ok 7 - path Data[1]["FIO"].fname is valid
ok 8 - path Data[[1]["FIO"].fname error pos 6 expected 6
+ ok 9 - test json_path_is_multikey: non-multikey path
+ ok 10 - test json_path_is_multikey: multikey path
+ ok 11 - test json_path_is_multikey: multikey path 8/8
ok 4 - subtests
*** test_path_cmp: done ***
*** test_path_snprint ***
--
2.21.0
More information about the Tarantool-patches
mailing list