]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
krprint: feature parity between old table_print and new pretty printer
authorPetr Špaček <petr.spacek@nic.cz>
Fri, 17 Jul 2020 15:18:26 +0000 (17:18 +0200)
committerPetr Špaček <petr.spacek@nic.cz>
Mon, 2 Nov 2020 09:28:54 +0000 (10:28 +0100)
daemon/lua/krprint.lua

index 11c8c76aded66d36acad5eaab23b1a993ef0e52e..44d67c323db0aa087409e598d6d233ccaac7277e 100644 (file)
@@ -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)