Tarantool development patches archive
 help / color / mirror / Atom feed
From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
To: tarantool-patches@freelists.org, vdavydov.dev@gmail.com
Cc: Kirill Shcherbatov <kshcherbatov@tarantool.org>
Subject: [PATCH v2 1/5] lib: introduce json_path_is_multikey helper
Date: Tue, 19 Mar 2019 15:32:06 +0300	[thread overview]
Message-ID: <2ea5cf6ff29e786687e4ddd6110a1cc4ecc6d0a3.1552998554.git.kshcherbatov@tarantool.org> (raw)
In-Reply-To: <cover.1552998554.git.kshcherbatov@tarantool.org>

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

  reply	other threads:[~2019-03-19 12:32 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-19 12:32 [PATCH v2 0/5] box: introduce multikey indexes in memtx Kirill Shcherbatov
2019-03-19 12:32 ` Kirill Shcherbatov [this message]
2019-03-19 15:43   ` [tarantool-patches] [PATCH v2 1/5] lib: introduce json_path_is_multikey helper Konstantin Osipov
2019-04-02 15:49     ` [tarantool-patches] " Kirill Shcherbatov
2019-03-19 12:32 ` [PATCH v2 2/5] lib: introduce is_weak_cmp flag for json_path_cmp Kirill Shcherbatov
2019-03-19 15:47   ` [tarantool-patches] " Konstantin Osipov
2019-03-19 12:32 ` [PATCH v2 3/5] box: move offset_slot init to tuple_format_add_field Kirill Shcherbatov
2019-03-19 12:32 ` [PATCH v2 4/5] box: introduce field_map_builder for field_map init Kirill Shcherbatov
2019-03-19 12:32 ` [PATCH v2 5/5] box: introduce multikey indexes in memtx Kirill Shcherbatov
2019-03-19 16:05   ` [tarantool-patches] " Kirill Shcherbatov
2019-03-21 12:35   ` Kirill Shcherbatov
2019-03-28 10:21 ` [PATCH v2 0/5] " Vladimir Davydov
2019-04-02 15:49   ` [tarantool-patches] " Kirill Shcherbatov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2ea5cf6ff29e786687e4ddd6110a1cc4ecc6d0a3.1552998554.git.kshcherbatov@tarantool.org \
    --to=kshcherbatov@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH v2 1/5] lib: introduce json_path_is_multikey helper' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox