Tarantool development patches archive
 help / color / mirror / Atom feed
From: olegrok@tarantool.org
To: v.shpilevoy@tarantool.org, lvasiliev@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v2 2/2] uuid: support uuid comparison with strings
Date: Wed, 18 Nov 2020 10:56:21 +0300	[thread overview]
Message-ID: <696b028492d8c1e680a3386c87534d96eac7c272.1605685304.git.babinoleg@mail.ru> (raw)
In-Reply-To: <cover.1605685304.git.babinoleg@mail.ru>

From: Oleg Babin <babinoleg@mail.ru>

Before this patch it was impossible to compare uuid values with
string representations of uuid. However we have cases when such
comparisons is possible (e.g. "decimal" where we can compare
decimal values with strings and numbers).

This patch extends uuid comparators (eq, lt, le) and every string
argument is tried to be converted to uuid value to compare then.

Follow-up #5511

@TarantoolBot document
Title: uuid values could be compared with strings

Currently it's possible to compare uuid values with its string
representations:
```lua
u1_str = 'aaaaaaaa-aaaa-4000-b000-000000000001'
u1 = uuid.fromstr(u1_str)
u2_str = 'bbbbbbbb-bbbb-4000-b000-000000000001'

u1 == u1_str -- true
u1 == u2_str -- false

u1 >= u1_str -- true
u1 < u2_str  -- true
```
---
Issue: https://github.com/tarantool/tarantool/issues/5511
Branch: https://github.com/tarantool/tarantool/tree/olegrok/5511-uuid-cmp-v2

 src/lua/uuid.lua       | 31 +++++++++++++--
 test/app/uuid.result   | 85 ++++++++++++++++++++++++++++++++++++++----
 test/app/uuid.test.lua | 27 ++++++++++++++
 3 files changed, 131 insertions(+), 12 deletions(-)

diff --git a/src/lua/uuid.lua b/src/lua/uuid.lua
index 08991cfeb..1d5d835df 100644
--- a/src/lua/uuid.lua
+++ b/src/lua/uuid.lua
@@ -93,11 +93,33 @@ local uuid_isnil = function(uu)
     return builtin.tt_uuid_is_nil(uu)
 end
 
+local fromstr = function(str)
+    local uu = static_alloc('struct tt_uuid')
+    local rc = builtin.tt_uuid_from_string(str, uu)
+    if rc ~= 0 then
+        return nil
+    end
+    return uu
+end
+
+local to_uuid = function(value)
+    if is_uuid(value) then
+        return value
+    end
+    if type(value) == 'string' then
+        return fromstr(value)
+    end
+    return nil
+end
+
 local uuid_eq = function(lhs, rhs)
-    if not is_uuid(rhs) then
+    rhs = to_uuid(rhs)
+    if rhs == nil then
         return false
     end
-    if not is_uuid(lhs) then
+
+    lhs = to_uuid(lhs)
+    if lhs == nil then
         return error('Usage: uuid == var')
     end
     return builtin.tt_uuid_is_equal(lhs, rhs)
@@ -121,11 +143,12 @@ local uuid_new_str = function()
 end
 
 local check_uuid = function(value, index)
-    if is_uuid(value) then
+    value = to_uuid(value)
+    if value ~= nil then
         return value
     end
 
-    local err_fmt = 'incorrect value to compare with uuid as %d argument'
+    local err_fmt = 'incorrect value to convert to uuid as %d argument'
     error(err_fmt:format(index), 0)
 end
 
diff --git a/test/app/uuid.result b/test/app/uuid.result
index e06331001..5b9ffa230 100644
--- a/test/app/uuid.result
+++ b/test/app/uuid.result
@@ -334,35 +334,35 @@ u1 < u2
 ...
 u1 < 1
 ---
-- error: incorrect value to compare with uuid as 2 argument
+- error: incorrect value to convert to uuid as 2 argument
 ...
 u1 <= 1
 ---
-- error: incorrect value to compare with uuid as 2 argument
+- error: incorrect value to convert to uuid as 2 argument
 ...
 u1 < 'abc'
 ---
-- error: incorrect value to compare with uuid as 2 argument
+- error: incorrect value to convert to uuid as 2 argument
 ...
 u1 <= 'abc'
 ---
-- error: incorrect value to compare with uuid as 2 argument
+- error: incorrect value to convert to uuid as 2 argument
 ...
 1 < u1
 ---
-- error: incorrect value to compare with uuid as 1 argument
+- error: incorrect value to convert to uuid as 1 argument
 ...
 1 <= u1
 ---
