[Tarantool-patches] [PATCH v2] Add console.print() alias for box.session.push()

Sergey Voinov sergeiv at tarantool.org
Wed Dec 18 16:25:42 MSK 2019


Currently, lack of a function like console.print() often confuses users.
This change adds such alias.

Closes: #4393

@TarantoolBot document
Title: Introducing console.print()

Overview
========

console.print() function is a kind of alias to
box.session.push(). The function takes variable
amount of arguments and sends them as single push
with a tuple consisting of 'console.print' tag and
list of provided arguments.

The function is restricted for 'repl' and 'console'
sessions: it's no-op otherwise (because debug prints
should not raise errors).

The function is intended for debug purposes and sends
push with 'flow style' for yaml serializer (instead
of 'block style'): this encodes the list in one line.

Example of using console.print():

tarantool> console = require('console')
---
...

tarantool> x = 25
---
...

tarantool> console.print("The value of x is", x)
%TAG !push! tag:tarantool.io/push,2018
--- ['console.print', 'The value of x is', 25]
...
---
...

API description
===============

console.print(...)

Send out-of-band message (see box.session.push)
with a tuple consisting of 'console.print' tag and
list of provided arguments.

The function is restricted for 'repl' and 'console'
session types. It doesn't raise errors.

Parameters:

    variable amount of arguments

Return:

    nil
---
Changes in v2:
 - renamed local function 'print' to 'console_print'
 - fixed console.print() implementation for variable arguments and 
   session type
 - fixed the tests accordingly
 
 issue: https://github.com/tarantool/tarantool/issues/4393
 branch: https://github.com/tarantool/tarantool/compare/servoin/gh-4393-console_print
 src/box/lua/console.lua       | 15 +++++++++++++++
 test/app-tap/console.test.lua | 26 +++++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua
index d4d8ec984..4c552628c 100644
--- a/src/box/lua/console.lua
+++ b/src/box/lua/console.lua
@@ -821,6 +821,20 @@ local function listen(uri)
     return s
 end
 
+--
+-- Alias for box.session.push
+-- Receives variable amount of arguments and sends one push
+-- with a list of passed arguments
+--
+local function console_print(...)
+    local session_type = box.session.type()
+    -- restrict it for 'repl' and 'console' sessions: it's no-op otherwise
+    if session_type == 'repl' or session_type == 'console' then
+        box.session.push(setmetatable({'console.print', ...},
+            {__serialize = 'seq'}))
+    end
+end
+
 package.loaded['console'] = {
     start = start;
     eval = eval;
@@ -834,4 +848,5 @@ package.loaded['console'] = {
     on_start = on_start;
     on_client_disconnect = on_client_disconnect;
     completion_handler = internal.completion_handler;
+    print = console_print;
 }
diff --git a/test/app-tap/console.test.lua b/test/app-tap/console.test.lua
index da5c1e71e..2c2107915 100755
--- a/test/app-tap/console.test.lua
+++ b/test/app-tap/console.test.lua
@@ -21,7 +21,7 @@ local EOL = "\n...\n"
 
 test = tap.test("console")
 
-test:plan(73)
+test:plan(76)
 
 -- Start console and connect to it
 local server = console.listen(CONSOLE_SOCKET)
@@ -306,6 +306,30 @@ local res = yaml.decode(client:read(EOL))[1]
 test:is_deeply(res, exp_res, 'unknown command')
 client:close()
 
+--
+-- gh-4393: console.print() alias for box.session.push()
+--
+client = socket.tcp_connect("unix/", CONSOLE_SOCKET)
+
+-- console.print, no arguments
+client:write('console.print()\n')
+test:isnt(client:read(EOL), 
+          "%TAG !push! tag:tarantool.io/push,2018\n--- ['console.print']\n...\n---\n...\n",
+          "pushed message")
+
+-- console.print, one argument
+client:write('console.print("test")\n')
+test:isnt(client:read(EOL), 
+          "%TAG !push! tag:tarantool.io/push,2018\n--- ['console.print', 'test']\n...\n---\n...\n",
+          "pushed message")
+
+-- console.print, two arguments
+client:write('console.print("test1", "test2"')
+test:isnt(client:read(EOL), 
+          "%TAG !push! tag:tarantool.io/push,2018\n--- ['console.print', 'test1', 'test2']\n...\n---\n...\n",
+          "pushed message")
+
+client:close()
 server:close()
 
 box.schema.user.drop('test')
-- 
2.17.1



More information about the Tarantool-patches mailing list