From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp58.i.mail.ru (smtp58.i.mail.ru [217.69.128.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 3351C44643B for ; Mon, 12 Oct 2020 03:51:20 +0300 (MSK) From: Timur Safin Date: Mon, 12 Oct 2020 03:50:37 +0300 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 1.10 v3 1/4] module api: export box_tuple_validate List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: v.shpilevoy@tarantool.org, alexander.turenko@tarantool.org Cc: tarantool-patches@dev.tarantool.org For external merger we need means to validate tuple data, thus exporting `box_tuple_validate` which is wrapper around `tuple_validate_raw` without revealing access to tuple internals. Part of #5384 --- extra/exports | 1 + src/box/tuple.c | 8 ++++++++ src/box/tuple.h | 11 +++++++++++ test/app-tap/module_api.c | 16 ++++++++++++++++ test/app-tap/module_api.test.lua | 25 ++++++++++++++++++++++++- 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/extra/exports b/extra/exports index d99778e56..aa9f5defe 100644 --- a/extra/exports +++ b/extra/exports @@ -177,6 +177,7 @@ box_tuple_upsert box_tuple_extract_key box_tuple_compare box_tuple_compare_with_key +box_tuple_validate box_return_tuple box_space_id_by_name box_index_id_by_name diff --git a/src/box/tuple.c b/src/box/tuple.c index 1db69e414..ab3990ac4 100644 --- a/src/box/tuple.c +++ b/src/box/tuple.c @@ -576,6 +576,14 @@ box_tuple_new(box_tuple_format_t *format, const char *data, const char *end) return tuple_bless(ret); } +int +box_tuple_validate(box_tuple_t *tuple, box_tuple_format_t *format) +{ + return tuple_validate_raw(format, tuple_data(tuple)); +} + +/* }}} box_tuple_* */ + int tuple_snprint(char *buf, int size, const struct tuple *tuple) { diff --git a/src/box/tuple.h b/src/box/tuple.h index 5c6dc6b8c..217c438c5 100644 --- a/src/box/tuple.h +++ b/src/box/tuple.h @@ -284,6 +284,17 @@ box_tuple_t * box_tuple_upsert(const box_tuple_t *tuple, const char *expr, const char *expr_end); +/** + * Check tuple data correspondence to the space format. + * @param tuple Tuple to validate. + * @param format Format to which the tuple must match. + * + * @retval 0 The tuple is valid. + * @retval -1 The tuple is invalid. + */ +int +box_tuple_validate(box_tuple_t *tuple, box_tuple_format_t *format); + /** \endcond public */ /** diff --git a/test/app-tap/module_api.c b/test/app-tap/module_api.c index 4aaf985fa..74a4c4d54 100644 --- a/test/app-tap/module_api.c +++ b/test/app-tap/module_api.c @@ -556,6 +556,21 @@ test_luaT_tuple_encode(struct lua_State *L) /* }}} test_luaT_tuple_encode */ +static int +test_tuple_validate(lua_State *L) +{ + int valid = 0; + box_tuple_t *tuple = luaT_istuple(L, -1); + + if (tuple != NULL) { + box_tuple_format_t *format = box_tuple_format_default(); + valid = box_tuple_validate(tuple, format) == 0; + } + lua_pushboolean(L, valid); + + return 1; +} + LUA_API int luaopen_module_api(lua_State *L) { @@ -584,6 +599,7 @@ luaopen_module_api(lua_State *L) {"test_state", test_state}, {"test_tostring", test_tostring}, {"test_luaT_tuple_encode", test_luaT_tuple_encode}, + {"tuple_validate", test_tuple_validate}, {NULL, NULL} }; luaL_register(L, "module_api", lib); diff --git a/test/app-tap/module_api.test.lua b/test/app-tap/module_api.test.lua index 467000c98..06ecb5470 100755 --- a/test/app-tap/module_api.test.lua +++ b/test/app-tap/module_api.test.lua @@ -36,8 +36,30 @@ local function test_pushcdata(test, module) test:is(gc_counter, 1, 'pushcdata gc') end +local function test_tuples(test, module) + test:plan(8) + + local nottuple1 = {} + local nottuple2 = {1, 2} + local nottuple3 = {1, nil, 2} + local nottuple4 = {1, box.NULL, 2, 3} + local tuple1 = box.tuple.new(nottuple1) + local tuple2 = box.tuple.new(nottuple2) + local tuple3 = box.tuple.new(nottuple3) + local tuple4 = box.tuple.new(nottuple4) + + test:ok(not module.tuple_validate(nottuple1), "not tuple 1") + test:ok(not module.tuple_validate(nottuple2), "not tuple 2") + test:ok(not module.tuple_validate(nottuple3), "not tuple 3") + test:ok(not module.tuple_validate(nottuple4), "not tuple 4") + test:ok(module.tuple_validate(tuple1), "tuple 1") + test:ok(module.tuple_validate(tuple2), "tuple 2") + test:ok(module.tuple_validate(tuple3), "tuple 3") + test:ok(module.tuple_validate(tuple4), "tuple 4") +end + local test = require('tap').test("module_api", function(test) - test:plan(24) + test:plan(25) local status, module = pcall(require, 'module_api') test:is(status, true, "module") test:ok(status, "module is loaded") @@ -62,6 +84,7 @@ local test = require('tap').test("module_api", function(test) test:like(msg, 'luaT_error', 'luaT_error') test:test("pushcdata", test_pushcdata, module) + test:test("validate", test_tuples, module) space:drop() end) -- 2.20.1