-- error: incorrect value to compare with uuid as 1 argument
+- error: incorrect value to convert to uuid as 1 argument
 ...
 'abc' < u1
 ---
-- error: incorrect value to compare with uuid as 1 argument
+- error: incorrect value to convert to uuid as 1 argument
 ...
 'abc' <= u1
 ---
-- error: incorrect value to compare with uuid as 1 argument
+- error: incorrect value to convert to uuid as 1 argument
 ...
 u1 = nil
 ---
@@ -370,6 +370,75 @@ u1 = nil
 u2 = nil
 ---
 ...
+--
+-- allow to compare uuid values with strings
+--
+u1_str = 'aaaaaaaa-aaaa-4000-b000-000000000001'
+---
+...
+u1 = uuid.fromstr(u1_str)
+---
+...
+u2_str = 'bbbbbbbb-bbbb-4000-b000-000000000001'
+---
+...
+u1 == u1_str
+---
+- true
+...
+u1 == u2_str
+---
+- false
+...
+u1_str == u1
+---
+- true
+...
+u2_str == u1
+---
+- false
+...
+u1 > u1_str
+---
+- false
+...
+u1 >= u1_str
+---
+- true
+...
+u1 < u1_str
+---
+- false
+...
+u1 <= u1_str
+---
+- true
+...
+u1 > u2_str
+---
+- false
+...
+u1 >= u2_str
+---
+- false
+...
+u1 < u2_str
+---
+- true
+...
+u1 <= u2_str
+---
+- true
+...
+u1 = nil
+---
+...
+u1_str = nil
+---
+...
+u2_str = nil
+---
+...
 uuid = nil
 ---
 ...
diff --git a/test/app/uuid.test.lua b/test/app/uuid.test.lua
index 34ab38d35..867bbd832 100644
--- a/test/app/uuid.test.lua
+++ b/test/app/uuid.test.lua
@@ -137,6 +137,33 @@ u1 <= 'abc'
 u1 = nil
 u2 = nil
 
+--
+-- allow to compare uuid values with strings
+--
+
+u1_str = 'aaaaaaaa-aaaa-4000-b000-000000000001'
+u1 = uuid.fromstr(u1_str)
+u2_str = 'bbbbbbbb-bbbb-4000-b000-000000000001'
+
+u1 == u1_str
+u1 == u2_str
+u1_str == u1
+u2_str == u1
+
+u1 > u1_str
+u1 >= u1_str
+u1 < u1_str
+u1 <= u1_str
+
+u1 > u2_str
+u1 >= u2_str
+u1 < u2_str
+u1 <= u2_str
+
+u1 = nil
+u1_str = nil
+u2_str = nil
+
 uuid = nil
 
 test_run:cmd("clear filter")
-- 
2.29.0

  parent reply	other threads:[~2020-11-18  7:56 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-18  7:56 [Tarantool-patches] [PATCH v2 0/2] Make uuid values comparable olegrok
2020-11-18  7:56 ` [Tarantool-patches] [PATCH v2 1/2] uuid: support comparison of uuid values in Lua olegrok
2020-11-21 15:17   ` Vladislav Shpilevoy
2020-11-21 19:07     ` Oleg Babin
2020-11-23 21:58       ` Vladislav Shpilevoy
2020-11-24  5:57         ` Oleg Babin
2020-11-24 13:06           ` Igor Munkin
2020-11-27 15:17         ` Oleg Babin
2020-11-24 15:20   ` Igor Munkin
2020-11-24 19:23     ` Oleg Babin
2020-11-18  7:56 ` olegrok [this message]
2020-11-21 15:17   ` [Tarantool-patches] [PATCH v2 2/2] uuid: support uuid comparison with strings Vladislav Shpilevoy
2020-11-21 19:07     ` Oleg Babin
2020-11-24 15:20   ` Igor Munkin
2020-11-24 19:23     ` Oleg Babin
2020-11-18  8:02 ` [Tarantool-patches] [PATCH v2 0/2] Make uuid values comparable Oleg Babin
2020-11-27 22:39 ` Vladislav Shpilevoy
2020-11-27 22:40 ` Vladislav Shpilevoy
2020-12-04  8:13   ` Oleg Babin
2020-12-07 23:04     ` Alexander V. Tikhonov
2020-12-07 23:24 ` Vladislav Shpilevoy

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=696b028492d8c1e680a3386c87534d96eac7c272.1605685304.git.babinoleg@mail.ru \
    --to=olegrok@tarantool.org \
    --cc=lvasiliev@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v2 2/2] uuid: support uuid comparison with strings' \
    /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