[Tarantool-patches] [PATCH v2 2/2] uuid: support uuid comparison with strings

olegrok at tarantool.org olegrok at tarantool.org
Wed Nov 18 10:56:21 MSK 2020


From: Oleg Babin <babinoleg at 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



More information about the Tarantool-patches mailing list