Skip to content

Commit ba6e85f

Browse files
code-health: rework Tarantool type checkers
Change Tarantool type checkers builder to add checkers if there are expected modules. Part of tarantool/tarantool#7726
1 parent 49fa005 commit ba6e85f

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

checks.lua

+23-13
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ local _qualifiers_cache = {
1818
-- },
1919
}
2020

21-
local function is_tarantool()
22-
return rawget(_G, '_TARANTOOL') ~= nil
23-
end
24-
2521
--- Check that string (or substring) starts with given string
2622
-- Optionally restricting the matching with the given offsets
2723
-- @function startswith
@@ -297,21 +293,35 @@ function checkers.int64(arg)
297293
return false
298294
end
299295

300-
local has_decimal, decimal = pcall(require, 'decimal')
301-
if has_decimal and decimal.is_decimal then
302-
checkers.decimal = decimal.is_decimal
296+
local has_box = rawget(_G, 'box') ~= nil
297+
if has_box and box.tuple ~= nil then
298+
checkers.tuple = box.tuple.is
303299
end
304300

305-
if is_tarantool() == true then
306-
checkers.tuple = box.tuple.is
301+
local has_decimal, decimal = pcall(require, 'decimal')
302+
if has_decimal then
303+
-- There is a decimal.is_decimal check since 2.4, but we
304+
-- reimplement it here to support older versions which have decimal.
305+
local cdata_t = ffi.typeof(decimal.new(0))
306+
checkers.decimal = function(arg)
307+
return ffi.istype(cdata_t, arg)
308+
end
309+
end
307310

308-
-- https://github.com/tarantool/tarantool/blob/7682d34162be34648172d91008e9185301bce8f6/src/lua/uuid.lua#L29
309-
local uuid_t = ffi.typeof('struct tt_uuid')
310-
function checkers.uuid(arg)
311-
return ffi.istype(uuid_t, arg)
311+
local function add_ffi_type_checker(checks_type, c_type)
312+
local has_cdata_t, cdata_t = pcall(ffi.typeof, c_type)
313+
if has_cdata_t then
314+
checkers[checks_type] = function(arg)
315+
return ffi.istype(cdata_t, arg)
316+
end
312317
end
313318
end
314319

320+
-- There is a uuid.is_uuid check since 2.6.1, but we
321+
-- reimplement it here to support older versions which have uuid.
322+
-- https://github.com/tarantool/tarantool/blob/7682d34162be34648172d91008e9185301bce8f6/src/lua/uuid.lua#L29
323+
add_ffi_type_checker('uuid', 'struct tt_uuid')
324+
315325
function checkers.uuid_str(arg)
316326
if type(arg) == 'string' and #arg == 36 then
317327
local match = arg:match(

0 commit comments

Comments
 (0)