From: Chris Sosnin <k.sosnin@tarantool.org> To: tarantool-patches@dev.tarantool.org, v.shpilevoy@tarantool.org Subject: [Tarantool-patches] [PATCH] box: fix formatting in session.push Date: Fri, 6 Mar 2020 17:03:34 +0300 [thread overview] Message-ID: <20200306140334.22908-1-k.sosnin@tarantool.org> (raw) box.session.push() encodes data as a YAML document independent on the current console output format. This patch handles lua case in the following way: tarantool>box.session.push(<data>) <data> true; Closes #4686 --- issue: https://github.com/tarantool/tarantool/issues/4686 branch: https://github.com/tarantool/tarantool/tree/ksosnin/gh-4686-session-push-fmt src/box/lua/console.c | 62 ++++++++++++++++++++++++++--------- src/box/lua/console.lua | 6 ++++ test/app-tap/console.test.lua | 14 +++++++- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/box/lua/console.c b/src/box/lua/console.c index 57e7e7f4f..a7573c306 100644 --- a/src/box/lua/console.c +++ b/src/box/lua/console.c @@ -50,6 +50,11 @@ extern char serpent_lua[]; static struct luaL_serializer *luaL_yaml_default = NULL; +enum { + OUTPUT_FORMAT_YAML, + OUTPUT_FORMAT_LUA, +}; + /* * Completion engine (Mike Paul's). * Used internally when collecting completions locally. Also a Lua @@ -377,9 +382,28 @@ console_session_fd(struct session *session) return session->meta.fd; } +static int +console_current_output(struct lua_State *L) +{ + lua_getfield(L, LUA_GLOBALSINDEX, "box"); + lua_getfield(L, -1, "session"); + lua_getfield(L, -1, "storage"); + lua_getfield(L, -1, "console_output_format"); + if (lua_isnil(L, -1)) { + lua_pop(L, 4); + return OUTPUT_FORMAT_YAML; + } + lua_getfield(L, -1, "fmt"); + int cmp = strcmp(lua_tostring(L, -1), "lua"); + lua_pop(L, 5); + if (cmp == 0) + return OUTPUT_FORMAT_LUA; + return OUTPUT_FORMAT_YAML; +} + /** - * Dump port lua data as a YAML document tagged with !push! global - * tag. + * Dump port lua data with respect to output format: + * YAML document tagged with !push! global tag or lua string. * @param port Port lua. * @param[out] size Size of the result. * @@ -391,19 +415,27 @@ port_lua_dump_plain(struct port *port, uint32_t *size) { struct port_lua *port_lua = (struct port_lua *) port; struct lua_State *L = port_lua->L; - int rc = lua_yaml_encode(L, luaL_yaml_default, "!push!", - "tag:tarantool.io/push,2018"); - if (rc == 2) { - /* - * Nil and error object are pushed onto the stack. - */ - assert(lua_isnil(L, -2)); + int fmt = console_current_output(L); + if (fmt == OUTPUT_FORMAT_YAML) { + int rc = lua_yaml_encode(L, luaL_yaml_default, "!push!", + "tag:tarantool.io/push,2018"); + if (rc == 2) { + /* + * Nil and error object are pushed onto the stack. + */ + assert(lua_isnil(L, -2)); + assert(lua_isstring(L, -1)); + diag_set(ClientError, ER_PROC_LUA, lua_tostring(L, -1)); + return NULL; + } + assert(rc == 1); assert(lua_isstring(L, -1)); - diag_set(ClientError, ER_PROC_LUA, lua_tostring(L, -1)); - return NULL; + } else { /* OUTPUT_FORMAT_LUA */ + luaL_findtable(L, LUA_GLOBALSINDEX, "box.internal", 1); + lua_getfield(L, -1, "format_lua_value"); + lua_pushvalue(L, -3); + lua_call(L, 1, 1); } - assert(rc == 1); - assert(lua_isstring(L, -1)); size_t len; const char *result = lua_tolstring(L, -1, &len); *size = (uint32_t) len; @@ -411,10 +443,10 @@ port_lua_dump_plain(struct port *port, uint32_t *size) } /** - * Push a tagged YAML document into a console socket. + * Push a tagged YAML document or a plain text into a console socket. * @param session Console session. * @param sync Unused request sync. - * @param port Port with YAML to push. + * @param port Port with the data to push. * * @retval 0 Success. * @retval -1 Error. diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua index 17e2c91b2..cdf64b063 100644 --- a/src/box/lua/console.lua +++ b/src/box/lua/console.lua @@ -12,6 +12,7 @@ local errno = require('errno') local urilib = require('uri') local yaml = require('yaml') local net_box = require('net.box') +local box_internal = require('box.internal') local PUSH_TAG_HANDLE = '!push!' @@ -96,6 +97,11 @@ local function format_lua_value(value, opts) return serpent.line(value, serpent_opts) end +box_internal.format_lua_value = function(value) -- for console_session_push + value = format_lua_value(value) + return value .. '\n' +end + output_handlers["lua"] = function(status, opts, ...) local collect = {} -- diff --git a/test/app-tap/console.test.lua b/test/app-tap/console.test.lua index da5c1e71e..71c482bf7 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(74) -- Start console and connect to it local server = console.listen(CONSOLE_SOCKET) @@ -39,6 +39,18 @@ test:is(client:read(EOL), '%TAG !push! tag:tarantool.io/push,2018\n--- 200\n...\ "pushed message") test:is(client:read(EOL), '---\n- true\n...\n', "pushed message") +-- +-- gh-4686: box.session.push should respect output format. +-- +client:write('\\set output lua\n') +client:read(";") + +client:write('box.session.push({ { [\'field\'] = 100 }, { 1, 2, 3 }, \'abc\' })\n') +test:is(client:read(";"), '{{field = 100}, {1, 2, 3}, "abc"}\ntrue;', "pushed message") + +client:write('\\set output yaml\n') +client:read(EOL) + -- -- gh-3790: box.session.push support uint64_t sync. -- -- 2.21.1 (Apple Git-122.3)
next reply other threads:[~2020-03-06 14:03 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-03-06 14:03 Chris Sosnin [this message] 2020-03-09 23:13 ` Vladislav Shpilevoy 2020-03-13 14:56 ` Chris Sosnin 2020-03-27 10:28 Chris Sosnin
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=20200306140334.22908-1-k.sosnin@tarantool.org \ --to=k.sosnin@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --cc=v.shpilevoy@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH] box: fix formatting in 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