add concat and concatLists

- rename mkLuaRaw to raw
- rename mkCall to call
- rename mkNamedField to namedField
This commit is contained in:
Dmitriy Pleshevskiy 2024-04-17 10:56:41 +03:00
parent 0102c7e7ab
commit eea936e1c5
Signed by: pleshevskiy
GPG Key ID: 17041163DA10A9A2
3 changed files with 58 additions and 39 deletions

View File

@ -1,5 +1,5 @@
# Run all tests # Run all tests
test: test:
nix eval --impure --expr 'import ./lib.test.nix {}' nix eval --impure --expr 'import ./lib.test.nix {}' $(TEST_ARGS)

72
lib.nix
View File

@ -24,6 +24,7 @@ let
error = message: throw "[nix2lua] ${message}"; error = message: throw "[nix2lua] ${message}";
warn = msg: builtins.trace "[nix2lua] warning: ${msg}"; warn = msg: builtins.trace "[nix2lua] warning: ${msg}";
deprecated = before: now: warn "`${before}` is deprecated. Use `${now}` instead";
getType = expr: if isAttrs expr && expr ? _type then expr._type else null; getType = expr: if isAttrs expr && expr ? _type then expr._type else null;
@ -40,66 +41,71 @@ let
if isList expr then { _type = "_join"; sep = validString sep; parts = expr; } if isList expr then { _type = "_join"; sep = validString sep; parts = expr; }
else error "Value '${toString expr}' is not supported for a join type"; else error "Value '${toString expr}' is not supported for a join type";
isLuaRaw = expr: getType expr == "raw"; concat = join "";
mkLuaRaw = expr: concatLines = lines:
if isLuaRaw expr then join "\n" (
lines
# add an empty line at the end
++ [ (raw "") ]
);
isRaw = expr: getType expr == "raw";
raw = expr:
if isRaw expr then
{ _type = "raw"; raw = expr.raw; } { _type = "raw"; raw = expr.raw; }
else if isString expr then else if isString expr then
{ _type = "raw"; raw = expr; } { _type = "raw"; raw = expr; }
else else
error "Value '${toString expr}' is not supported for a raw type"; error "Value '${toString expr}' is not supported for a raw type";
mkCall = fnName: args: call = fnName: args:
let let
luaFn = luaFn =
if isString fnName && builtins.stringLength fnName > 0 then mkLuaRaw fnName if isString fnName && builtins.stringLength fnName > 0 then raw fnName
else error "Value '${toString fnName}' is not a valid function name"; else error "Value '${toString fnName}' is not a valid function name";
in in
join "" ( concat (
[ luaFn (mkLuaRaw "(") ] [ luaFn (raw "(") ]
++ [ (join ", " args) ] ++ [ (join ", " args) ]
++ [ (mkLuaRaw ")") ] ++ [ (raw ")") ]
); );
toLuaStr = expr: "\"${validString expr}\"";
toLuaBool = expr: if expr then "true" else "false";
isLuaNil = expr: getType expr == "nil"; isLuaNil = expr: getType expr == "nil";
LuaNil = { _type = "nil"; }; LuaNil = { _type = "nil"; };
# DEPRECATED
mkLuaNil = warn "`mkLuaNil` is deprecated. Use `LuaNil` instead" LuaNil;
isNamedField = expr: getType expr == "table_field";
namedField = name: expr: {
_type = "table_field";
name = validString name;
value = toLua expr;
};
toLuaBool = expr: if expr then "true" else "false";
toLuaNumber = toString;
toLuaString = expr: "\"${validString expr}\"";
toLuaList = onValue: expr: toLuaList = onValue: expr:
let let
wrapObj = expr: "{ ${concatStringsSep ", " expr} }"; wrapObj = expr: "{ ${concatStringsSep ", " expr} }";
excludeNull = expr: filter (v: !(isNull v)) expr; excludeNull = expr: filter (v: !(isNull v)) expr;
in in
wrapObj (excludeNull (map onValue expr)); wrapObj (excludeNull (map onValue expr));
toLuaTable = onValue: expr: onValue (attrValues (mapAttrs mkNamedField expr));
mkNamedField = name: expr: {
_type = "table_field";
name = validString name;
value = toLua expr;
};
isNamedField = expr: getType expr == "table_field";
toLuaNamedField = name: expr: toLuaNamedField = name: expr:
if isNull expr then null if isNull expr then null
else "[${toLuaStr name}] = ${expr}"; else "[${toLuaString name}] = ${expr}";
toLuaTable = onValue: expr: onValue (attrValues (mapAttrs namedField expr));
toLuaInternal = depth: expr: toLuaInternal = depth: expr:
let nextDepth = depth + 1; in let nextDepth = depth + 1; in
if isJoin expr then concatStringsSep expr.sep (map (toLuaInternal depth) expr.parts) if isJoin expr then concatStringsSep expr.sep (map (toLuaInternal depth) expr.parts)
else if isLuaNil expr then "nil" else if isLuaNil expr then "nil"
else if isLuaRaw expr then expr.raw else if isRaw expr then expr.raw
else if isNamedField expr then else if isNamedField expr then
if depth > 0 then toLuaNamedField expr.name expr.value if depth > 0 then toLuaNamedField expr.name expr.value
else error "You cannot render table field at the top level" else error "You cannot render table field at the top level"
else if isAttrs expr then toLuaTable (toLuaInternal nextDepth) expr else if isAttrs expr then toLuaTable (toLuaInternal nextDepth) expr
else if isList expr then toLuaList (toLuaInternal nextDepth) expr else if isList expr then toLuaList (toLuaInternal nextDepth) expr
else if isString expr || isPath expr then toLuaStr expr else if isString expr || isPath expr then toLuaString expr
else if isFloat expr || isInt expr then toString expr else if isFloat expr || isInt expr then toLuaNumber expr
else if isBool expr then toLuaBool expr else if isBool expr then toLuaBool expr
else if isNull expr then null else if isNull expr then null
else error "Value '${toString expr}' is not supported yet"; else error "Value '${toString expr}' is not supported yet";
@ -108,11 +114,15 @@ let
in in
{ {
# low-level # Deprecated
inherit join; mkLuaNil = deprecated "mkLuaNil" "Nil" LuaNil;
mkLuaRaw = deprecated "mkLuaRaw" "raw" raw;
mkCall = deprecated "mkCall" "call" call;
mkNamedField = deprecated "mkNamedField" "namedField" namedField;
inherit toLua; inherit toLua;
inherit LuaNil mkLuaRaw mkNamedField mkCall;
# DEPRECATED inherit LuaNil;
inherit mkLuaNil; inherit raw join concat concatLines;
inherit call namedField;
} }

