From 70e993232e2f3c8bafc08d41e8ad5b317dbc4872 Mon Sep 17 00:00:00 2001
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Date: Wed, 15 May 2019 17:21:31 +0300
Subject: [PATCH] swim: allow to use cdata struct tt_uuid in Lua API

Sometimes, especially in tests, it is useful to make something
like this:

    s:add_member({uuid = member:uuid(), uri = member:uri()})

But member:uuid() is cdata struct tt_uuid. This commit allows
that.

Part of #3234
---
 src/lua/swim.lua        |  5 ++++-
 test/swim/swim.result   | 17 ++++++++++++++---
 test/swim/swim.test.lua |  3 +++
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/lua/swim.lua b/src/lua/swim.lua
index d7dbd36f59..b95fe0af2c 100644
--- a/src/lua/swim.lua
+++ b/src/lua/swim.lua
@@ -194,7 +194,10 @@ local function swim_check_uuid(value, func_name)
         return nil
     end
     if type(value) ~= 'string' then
-        return error(func_name..': expected string UUID')
+        if ffi.istype('struct tt_uuid', value) then
+            return value
+        end
+        return error(func_name..': expected string UUID or struct tt_uuid')
     end
     value = uuid.fromstr(value)
     if not value then
diff --git a/test/swim/swim.result b/test/swim/swim.result
index 28135bdceb..863f8b9d87 100644
--- a/test/swim/swim.result
+++ b/test/swim/swim.result
@@ -45,7 +45,7 @@ swim.new({gc_mode = 0})
 ...
 swim.new({uuid = 123})
 ---
-- error: 'builtin/swim.lua:<line>: swim:cfg: expected string UUID'
+- error: 'builtin/swim.lua:<line>: swim:cfg: expected string UUID or struct tt_uuid'
 ...
 swim.new({uuid = '1234'})
 ---
@@ -256,7 +256,8 @@ s1.remove_member()
 ...
 s1:remove_member(100)
 ---
-- error: 'builtin/swim.lua:<line>: swim:remove_member: expected string UUID'
+- error: 'builtin/swim.lua:<line>: swim:remove_member: expected string UUID or struct
+    tt_uuid'
 ...
 s1:remove_member('1234')
 ---
@@ -484,12 +485,22 @@ s1:member_by_uuid(uuid(1)) ~= nil
 ...
 s1:member_by_uuid(50)
 ---
-- error: 'builtin/swim.lua:<line>: swim:member_by_uuid: expected string UUID'
+- error: 'builtin/swim.lua:<line>: swim:member_by_uuid: expected string UUID or struct
+    tt_uuid'
 ...
 s1:member_by_uuid(uuid(2))
 ---
 - null
 ...
+-- UUID can be cdata.
+s1:member_by_uuid(s:uuid())
+---
+- uri: 127.0.0.1:<port>
+  status: alive
+  incarnation: 1
+  uuid: 00000000-0000-1000-8000-000000000001
+  payload_size: 0
+...
 s1:quit()
 ---
 ...
diff --git a/test/swim/swim.test.lua b/test/swim/swim.test.lua
index 48e3d30f61..9d9c87c4a9 100644
--- a/test/swim/swim.test.lua
+++ b/test/swim/swim.test.lua
@@ -160,6 +160,9 @@ s1:member_by_uuid(uuid(1)) ~= nil
 s1:member_by_uuid(50)
 s1:member_by_uuid(uuid(2))
 
+-- UUID can be cdata.
+s1:member_by_uuid(s:uuid())
+
 s1:quit()
 s:status()
 s:is_dropped()
-- 
GitLab