Tarantool development patches archive
 help / color / mirror / Atom feed
From: olegrok@tarantool.org
To: tsafin@tarantool.org, v.shpilevoy@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH] exports: allow to use json tools via FFI
Date: Fri, 21 Aug 2020 19:26:07 +0300	[thread overview]
Message-ID: <20200821162607.68179-1-olegrok@tarantool.org> (raw)

From: Oleg Babin <babinoleg@mail.ru>

This patch exports some json functions to be used via FFI. We solve
following problem: currently we don't have any tools to inspect
jsonpaths. E.g. user wants to ban or restrict an access to some
tuple fields, it could be some system fields. Tarantool doesn't
have hidden fields and to solve such problem we should fairly
parse input jsonpaths. Before this patch user should write its own
or some external tools. This patch allows to use functions from
built-in json-lexer directly from Tarantool via FFI.

Part of #5203
---
Issue: https://github.com/tarantool/tarantool/issues/5203
Branch: https://github.com/tarantool/tarantool/tree/olegrok/5203-expose-json-helpers
Also issue contains an example of usage this feature.

@Changelog:
  - Some symbols from tarantool json moudule are exported now
    (gh-5203).

 src/exports.h                              |  4 ++
 test/box-tap/gh-5203-json-exports.test.lua | 82 ++++++++++++++++++++++
 2 files changed, 86 insertions(+)
 create mode 100755 test/box-tap/gh-5203-json-exports.test.lua

diff --git a/src/exports.h b/src/exports.h
index 7cf283e5b..83c90a8bc 100644
--- a/src/exports.h
+++ b/src/exports.h
@@ -152,6 +152,10 @@ EXPORT(ibuf_create)
 EXPORT(ibuf_destroy)
 EXPORT(ibuf_reinit)
 EXPORT(ibuf_reserve_slow)
+EXPORT(json_lexer_next_token)
+EXPORT(json_path_cmp)
+EXPORT(json_path_validate)
+EXPORT(json_path_multikey_offset)
 EXPORT(lbox_socket_local_resolve)
 EXPORT(lbox_socket_nonblock)
 EXPORT(log_format)
diff --git a/test/box-tap/gh-5203-json-exports.test.lua b/test/box-tap/gh-5203-json-exports.test.lua
new file mode 100755
index 000000000..1b8eb9afa
--- /dev/null
+++ b/test/box-tap/gh-5203-json-exports.test.lua
@@ -0,0 +1,82 @@
+#!/usr/bin/env tarantool
+
+local tap = require('tap')
+local ffi = require('ffi')
+ffi.cdef([[
+    void *dlsym(void *handle, const char *symbol);
+    /**
+     * Lexer for JSON paths:
+     * <field>, <.field>, <[123]>, <['field']> and their combinations.
+     */
+    struct json_lexer {
+        /** Source string. */
+        const char *src;
+        /** Length of string. */
+        int src_len;
+        /** Current lexer's offset in bytes. */
+        int offset;
+        /** Current lexer's offset in symbols. */
+        int symbol_count;
+        /**
+         * Base field offset for emitted JSON_TOKEN_NUM tokens,
+         * e.g. 0 for C and 1 for Lua.
+         */
+        int index_base;
+    };
+
+    enum json_token_type {
+        JSON_TOKEN_NUM,
+        JSON_TOKEN_STR,
+        JSON_TOKEN_ANY,
+        /** Lexer reached end of path. */
+        JSON_TOKEN_END,
+    };
+
+    int
+    json_lexer_next_token(struct json_lexer *lexer, struct json_token *token);
+
+    int
+    json_path_cmp(const char *a, int a_len, const char *b, int b_len,
+            int index_base);
+
+    int
+    json_path_validate(const char *path, int path_len, int index_base);
+
+    int
+    json_path_multikey_offset(const char *path, int path_len, int index_base);
+]])
+
+local test = tap.test('json-features')
+test:plan(1)
+
+local RTLD_DEFAULT
+-- See `man 3 dlsym`:
+-- RTLD_DEFAULT
+--   Find  the  first occurrence of the desired symbol using the default
+--   shared object search order.  The search will include global symbols
+--   in the executable and its dependencies, as well as symbols in shared
+--   objects that were dynamically loaded with the RTLD_GLOBAL flag.
+if jit.os == "OSX" then
+    RTLD_DEFAULT = ffi.cast("void *", -2LL)
+else
+    RTLD_DEFAULT = ffi.cast("void *", 0LL)
+end
+
+local json_symbols = {
+    'json_lexer_next_token',
+    'json_path_cmp',
+    'json_path_validate',
+    'json_path_multikey_offset',
+}
+
+test:test('json_symbols', function(t)
+    t:plan(#json_symbols)
+    for _, sym in ipairs(json_symbols) do
+        t:ok(
+            ffi.C.dlsym(RTLD_DEFAULT, sym) ~= nil,
+            ('Symbol %q found'):format(sym)
+        )
+    end
+end)
+
+os.exit(test:check() and 0 or 1)
-- 
2.23.0

             reply	other threads:[~2020-08-21 16:26 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-21 16:26 olegrok [this message]
2020-08-21 16:50 ` Timur Safin
2020-08-21 21:56 ` Vladislav Shpilevoy
2020-08-24  7:12   ` Oleg Babin

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=20200821162607.68179-1-olegrok@tarantool.org \
    --to=olegrok@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=tsafin@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH] exports: allow to use json tools via FFI' \
    /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