From: olegrok@tarantool.org To: lvasiliev@tarantool.org, v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Subject: [Tarantool-patches] [PATCH] lua: introduce function to check that passed value is uuid Date: Thu, 16 Jul 2020 21:20:32 +0300 [thread overview] Message-ID: <20200716182032.84609-1-olegrok@tarantool.org> (raw) From: Oleg Babin <babinoleg@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
next reply other threads:[~2020-07-16 18:20 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-07-16 18:20 olegrok [this message] 2020-07-27 20:19 ` Vladislav Shpilevoy 2020-07-28 8:23 ` Leonid Vasiliev 2020-07-28 8:34 ` Oleg Babin 2020-07-28 14:59 ` Kirill Yukhin
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=20200716182032.84609-1-olegrok@tarantool.org \ --to=olegrok@tarantool.org \ --cc=lvasiliev@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --cc=v.shpilevoy@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH] lua: introduce function to check that passed value is uuid' \ /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