lib/lua: support nixpkg's "lua-inline" type

See #1935
This commit is contained in:
Matt Sturgeon 2024-08-11 21:17:45 +01:00
parent dbf6f7bc99
commit 4eb2ad7db7
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
3 changed files with 24 additions and 1 deletions

View file

@ -87,6 +87,7 @@ let
inherit (helpers.extendedLib) maintainers; inherit (helpers.extendedLib) maintainers;
toLuaObject = helpers.lua.toLua; toLuaObject = helpers.lua.toLua;
mkLuaInline = helpers.lua.mkInline;
}; };
in in
helpers helpers

View file

@ -32,6 +32,13 @@ rec {
# and contain only letters, digits, and underscores. # and contain only letters, digits, and underscores.
isIdentifier = s: !(isKeyword s) && (builtins.match "[A-Za-z_][0-9A-Za-z_]*" s) == [ ]; isIdentifier = s: !(isKeyword s) && (builtins.match "[A-Za-z_][0-9A-Za-z_]*" s) == [ ];
# Alias for nixpkgs lib's `mkLuaInline`,
# but can also convert rawLua to lua-inline
mkInline = v: lib.generators.mkLuaInline (v.__raw or v);
# Whether the value is a lua-inline type
isInline = v: v._type or null == "lua-inline";
# toLua' with default options, aliased as toLuaObject at the top-level # toLua' with default options, aliased as toLuaObject at the top-level
toLua = toLua' { }; toLua = toLua' { };
@ -141,6 +148,8 @@ rec {
value value
else if allowRawValues && value ? __raw then else if allowRawValues && value ? __raw then
value value
else if isInline value then
value
else if isDerivation value then else if isDerivation value then
value value
else if isList value then else if isList value then
@ -227,8 +236,11 @@ rec {
v.__pretty v.val v.__pretty v.val
# apply raw values if allowed # apply raw values if allowed
else if allowRawValues && v ? __raw then else if allowRawValues && v ? __raw then
# TODO: apply indentation to multiline raw values # TODO: deprecate in favour of inline-lua
v.__raw v.__raw
else if isInline v then
# TODO: apply indentation to multiline raw values?
"(${v.expr})"
else else
"{" "{"
+ introSpace + introSpace

View file

@ -65,6 +65,16 @@ let
expected = "<lua code>"; expected = "<lua code>";
}; };
testToLuaObjectInlineLua = {
expr = helpers.toLuaObject (lib.generators.mkLuaInline "<lua code>");
expected = "(<lua code>)";
};
testToLuaObjectInlineLuaNested = {
expr = helpers.toLuaObject { lua = lib.generators.mkLuaInline "<lua code>"; };
expected = "{ lua = (<lua code>) }";
};
testToLuaObjectLuaTableMixingList = { testToLuaObjectLuaTableMixingList = {
expr = helpers.toLuaObject { expr = helpers.toLuaObject {
"__unkeyed...." = "foo"; "__unkeyed...." = "foo";