[Tarantool-patches] [PATCH v2] net.box: add __serialize and __tostring methods for future objects

Vladimir Davydov vdavydov at tarantool.org
Tue Aug 17 11:00:50 MSK 2021


__tostring always returns the type name, which is 'net.box.request'.

The output of __serialize depends on whether there are user-defined
fields attached to the future. If yes, it returns the fields in a table,
otherwise it falls back on __tostring.

Closes #6314
---
https://github.com/tarantool/tarantool/issues/6314
https://github.com/tarantool/tarantool/tree/vdavydov/netbox-future-serialize

Changes in v2:
 - Remove 'sync' from the __tostring output.
 - Remove inaccessible fields from the __serialize output.
   Return user-defined fields instead if any.

 src/box/lua/net_box.c                         | 25 ++++++++++++++
 test/box/net.box_fiber-async_gh-3107.result   | 33 +++++++++++++++++++
 test/box/net.box_fiber-async_gh-3107.test.lua | 12 +++++++
 3 files changed, 70 insertions(+)

diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c
index 1783da607dcc..3ed464a93437 100644
--- a/src/box/lua/net_box.c
+++ b/src/box/lua/net_box.c
@@ -1432,6 +1432,29 @@ luaT_netbox_request_gc(struct lua_State *L)
 	return 0;
 }
 
+static int
+luaT_netbox_request_tostring(struct lua_State *L)
+{
+	lua_pushstring(L, netbox_request_typename);
+	return 1;
+}
+
+static int
+luaT_netbox_request_serialize(struct lua_State *L)
+{
+	struct netbox_request *request = luaT_check_netbox_request(L, 1);
+	/*
+	 * If there are user-defined fields attached to the future object,
+	 * return them, otherwise push the type name, like __tostring does.
+	 */
+	if (request->index_ref != LUA_NOREF) {
+		lua_rawgeti(L, LUA_REGISTRYINDEX, request->index_ref);
+	} else {
+		lua_pushstring(L, netbox_request_typename);
+	}
+	return 1;
+}
+
 static int
 luaT_netbox_request_index(struct lua_State *L)
 {
@@ -2107,6 +2130,8 @@ luaopen_net_box(struct lua_State *L)
 
 	static const struct luaL_Reg netbox_request_meta[] = {
 		{ "__gc",           luaT_netbox_request_gc },
+		{"__tostring",      luaT_netbox_request_tostring },
+		{"__serialize",     luaT_netbox_request_serialize },
 		{ "__index",        luaT_netbox_request_index },
 		{ "__newindex",     luaT_netbox_request_newindex },
 		{ "is_ready",       luaT_netbox_request_is_ready },
diff --git a/test/box/net.box_fiber-async_gh-3107.result b/test/box/net.box_fiber-async_gh-3107.result
index ae1c2c308092..450934cd6593 100644
--- a/test/box/net.box_fiber-async_gh-3107.result
+++ b/test/box/net.box_fiber-async_gh-3107.result
@@ -251,6 +251,39 @@ gc.data3 == nil
 ---
 - true
 ...
+--
+-- __tostring and __serialize future methods
+--
+future = c:eval('return 123', {}, {is_async = true})
+---
+...
+tostring(future)
+---
+- net.box.request
+...
+future
+---
+- net.box.request
+...
+future.abc = 123
+---
+...
+future.xyz = 'abc'
+---
+...
+tostring(future)
+---
+- net.box.request
+...
+future
+---
+- xyz: abc
+  abc: 123
+...
+future:wait_result()
+---
+- [123]
+...
 box.schema.user.revoke('guest', 'execute', 'universe')
 ---
 ...
diff --git a/test/box/net.box_fiber-async_gh-3107.test.lua b/test/box/net.box_fiber-async_gh-3107.test.lua
index b6bf35ca2fa1..f93d962ae88e 100644
--- a/test/box/net.box_fiber-async_gh-3107.test.lua
+++ b/test/box/net.box_fiber-async_gh-3107.test.lua
@@ -89,6 +89,18 @@ _ = collectgarbage('collect')
 _ = collectgarbage('collect')
 gc.data3 == nil
 
+--
+-- __tostring and __serialize future methods
+--
+future = c:eval('return 123', {}, {is_async = true})
+tostring(future)
+future
+future.abc = 123
+future.xyz = 'abc'
+tostring(future)
+future
+future:wait_result()
+
 box.schema.user.revoke('guest', 'execute', 'universe')
 
 c:close()
-- 
2.25.1



More information about the Tarantool-patches mailing list