]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
sandbox: table_print sorts table keys
authorPetr Špaček <petr.spacek@nic.cz>
Sat, 16 Mar 2019 10:06:21 +0000 (11:06 +0100)
committerPetr Špaček <petr.spacek@nic.cz>
Wed, 10 Apr 2019 12:23:03 +0000 (14:23 +0200)
This makes it much easier to navigate in complex data structures.
AFAIK table_print is not used for anything except user interface so it
is not performance critical and we can re-sort table every time.

daemon/lua/sandbox.lua.in

index 562d1d7dcdc47006c558774b3fdb8335d24d071d..0d27f5477f2abcd3cf75807dd3488ade4b482a04 100644 (file)
@@ -359,6 +359,20 @@ function table_print (tt, indent, done)
        indent = indent or 0
        local result = ""
        -- Convert to printable string (escape unprintable)
+       local function ordered_iter(unordered_tt)
+               local keys = {}
+               for k in pairs(unordered_tt) do
+                       table.insert(keys, tostring(k))
+               end
+               table.sort(keys)
+               local i = 0
+               return function()
+                       i = i + 1
+                       if keys[i] then
+                               return keys[i], unordered_tt[keys[i]]
+                       end
+               end
+       end
        local function printable(value)
                value = tostring(value)
                local bytes = {}
@@ -372,7 +386,7 @@ function table_print (tt, indent, done)
                return table.concat(bytes)
        end
        if type(tt) == "table" then
-               for key, value in pairs (tt) do
+               for key, value in ordered_iter(tt) do
                        result = result .. string.rep (" ", indent)
                        if type (value) == "table" and not done [value] then
                                done [value] = true