helpers/lib: toLuaObject ignore empty attrs (#312)

This commit is contained in:
Gaétan Lepage 2023-04-03 11:25:37 +02:00 committed by GitHub
parent 3d64fab719
commit 35c56b62a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View file

@ -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 =

View file

@ -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