add support of multiline string
This commit is contained in:
parent
8c4f3ca834
commit
3c08fa87d1
2 changed files with 16 additions and 2 deletions
7
lib.nix
7
lib.nix
|
@ -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} }";
|
||||||
|
|
11
lib.test.nix
11
lib.test.nix
|
@ -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"'';
|
||||||
|
|
Loading…
Reference in a new issue