lib/to-lua: fix removing empties nested in lists

- Add options for removing empty/null list entries (default false)
- Fix recursion into attrs that are themselves list entries

Fixes #1804
This commit is contained in:
Matt Sturgeon 2024-07-03 15:53:51 +01:00
parent 87f50db84d
commit 9b25eaaa6f
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
2 changed files with 125 additions and 11 deletions

View file

@ -137,6 +137,55 @@ let
expected = ''{ }'';
};
testToLuaObjectAttrListFilters = {
expr = helpers.toLuaObject {
a = null;
b = { };
c = [ ];
d = {
e = null;
f = { };
};
g = [
{
x = null;
y = null;
z = null;
}
{
x = { };
y = [ ];
z = null;
}
];
__unkeyed-1 = [
{
x = null;
y = null;
z = null;
}
{
x = { };
y = [ ];
z = null;
}
[
{
x = null;
y = null;
z = null;
}
{
x = { };
y = [ ];
z = null;
}
]
];
};
expected = ''{ { { }, { }, { { }, { } } }, g = { { }, { } } }'';
};
testToLuaObjectEmptyTable = {
expr = helpers.toLuaObject {
a = null;
@ -153,6 +202,47 @@ let
expected = ''{ c = { }, d = { g = { } } }'';
};
testToLuaObjectEmptyListEntries = {
expr = helpers.lua.toLua' { removeEmptyListEntries = true; } [
{ }
[ ]
[ { } ]
null
1
2
[
null
3
4
{ }
[ ]
[ { } ]
]
5
];
expected = "{ nil, 1, 2, { nil, 3, 4 }, 5 }";
};
testToLuaObjectNullListEntries = {
expr = helpers.lua.toLua' { removeNullListEntries = true; } [
null
1
2
[
null
3
4
[
null
5
6
]
]
7
];
expected = "{ 1, 2, { 3, 4, { 5, 6 } }, 7 }";
};
testIsLuaKeyword = {
expr = builtins.mapAttrs (_: builtins.filter helpers.lua.isKeyword) luaNames;
expected = {