[Tarantool-patches] [PATCH] lua: introduce function to check that passed value is uuid
olegrok at tarantool.org
olegrok at tarantool.org
Thu Jul 16 21:20:32 MSK 2020
From: Oleg Babin <babinoleg at mail.ru>
We've already have is_decimal function that checks allowed value
is decimal. After tarantool started to support UUID type it will
be quite often case to check that some value has UUID type as
well. This patch introduces "is_uuid" function for this purpose.
Closes #5171
@TarantoolBot document
Title: uuid.is_uuid
is_uuid function returns "true" if specified value has uuid type
and "false" otherwise.
---
Issue: https://github.com/tarantool/tarantool/issues/5171
Branch: https://github.com/tarantool/tarantool/tree/olegrok/5171-is_uuid
@Changelog:
* Introduce function to check that specified value has UUID
type (gh-5171).
src/lua/uuid.lua | 15 ++++++++++-----
test/app/uuid.result | 19 +++++++++++++++++++
test/app/uuid.test.lua | 10 ++++++++++
3 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/src/lua/uuid.lua b/src/lua/uuid.lua
index 5d19a4408..42016601d 100644
--- a/src/lua/uuid.lua
+++ b/src/lua/uuid.lua
@@ -26,8 +26,12 @@ local uuid_t = ffi.typeof('struct tt_uuid')
local UUID_STR_LEN = 36
local UUID_LEN = ffi.sizeof(uuid_t)
+local is_uuid = function(value)
+ return ffi.istype(uuid_t, value)
+end
+
local uuid_tostring = function(uu)
- if not ffi.istype(uuid_t, uu) then
+ if not is_uuid(uu) then
return error('Usage: uuid:str()')
end
return ffi.string(builtin.tt_uuid_str(uu), UUID_STR_LEN)
@@ -56,7 +60,7 @@ local need_bswap = function(order)
end
local uuid_tobin = function(uu, byteorder)
- if not ffi.istype(uuid_t, uu) then
+ if not is_uuid(uu) then
return error('Usage: uuid:bin([byteorder])')
end
if need_bswap(byteorder) then
@@ -81,17 +85,17 @@ local uuid_frombin = function(bin, byteorder)
end
local uuid_isnil = function(uu)
- if not ffi.istype(uuid_t, uu) then
+ if not is_uuid(uu) then
return error('Usage: uuid:isnil()')
end
return builtin.tt_uuid_is_nil(uu)
end
local uuid_eq = function(lhs, rhs)
- if not ffi.istype(uuid_t, rhs) then
+ if not is_uuid(rhs) then
return false
end
- if not ffi.istype(uuid_t, lhs) then
+ if not is_uuid(lhs) then
return error('Usage: uuid == var')
end
return builtin.tt_uuid_is_equal(lhs, rhs)
@@ -133,6 +137,7 @@ return setmetatable({
frombin = uuid_frombin;
bin = uuid_new_bin; -- optimized shortcut for new():bin()
str = uuid_new_str; -- optimized shortcut for new():str()
+ is_uuid = is_uuid;
}, {
__call = uuid_new; -- shortcut for new()
})
diff --git a/test/app/uuid.result b/test/app/uuid.result
index 751865e5f..9fe0e7fb4 100644
--- a/test/app/uuid.result
+++ b/test/app/uuid.result
@@ -272,6 +272,25 @@ uu.str()
uu = nil
---
...
+--
+-- gh-5171: is_uuid function
+--
+uuid.is_uuid(uuid.new())
+---
+- true
+...
+uuid.is_uuid(uuid.new():str())
+---
+- false
+...
+uuid.is_uuid(1)
+---
+- false
+...
+uuid.is_uuid(require('decimal').new('123'))
+---
+- false
+...
uuid = nil
---
...
diff --git a/test/app/uuid.test.lua b/test/app/uuid.test.lua
index ac125cddc..47a96f3c6 100644
--- a/test/app/uuid.test.lua
+++ b/test/app/uuid.test.lua
@@ -98,6 +98,16 @@ uu.bin()
uu.str()
uu = nil
+
+--
+-- gh-5171: is_uuid function
+--
+
+uuid.is_uuid(uuid.new())
+uuid.is_uuid(uuid.new():str())
+uuid.is_uuid(1)
+uuid.is_uuid(require('decimal').new('123'))
+
uuid = nil
test_run:cmd("clear filter")
--
2.23.0
More information about the Tarantool-patches
mailing list