View File

@ -18,12 +18,11 @@
let let
nix2lua = import ./lib.nix; nix2lua = import ./lib.nix;
inherit (nix2lua) toLua LuaNil mkLuaRaw mkNamedField mkCall;
inherit (builtins) tryEval; inherit (builtins) tryEval;
failed = { success = false; value = false; }; failed = { success = false; value = false; };
in in
pkgs.lib.runTests { with nix2lua; pkgs.lib.runTests {
"test returns null" = { "test returns null" = {
expr = toLua null; expr = toLua null;
expected = null; expected = null;
@ -99,17 +98,17 @@ pkgs.lib.runTests {
"test returns table with one named field" = { "test returns table with one named field" = {
expr = toLua [ expr = toLua [
"foo" "foo"
(mkNamedField "foo" "hello") (namedField "foo" "hello")
10 10
]; ];
expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }"; expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }";
}; };
"test returns raw string" = { "test returns raw string" = {
expr = toLua (mkLuaRaw "hello"); expr = toLua (raw "hello");
expected = "hello"; expected = "hello";
}; };
"test returns deep raw string" = { "test returns deep raw string" = {
expr = toLua (mkLuaRaw (mkLuaRaw (mkLuaRaw "hello"))); expr = toLua (raw (raw (raw "hello")));
expected = "hello"; expected = "hello";
}; };
"test returns path as string" = { "test returns path as string" = {
@ -117,11 +116,21 @@ pkgs.lib.runTests {
expected = "\"/foo/bar\""; expected = "\"/foo/bar\"";
}; };
"test throws an error when you try to use named field withoun table" = { "test throws an error when you try to use named field withoun table" = {
expr = tryEval (toLua (mkNamedField "foo" "bar")); expr = tryEval (toLua (namedField "foo" "bar"));
expected = failed; expected = failed;
}; };
"test returns call function with arguments" = { "test returns call function with arguments" = {
expr = toLua (mkCall "root_pattern" [ "deno.json" "deno.jsonc" ]); expr = toLua (call "root_pattern" [ "deno.json" "deno.jsonc" ]);
expected = "root_pattern(\"deno.json\", \"deno.jsonc\")"; expected = "root_pattern(\"deno.json\", \"deno.jsonc\")";
}; };
"test returns concated lines" = {
expr = toLua (concatLines [
(call "foo" [ 1 2 ])
(call "bar" [ "baz" "biz" ])
]);
expected = ''
foo(1, 2)
bar("baz", "biz")
'';
};
} }