Last active
January 20, 2016 13:53
-
-
Save Fizzadar/5076072 to your computer and use it in GitHub Desktop.
Lua table => Lua Code
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
local testconf = { | |
test = 'hi', | |
test2 = { | |
hi = 'ho', | |
what = true, | |
fuck = function() return 'hello' end | |
} | |
} | |
function tableToLua( table ) | |
local out = '' | |
for k, v in pairs( table ) do | |
if type( v ) == 'table' then | |
out = out .. k .. '={' .. tableToLua( v ) .. '},' | |
else | |
if type( v ) == 'function' then v = tostring( v() ) end | |
if type( v ) == 'string' then v = "'" .. v .. "'" end | |
if type( v ) == 'boolean' then v = tostring( v ) end | |
out = out .. k .. '=' .. v .. ',' | |
end | |
end | |
out = out:sub( 0, out:len() - 1 ) | |
return out | |
end | |
function tableToLuaFile( table ) | |
return 'local _lua = {' .. tableToLua( table ) .. '} return _lua' | |
end | |
print( tableToLuaFile( testconf ) ) | |
--output = | |
local _lua = {test='hi',test2={fuck='hello',hi='ho',what=true}} return _lua |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Love the test!! Of course, hopefully some errant mistake never results in fucking "unprofessional" language making its way into production code. Personally, I find that sprinkling funny fucking shit in my tests is the best way to keep my interest. Plus, the test output is fun too; nothing beats an obscenely profane test suite.