Last active
May 22, 2021 10:17
-
-
Save sayanarijit/29cdad7ba97e2d234eedfe0e66ec30aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Credits: | |
-- https://gist.github.com/bofm/70766566ecd4edee76f8f17ffa7362c9 | |
-- https://stackoverflow.com/questions/21649967/lua-equivalent-of-python-repr | |
local data = {} | |
local function noop(...) | |
return ... | |
end | |
-- convert a nested table to a flat table | |
local function flatten(t, sep, key_modifier, res) | |
if type(t) ~= 'table' then | |
return t | |
end | |
if sep == nil then | |
sep = '.' | |
end | |
if res == nil then | |
res = {} | |
end | |
if key_modifier == nil then | |
key_modifier = noop | |
end | |
for k, v in pairs(t) do | |
if type(v) == 'table' then | |
local v = flatten(v, sep, key_modifier, {}) | |
for k2, v2 in pairs(v) do | |
res[key_modifier(k) .. sep .. key_modifier(k2)] = v2 | |
end | |
else | |
res[key_modifier(k)] = v | |
end | |
end | |
return res | |
end | |
for k, v in pairs(flatten(data)) do | |
print(k .. " = " .. string.format('%q', tostring(v))) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment