add mkLuaRaw method

This commit is contained in:
Dmitriy Pleshevskiy 2022-11-19 00:25:30 +03:00
parent 278a02cc87
commit 9666618527
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
2 changed files with 10 additions and 2 deletions

View file

@ -2,6 +2,9 @@ let
inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull; inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull;
inherit (builtins) concatStringsSep filter mapAttrs attrValues; inherit (builtins) concatStringsSep filter mapAttrs attrValues;
mkLuaRaw = raw: { _type = "raw"; inherit raw; };
isLuaRaw = val: getType val == "raw";
mkLuaNil = { _type = "nil"; }; mkLuaNil = { _type = "nil"; };
isLuaNil = val: getType val == "nil"; isLuaNil = val: getType val == "nil";
@ -17,6 +20,7 @@ let
toLua = val: toLua = val:
if isLuaNil val then "nil" if isLuaNil val then "nil"
else if isLuaRaw val then val.raw
else if isDictItem val then toLuaDictItem val.name val.value else if isDictItem val then toLuaDictItem val.name val.value
else if isAttrs val then toLuaDict val else if isAttrs val then toLuaDict val
else if isList val then toLuaList val else if isList val then toLuaList val
@ -47,5 +51,5 @@ let
in in
{ {
inherit toLua; inherit toLua;
inherit mkLuaNil mkDictItem; inherit mkLuaNil mkDictItem mkLuaRaw;
} }

View file

@ -2,7 +2,7 @@
let let
nix2lua = import ./lib.nix; nix2lua = import ./lib.nix;
inherit (nix2lua) toLua mkLuaNil mkDictItem; inherit (nix2lua) toLua mkLuaNil mkDictItem mkLuaRaw;
in in
pkgs.lib.runTests { pkgs.lib.runTests {
"test returns null" = { "test returns null" = {
@ -77,4 +77,8 @@ pkgs.lib.runTests {
]; ];
expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }"; expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }";
}; };
"test returns raw string" = {
expr = toLua (mkLuaRaw "hello");
expected = "hello";
};
} }