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: kostja@tarantool.org, Kirill Shcherbatov <kshcherbatov@tarantool.org>
Subject: [PATCH v6 4/4] lib: introduce json_path_cmp, json_path_validate
Date: Thu,  6 Dec 2018 11:42:31 +0300	[thread overview]
Message-ID: <7e8a84a863de29269e0731f3dcff2df90ed6813e.1544084229.git.kshcherbatov@tarantool.org> (raw)
In-Reply-To: <cover.1544084229.git.kshcherbatov@tarantool.org>

Introduced json_path_validate routine to ensure user-defined
JSON path is valid. This will be required to raise an error if
an incorrect user-defined jason-path is detected.

Introduced json_path_cmp routine to compare JSON paths that may
have different representation.
Note that:
 - in case of paths that have same token-sequence prefix,
   the path having more tokens is assumed to be greater
 - both paths to compare should be valid

Needed for #1012
---
 src/lib/json/json.c        | 29 +++++++++++++++++++++++++++++
 src/lib/json/json.h        | 28 ++++++++++++++++++++++++++++
 test/unit/json_path.c      | 37 ++++++++++++++++++++++++++++++++++++-
 test/unit/json_path.result | 13 ++++++++++++-
 4 files changed, 105 insertions(+), 2 deletions(-)

diff --git a/src/lib/json/json.c b/src/lib/json/json.c
index 65169b047..5586e59fc 100644
--- a/src/lib/json/json.c
+++ b/src/lib/json/json.c
@@ -500,3 +500,32 @@ json_tree_postorder_next(struct json_token *root, struct json_token *pos)
 		return json_tree_leftmost(next);
 	return pos->parent;
 }
+
+int
+json_path_cmp(const char *a, uint32_t a_len, const char *b, uint32_t b_len,
+	      uint32_t index_base)
+{
+	struct json_lexer lexer_a, lexer_b;
+	json_lexer_create(&lexer_a, a, a_len, index_base);
+	json_lexer_create(&lexer_b, b, b_len, index_base);
+	struct json_token token_a, token_b;
+	token_a.parent = NULL;
+	token_b.parent = NULL;
+	int rc_a, rc_b;
+	while ((rc_a = json_lexer_next_token(&lexer_a, &token_a)) == 0 &&
+		(rc_b = json_lexer_next_token(&lexer_b, &token_b)) == 0 &&
+		token_a.type != JSON_TOKEN_END &&
+		token_b.type != JSON_TOKEN_END) {
+		int rc = json_token_cmp(&token_a, &token_b);
+		if (rc != 0)
+			return rc;
+	}
+	/* Paths a and b should be valid. */
+	assert(rc_b == 0 && rc_b == 0);
+	/*
+	 * The parser stopped because the end of one of the paths
+	 * was reached. As JSON_TOKEN_END > JSON_TOKEN_{NUM, STR},
+	 * the path having more tokens has lower key.type value.
+	 */
+	return token_b.type - token_a.type;
+}
diff --git a/src/lib/json/json.h b/src/lib/json/json.h
index 4ff2b9c67..aae337e29 100644
--- a/src/lib/json/json.h
+++ b/src/lib/json/json.h
@@ -30,6 +30,7 @@
  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+#include <stdbool.h>
 #include "trivia/util.h"
 
 #ifdef __cplusplus
