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
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}";
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;
@ -40,66 +41,71 @@ let
if isList expr then { _type = "_join"; sep = validString sep; parts = expr; }
else error "Value '${toString expr}' is not supported for a join type";
isLuaRaw = expr: getType expr == "raw";
mkLuaRaw = expr:
if isLuaRaw expr then
concat = join "";
concatLines = lines:
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; }
else if isString expr then
{ _type = "raw"; raw = expr; }
else
error "Value '${toString expr}' is not supported for a raw type";
mkCall = fnName: args:
call = fnName: args:
let
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";
in
join "" (
[ luaFn (mkLuaRaw "(") ]
concat (
[ luaFn (raw "(") ]
++ [ (join ", " args) ]
++ [ (mkLuaRaw ")") ]
++ [ (raw ")") ]
);
toLuaStr = expr: "\"${validString expr}\"";
toLuaBool = expr: if expr then "true" else "false";
isLuaNil = expr: getType expr == "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:
let
wrapObj = expr: "{ ${concatStringsSep ", " expr} }";
excludeNull = expr: filter (v: !(isNull v)) expr;
in
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:
if isNull expr then null
else "[${toLuaStr name}] = ${expr}";
else "[${toLuaString name}] = ${expr}";
toLuaTable = onValue: expr: onValue (attrValues (mapAttrs namedField expr));
toLuaInternal = depth: expr:
let nextDepth = depth + 1; in
if isJoin expr then concatStringsSep expr.sep (map (toLuaInternal depth) expr.parts)
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
if depth > 0 then toLuaNamedField expr.name expr.value
else error "You cannot render table field at the top level"
else if isAttrs expr then toLuaTable (toLuaInternal nextDepth) expr
else if isList expr then toLuaList (toLuaInternal nextDepth) expr
else if isString expr || isPath expr then toLuaStr expr
else if isFloat expr || isInt expr then toString expr
else if isString expr || isPath expr then toLuaString expr
else if isFloat expr || isInt expr then toLuaNumber expr
else if isBool expr then toLuaBool expr
else if isNull expr then null
else error "Value '${toString expr}' is not supported yet";
@ -108,11 +114,15 @@ let
in
{
# low-level
inherit join;
# Deprecated
mkLuaNil = deprecated "mkLuaNil" "Nil" LuaNil;
mkLuaRaw = deprecated "mkLuaRaw" "raw" raw;
mkCall = deprecated "mkCall" "call" call;
mkNamedField = deprecated "mkNamedField" "namedField" namedField;
inherit toLua;
inherit LuaNil mkLuaRaw mkNamedField mkCall;
# DEPRECATED
inherit mkLuaNil;
inherit LuaNil;
inherit raw join concat concatLines;
inherit call namedField;
}

View file

@ -18,12 +18,11 @@
let
nix2lua = import ./lib.nix;
inherit (nix2lua) toLua LuaNil mkLuaRaw mkNamedField mkCall;
inherit (builtins) tryEval;
failed = { success = false; value = false; };
in
pkgs.lib.runTests {
with nix2lua; pkgs.lib.runTests {
"test returns null" = {
expr = toLua null;
expected = null;
@ -99,17 +98,17 @@ pkgs.lib.runTests {
"test returns table with one named field" = {
expr = toLua [
"foo"
(mkNamedField "foo" "hello")
(namedField "foo" "hello")
10
];
expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }";
};
"test returns raw string" = {
expr = toLua (mkLuaRaw "hello");
expr = toLua (raw "hello");
expected = "hello";
};
"test returns deep raw string" = {
expr = toLua (mkLuaRaw (mkLuaRaw (mkLuaRaw "hello")));
expr = toLua (raw (raw (raw "hello")));
expected = "hello";
};
"test returns path as string" = {
@ -117,11 +116,21 @@ pkgs.lib.runTests {
expected = "\"/foo/bar\"";
};
"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;
};
"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\")";
};
"test returns concated lines" = {
expr = toLua (concatLines [
(call "foo" [ 1 2 ])
(call "bar" [ "baz" "biz" ])
]);
expected = ''
foo(1, 2)
bar("baz", "biz")
'';
};
}