From: Sergey Voinov <sergeiv@tarantool.org> To: tarantool-patches@dev.tarantool.org, Alexander Turenko <alexander.turenko@tarantool.org> Subject: [Tarantool-patches] [PATCH v2] Add console.print() alias for box.session.push() Date: Wed, 18 Dec 2019 16:25:42 +0300 [thread overview] Message-ID: <20191218132542.20524-1-sergeiv@tarantool.org> (raw) 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
reply other threads:[~2019-12-18 13:25 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20191218132542.20524-1-sergeiv@tarantool.org \ --to=sergeiv@tarantool.org \ --cc=alexander.turenko@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v2] Add console.print() alias for box.session.push()' \ /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