From 73ec4e0f5e76f3c21577f52505bbe421658d4f2d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20=C5=A0pa=C4=8Dek?= Date: Fri, 17 Jul 2020 17:18:26 +0200 Subject: [PATCH] krprint: feature parity between old table_print and new pretty printer --- daemon/lua/krprint.lua | 49 ++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/daemon/lua/krprint.lua b/daemon/lua/krprint.lua index 11c8c76ad..44d67c323 100644 --- a/daemon/lua/krprint.lua +++ b/daemon/lua/krprint.lua @@ -19,7 +19,7 @@ function base_class.new(class, on_unrepresentable) end -- format comment with leading/ending whitespace if needed -function base_class.format_note(self, note, ws_prefix, ws_suffix) +function base_class.format_note(_, note, ws_prefix, ws_suffix) if note == nil then return '' else @@ -40,17 +40,28 @@ function base_class.indent_dec(self) self.cur_indent = self.cur_indent - self.indent_step end +function base_class._fallback(self, val) + if self.on_unrepresentable == 'comment' then + return 'nil', string.format('missing %s', val) + elseif self.on_unrepresentable == 'error' then + local key_path_msg + if #self.tab_key_path > 0 then + local key_path = '[' .. table.concat(self.tab_key_path, '][') .. ']' + key_path_msg = string.format(' (found at [%s])', key_path) + else + key_path_msg = '' + end + error(string.format('cannot serialize type %s%s', type(val), key_path_msg), 2) + end +end + function base_class.val2expr(self, val) local val_type = type(val) local val_repr = self[val_type] if val_repr then return val_repr(self, val) - else -- function, thread, userdata - if self.on_unrepresentable == 'comment' then - return 'nil', string.format('missing %s', val) - elseif self.on_unrepresentable == 'error' then - error(string.format('cannot print %s', val_type), 2) - end + else + return self:_fallback(val) end end @@ -166,13 +177,18 @@ function base_class.table(self, tab) if #errors == 0 then -- finally serialize one [key=]?value expression local indent = self:indent_head() + local note if addidx then - item = string.format('%s%s[%s]%s=%s', indent, self:format_note(idxnote, nil, self.key_val_sep), idxexpr, self.key_val_sep, self.key_val_sep) + note = self:format_note(idxnote, nil, self.key_val_sep) + item = string.format('%s%s[%s]%s=%s', + indent, note, + idxexpr, self.key_val_sep, self.key_val_sep) indent = '' end - item = item .. string.format('%s%s%s,', indent, self:format_note(valnote, nil, self.item_sep), valexpr) + note = self:format_note(valnote, nil, self.item_sep) + item = item .. string.format('%s%s%s,', indent, note, valexpr) else - local errmsg = string.format('%s = %s (%s)', + local errmsg = string.format('cannot print %s = %s (%s)', tostring(idx), tostring(val), table.concat(errors, ', ')) @@ -215,9 +231,20 @@ local pprinter_class = { item_sep = '\n', key_val_sep = ' ', __inst_mt = {}, - format_note = function() return '' end, } +-- should be always empty because pretty-printer has fallback for all types +function pprinter_class.format_note() + return '' +end + +function pprinter_class._fallback(self, val) + if self.on_unrepresentable == 'error' then + base_class._fallback(self, val) + end + return tostring(val) +end + -- "function" is a Lua keyword so assignment below is workaround to create -- function pprinter_class.function(self, f) pprinter_class['function'] = function(self, f) -- 2.47.2