Commit 1fcf4384 authored by Carla Goncalves Ourofino's avatar Carla Goncalves Ourofino
Browse files

Nova função sga.util.debug_metatable(t)

Essa função mostra as chaves que existem na tabela e as chaves que
podem ser acessadas na tabela através da sua metatable.

[SOMA-3827][SOMA-4164]
parent e09dfd2c
......@@ -4,6 +4,16 @@ local util = {}
local lfs = require("lfs")
local stat = require("posix.sys.stat")
local format_str = function(s)
s = tostring(s)
local _, n = s:gsub('\n','')
if n > 0 then
return "\n[[\n"..s.."]]" -- block string
else
return s -- line string
end
end
function util.write_file(name, data)
local fd = io.open(name, "w")
if not fd then
......@@ -29,6 +39,34 @@ function util.read_file(name, offset)
return data
end
function util.debug_metatable(tbl, lvl)
lvl = lvl or 0
local indent = (" "):rep(lvl)
local out = {"{\n"}
for k,v in pairs(tbl) do
table.insert(out, indent.." ")
table.insert(out, tostring(k).." = ")
table.insert(out, (type(v) == "table" and util.debug_metatable(v, lvl + 1) or format_str(v)))
table.insert(out, ",\n")
end
local mt = getmetatable(tbl)
if mt and mt.__index and type(mt.__index) == "table" then
for k,v in pairs(mt) do
if k ~= "__index" then
v = tbl[k]
if v and not rawget(tbl, k) then
table.insert(out, indent.." ")
table.insert(out, tostring(k).." = ")
table.insert(out, (type(v) == "table" and util.debug_metatable(v, lvl + 1) or format_str(v)))
table.insert(out, ",\n")
end
end
end
end
table.insert(out, indent.."}")
return table.concat(out)
end
function util.debug_table(tbl, lvl)
lvl = lvl or 0
local indent = (" "):rep(lvl)
......@@ -36,8 +74,8 @@ function util.debug_table(tbl, lvl)
for k,v in pairs(tbl) do
table.insert(out, indent.." ")
table.insert(out, tostring(k).." = ")
table.insert(out, (type(v) == "table" and util.debug_table(v, lvl + 1) or tostring(v)))
table.insert(out, "\n")
table.insert(out, (type(v) == "table" and util.debug_table(v, lvl + 1) or format_str(v)))
table.insert(out, ",\n")
end
table.insert(out, indent.."}")
return table.concat(out)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment