[PATCH 3/7] box/console: Add explicit output EOS mapping

Cyrill Gorcunov gorcunov at gmail.com
Fri Oct 4 10:49:22 MSK 2019


This will help us to distinguish end of string/stream
in text protocols (such as remote console connection).

Note that we start printing ";" terminator for every
lua output. Actually current yaml output does the
same but inside console.c module.

And since lua output is yet a new feature in stabilization
phase we're safe to make such changes without breaking api.

Part-of #3834

Reviewed-by: Konstantin Osipov <kostja.osipov at gmail.com>
Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
 src/box/lua/console.lua | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua
index d40b12e5b..b8c6d8eb2 100644
--- a/src/box/lua/console.lua
+++ b/src/box/lua/console.lua
@@ -11,7 +11,6 @@ local urilib = require('uri')
 local yaml = require('yaml')
 local net_box = require('net.box')
 
-local YAML_TERM = '\n...\n'
 local PUSH_TAG_HANDLE = '!push!'
 
 --
@@ -20,6 +19,18 @@ local PUSH_TAG_HANDLE = '!push!'
 local default_output_format = { ["fmt"] = "yaml", ["opts"] = nil }
 local output_handlers = { }
 
+--
+-- This is an end-of-stream marker for text output of Tarantool
+-- server, for different output formats - yaml and Lua.
+--
+-- While for yaml ... is a standard stream end marker, using ';'
+-- for Lua output we're walking on a thin ice - ';' is legal inside
+-- a valid Lua block as well. But since this marker is used when
+-- reading data created by a Tarantool server, we can rely on it not
+-- being present inside of stream, since Tarantool server never
+-- puts it inside of a stream.
+local output_eos = { ["yaml"] = '\n...\n', ["lua"] = ';' }
+
 output_handlers["yaml"] = function(status, opts, ...)
     local err
     if status then
@@ -66,13 +77,15 @@ end
 
 output_handlers["lua"] = function(status, opts, ...)
     --
-    -- Don't print nil if there is no data
+    -- If no data present at least EOS should be put,
+    -- otherwise wire readers won't be able to find
+    -- where the end of string is.
     if not ... then
-        return ""
+        return output_eos["lua"]
     end
     for k,v in pairs(lua_map_direct_symbols) do
         if k == ... then
-            return v
+            return v .. output_eos["lua"]
         end
     end
     local serpent_opts = {
@@ -81,9 +94,9 @@ output_handlers["lua"] = function(status, opts, ...)
         nocode = true,
     }
     if opts == "block" then
-        return serpent.block(..., serpent_opts)
+        return serpent.block(..., serpent_opts) .. output_eos["lua"]
     end
-    return serpent.line(..., serpent_opts)
+    return serpent.line(..., serpent_opts) .. output_eos["lua"]
 end
 
 local function output_verify_opts(fmt, opts)
@@ -315,7 +328,7 @@ local text_connection_mt = {
         -- @retval     nil Error.
         --
         read = function(self)
-            local ret = self._socket:read(YAML_TERM)
+            local ret = self._socket:read(output_eos["yaml"])
             if ret and ret ~= '' then
                 return ret
             end
-- 
2.20.1




More information about the Tarantool-patches mailing list