Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ let
inherit (helpers.extendedLib) maintainers;

toLuaObject = helpers.lua.toLua;
mkLuaInline = helpers.lua.mkInline;
};
in
helpers
14 changes: 13 additions & 1 deletion lib/to-lua.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ rec {
# and contain only letters, digits, and underscores.
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 = toLua' { };

Expand Down Expand Up @@ -141,6 +148,8 @@ rec {
value
else if allowRawValues && value ? __raw then
value
else if isInline value then
value
else if isDerivation value then
value
else if isList value then
Expand Down Expand Up @@ -227,8 +236,11 @@ rec {
v.__pretty v.val
# apply raw values if allowed
else if allowRawValues && v ? __raw then
# TODO: apply indentation to multiline raw values
# TODO: deprecate in favour of inline-lua
v.__raw
else if isInline v then
# TODO: apply indentation to multiline raw values?
"(${v.expr})"
else
"{"
+ introSpace
Expand Down
10 changes: 10 additions & 0 deletions tests/lib-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ let
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 = {
expr = helpers.toLuaObject {
"__unkeyed...." = "foo";
Expand Down