diff --git a/lib/helpers.nix b/lib/helpers.nix index a5f6f9f0..7ef8b802 100644 --- a/lib/helpers.nix +++ b/lib/helpers.nix @@ -12,6 +12,8 @@ with lib; rec { then if hasAttr "__raw" args then args.__raw + else if hasAttr "__empty" args + then "{ }" else "{" + (concatStringsSep "," @@ -20,7 +22,12 @@ with lib; rec { if head (stringToCharacters n) == "@" then toLuaObject v else "[${toLuaObject n}] = " + (toLuaObject v)) - (filterAttrs (n: v: !isNull v) args))) + (filterAttrs + ( + n: v: + !isNull v && (toLuaObject v != "{}") + ) + args))) + "}" else if builtins.isList args then "{" + concatMapStringsSep "," toLuaObject args + "}" @@ -40,6 +47,8 @@ with lib; rec { then "nil" else ""; + emptyTable = {"__empty" = null;}; + # Generates maps for a lua config genMaps = mode: maps: let normalized = diff --git a/tests/lib-tests.nix b/tests/lib-tests.nix index 2ddb4c33..d7fc535a 100644 --- a/tests/lib-tests.nix +++ b/tests/lib-tests.nix @@ -68,7 +68,7 @@ expected = ''"foo\\bar\nbaz"''; }; - testToLuaObjectShouldFilterNullAttrs = { + testToLuaObjectFilters = { expr = helpers.toLuaObject { a = null; b = {}; @@ -78,7 +78,21 @@ f = {}; }; }; - expected = ''{["b"] = {},["c"] = {},["d"] = {["f"] = {}}}''; + expected = ''{}''; + }; + + testToLuaObjectEmptyTable = { + expr = helpers.toLuaObject { + a = null; + b = {}; + c = {__empty = null;}; + d = { + e = null; + f = {}; + g = helpers.emptyTable; + }; + }; + expected = ''{["c"] = { },["d"] = {["g"] = { }}}''; }; }; in