add support of multiline string

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-20 23:43:21 +03:00
parent 8c4f3ca834
commit 3c08fa87d1
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
2 changed files with 16 additions and 2 deletions

View file

@ -16,7 +16,7 @@
*/ */
let let
inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath; inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath;
inherit (builtins) concatStringsSep filter mapAttrs attrValues concatLists; inherit (builtins) concatStringsSep filter mapAttrs attrValues concatLists match;
isNotNull = v: !(isNull v); isNotNull = v: !(isNull v);
excludeNull = expr: filter isNotNull expr; excludeNull = expr: filter isNotNull expr;
@ -175,7 +175,10 @@ let
toLuaBool = expr: if expr then "true" else "false"; toLuaBool = expr: if expr then "true" else "false";
toLuaNumber = toString; toLuaNumber = toString;
toLuaString = expr: "\"${validString expr}\""; toLuaString = expr:
let str = validString expr; in
let isLines = match ".*\n.*" str; in
if isLines != null then "[[\n${str}]]" else "\"${str}\"";
toLuaList = onValue: expr: toLuaList = onValue: expr:
let let
wrapObj = expr: "{ ${concatStringsSep ", " expr} }"; wrapObj = expr: "{ ${concatStringsSep ", " expr} }";

View file

@ -35,6 +35,17 @@ with nix2lua; pkgs.lib.runTests {
expr = toLua "hello world"; expr = toLua "hello world";
expected = ''"hello world"''; expected = ''"hello world"'';
}; };
"test returns a multiline lua string" = {
expr = toLua ''
hello
world
'';
expected = ''
[[
hello
world
]]'';
};
"test returns a path as a lua string" = { "test returns a path as a lua string" = {
expr = toLua /hello/world; expr = toLua /hello/world;
expected = ''"/hello/world"''; expected = ''"/hello/world"'';