@@ -222,6 +223,33 @@ 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.
+ * - in case of paths that have same token-sequence prefix,
+ *   the path having more tokens is assumed to be greater
+ * - both paths should be valid
+ *   (may be tested with json_path_validate).
+ */
+int
+json_path_cmp(const char *a, uint32_t a_len, const char *b, uint32_t b_len,
+	      uint32_t index_base);
+
+/**
+ * Check if the passed JSON path is valid.
+ * Return 0 for valid path and error position for invalid.
+ */
+static inline int
+json_path_validate(const char *path, uint32_t path_len, uint32_t index_base)
+{
+	struct json_lexer lexer;
+	json_lexer_create(&lexer, path, path_len, index_base);
+	struct json_token token;
+	int rc;
+	while ((rc = json_lexer_next_token(&lexer, &token)) == 0 &&
+		token.type != JSON_TOKEN_END) {};
+	return rc;
+}
+
 /** 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 04d36688b..f345b8125 100644
--- a/test/unit/json_path.c
+++ b/test/unit/json_path.c
@@ -405,15 +405,50 @@ 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]", 1},
+		{"Data[1][\"FIO\"].fname[1]", -1},
+		{"Data[1][\"Info\"].fname[1]", -1},
+	};
+	header();
+	plan(lengthof(rc) + 2);
+	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),
+				       TUPLE_INDEX_BASE);
+		if (rc > 0) rc = 1;
+		if (rc < 0) rc = -1;
+		is(rc, errpos, "path cmp result \"%s\" with \"%s\": "
+		   "have %d, expected %d", a, path, rc, errpos);
+	}
+	const char *invalid = "Data[[1][\"FIO\"].fname";
+	int ret = json_path_validate(a, strlen(a), TUPLE_INDEX_BASE);
+	is(ret, 0, "path %s is valid", a);
+	ret = json_path_validate(invalid, strlen(invalid), TUPLE_INDEX_BASE);
+	is(ret, 6, "path %s error pos %d expected %d", invalid, ret, 6);
+
+	check_plan();
+	footer();
+}
+
 int
 main()
 {
 	header();
-	plan(3);
+	plan(4);
 
 	test_basic();
 	test_errors();
 	test_tree();
+	test_path_cmp();
 
 	int rc = check_plan();
 	footer();
diff --git a/test/unit/json_path.result b/test/unit/json_path.result
index 0ee970c8c..cf0fa51c4 100644
--- a/test/unit/json_path.result
+++ b/test/unit/json_path.result
@@ -1,5 +1,5 @@
 	*** main ***
-1..3
+1..4
 	*** test_basic ***
     1..71
     ok 1 - parse <[1]>
@@ -158,4 +158,15 @@ ok 2 - subtests
     ok 54 - records iterated count 4 of 4
 ok 3 - subtests
 	*** test_tree: done ***
+	*** test_path_cmp ***
+    1..7
+    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
+    ok 4 - path cmp result "Data[1]["FIO"].fname" with "Data[1]["FIO"].fname[1]": have -1, expected -1
+    ok 5 - path cmp result "Data[1]["FIO"].fname" with "Data[1]["Info"].fname[1]": have -1, expected -1
+    ok 6 - path Data[1]["FIO"].fname is valid
+    ok 7 - path Data[[1]["FIO"].fname error pos 6 expected 6
+ok 4 - subtests
+	*** test_path_cmp: done ***
 	*** main: done ***
-- 
2.19.2

  parent reply	other threads:[~2018-12-06  8:42 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-06  8:42 [PATCH v6 0/4] box: JSON tree class Kirill Shcherbatov
2018-12-06  8:42 ` [PATCH v6 1/4] lib: make index_base support for json_lexer Kirill Shcherbatov
2018-12-10 17:34   ` Vladimir Davydov
2018-12-06  8:42 ` [PATCH v6 2/4] lib: implement JSON tree class for json library Kirill Shcherbatov
2018-12-10 17:36   ` Vladimir Davydov
2018-12-06  8:42 ` [PATCH v6 3/4] box: manage format fields with JSON tree class Kirill Shcherbatov
2018-12-11  9:52   ` Vladimir Davydov
2018-12-06  8:42 ` Kirill Shcherbatov [this message]
2018-12-10 17:38   ` [PATCH v6 4/4] lib: introduce json_path_cmp, json_path_validate Vladimir Davydov

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=7e8a84a863de29269e0731f3dcff2df90ed6813e.1544084229.git.kshcherbatov@tarantool.org \
    --to=kshcherbatov@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH v6 4/4] lib: introduce json_path_cmp, json_path_validate' \
    /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