diff --git a/lib/to-lua.nix b/lib/to-lua.nix index bcdcd56f..3fcda4bd 100644 --- a/lib/to-lua.nix +++ b/lib/to-lua.nix @@ -47,16 +47,18 @@ rec { mapAttrsToList ( n: v: let + keyString = + if n == "__emptyString" then + "['']" + else if hasPrefix "__rawKey__" n then + "[${removePrefix "__rawKey__" n}]" + else if isIdentifier n then + n + else + "[${toLuaObject n}]"; valueString = toLuaObject v; in - if hasPrefix "__unkeyed" n then - valueString - else if hasPrefix "__rawKey__" n then - ''[${removePrefix "__rawKey__" n}] = '' + valueString - else if n == "__emptyString" then - "[''] = " + valueString - else - "[${toLuaObject n}] = " + valueString + if hasPrefix "__unkeyed" n then valueString else "${keyString} = ${valueString}" ) (filterAttrs (n: v: v != null && (toLuaObject v != "{}")) args) )) + "}" diff --git a/tests/lib-tests.nix b/tests/lib-tests.nix index 583087f8..e89d97ba 100644 --- a/tests/lib-tests.nix +++ b/tests/lib-tests.nix @@ -55,7 +55,7 @@ let 3 ]; }; - expected = ''{["foo"] = "bar",["qux"] = {1,2,3}}''; + expected = ''{foo = "bar",qux = {1,2,3}}''; }; testToLuaObjectRawLua = { @@ -68,7 +68,7 @@ let "__unkeyed...." = "foo"; bar = "baz"; }; - expected = ''{"foo",["bar"] = "baz"}''; + expected = ''{"foo",bar = "baz"}''; }; testToLuaObjectNestedAttrs = { @@ -81,7 +81,7 @@ let }; }; }; - expected = ''{["a"] = {["b"] = 1,["c"] = 2,["d"] = {["e"] = 3}}}''; + expected = ''{a = {b = 1,c = 2,d = {e = 3}}}''; }; testToLuaObjectNestedList = { @@ -109,7 +109,7 @@ let d = false; e = null; }; - expected = ''{["a"] = 1.000000,["b"] = 2,["c"] = true,["d"] = false}''; + expected = ''{a = 1.000000,b = 2,c = true,d = false}''; }; testToLuaObjectNilPrim = { @@ -150,7 +150,17 @@ let g = helpers.emptyTable; }; }; - expected = ''{["c"] = { },["d"] = {["g"] = { }}}''; + expected = ''{c = { },d = {g = { }}}''; + }; + + testToLuaObjectQuotedKeys = { + expr = helpers.toLuaObject { + "1_a" = "a"; + _b = "b"; + c = "c"; + d-d = "d"; + }; + expected = ''{["1_a"] = "a",_b = "b",c = "c",["d-d"] = "d"}''; }; testIsLuaKeyword = {