rename dict and array to table
This commit is contained in:
parent
9666618527
commit
e4bed59cb9
2 changed files with 40 additions and 22 deletions
35
lib.nix
35
lib.nix
|
@ -8,32 +8,37 @@ let
|
|||
mkLuaNil = { _type = "nil"; };
|
||||
isLuaNil = val: getType val == "nil";
|
||||
|
||||
mkDictItem = name: value: {
|
||||
_type = "dict_item";
|
||||
mkNamedField = name: value: {
|
||||
_type = "table_field";
|
||||
name = validString name;
|
||||
value = toLua value;
|
||||
};
|
||||
isDictItem = val: getType val == "dict_item";
|
||||
toLuaDictItem = name: value:
|
||||
isNamedField = val: getType val == "table_field";
|
||||
toLuaNamedField = name: value:
|
||||
if isNull value then null
|
||||
else "[${toLuaStr name}] = ${value}";
|
||||
|
||||
toLua = val:
|
||||
toLua = val: toLuaInternal 0 val;
|
||||
|
||||
toLuaInternal = depth: val:
|
||||
let nextDepth = depth + 1; in
|
||||
if isLuaNil val then "nil"
|
||||
else if isLuaRaw val then val.raw
|
||||
else if isDictItem val then toLuaDictItem val.name val.value
|
||||
else if isAttrs val then toLuaDict val
|
||||
else if isList val then toLuaList val
|
||||
else if isNamedField val then
|
||||
if depth > 0 then toLuaNamedField val.name val.value
|
||||
else error "You cannot render table field at the top level"
|
||||
else if isAttrs val then toLuaTable nextDepth val
|
||||
else if isList val then toLuaList nextDepth val
|
||||
else if isString val then toLuaStr val
|
||||
else if isFloat val || isInt val then toString val
|
||||
else if isBool val then toLuaBool val
|
||||
else if isNull val then null
|
||||
else throw "[nix2lua] Value '${toString val}' is not supported";
|
||||
else error "Value '${toString val}' is not supported";
|
||||
|
||||
toLuaList = val:
|
||||
wrapObj (excludeNull (map toLua val));
|
||||
toLuaList = depth: val:
|
||||
wrapObj (excludeNull (map (toLuaInternal depth) val));
|
||||
|
||||
toLuaDict = val: toLua (attrValues (mapAttrs mkDictItem val));
|
||||
toLuaTable = depth: val: toLuaInternal depth (attrValues (mapAttrs mkNamedField val));
|
||||
|
||||
excludeNull = val: filter (v: !(isNull v)) val;
|
||||
|
||||
|
@ -47,9 +52,11 @@ let
|
|||
|
||||
validString = value:
|
||||
if isString value then value
|
||||
else throw "[nix2lua] Value '${toString value}' is not a valid string";
|
||||
else error "Value '${toString value}' is not a valid string";
|
||||
|
||||
error = message: throw "[nix2lua] ${message}";
|
||||
in
|
||||
{
|
||||
inherit toLua;
|
||||
inherit mkLuaNil mkDictItem mkLuaRaw;
|
||||
inherit mkLuaNil mkLuaRaw mkNamedField;
|
||||
}
|
||||
|
|
27
lib.test.nix
27
lib.test.nix
|
@ -2,7 +2,10 @@
|
|||
|
||||
let
|
||||
nix2lua = import ./lib.nix;
|
||||
inherit (nix2lua) toLua mkLuaNil mkDictItem mkLuaRaw;
|
||||
inherit (nix2lua) toLua mkLuaNil mkLuaRaw mkNamedField;
|
||||
inherit (builtins) tryEval;
|
||||
|
||||
failed = { success = false; value = false; };
|
||||
in
|
||||
pkgs.lib.runTests {
|
||||
"test returns null" = {
|
||||
|
@ -37,15 +40,15 @@ pkgs.lib.runTests {
|
|||
expr = toLua false;
|
||||
expected = "false";
|
||||
};
|
||||
"test returns array with all primitive types" = {
|
||||
"test returns table with all primitive types" = {
|
||||
expr = toLua [ "hello" 10 10.1 true ];
|
||||
expected = "{ \"hello\", 10, 10.100000, true }";
|
||||
};
|
||||
"test returns array without null values" = {
|
||||
"test returns table without null values" = {
|
||||
expr = toLua [ null "hello" null 10 null 10.1 null true null ];
|
||||
expected = "{ \"hello\", 10, 10.100000, true }";
|
||||
};
|
||||
"test returns dict" = {
|
||||
"test returns named table" = {
|
||||
expr = toLua {
|
||||
foo = "hello";
|
||||
int = 10;
|
||||
|
@ -55,11 +58,11 @@ pkgs.lib.runTests {
|
|||
};
|
||||
expected = "{ [\"fail\"] = false, [\"float\"] = 10.100000, [\"foo\"] = \"hello\", [\"int\"] = 10, [\"success\"] = true }";
|
||||
};
|
||||
"test returns dict without nullable items" = {
|
||||
"test returns named table without nullable items" = {
|
||||
expr = toLua { foo = "hello"; bar = null; };
|
||||
expected = "{ [\"foo\"] = \"hello\" }";
|
||||
};
|
||||
"test returns recursive dict" = {
|
||||
"test returns recursive named table" = {
|
||||
expr = toLua {
|
||||
first = {
|
||||
second = {
|
||||
|
@ -69,10 +72,14 @@ pkgs.lib.runTests {
|
|||
};
|
||||
expected = "{ [\"first\"] = { [\"second\"] = { [\"last\"] = \"hello\" } } }";
|
||||
};
|
||||
"test returns array with dict items" = {
|
||||
"test return recursive table" = {
|
||||
expr = toLua [ [ [ "foo" ] "bar" ] ];
|
||||
expected = "{ { { \"foo\" }, \"bar\" } }";
|
||||
};
|
||||
"test returns table with one named field" = {
|
||||
expr = toLua [
|
||||
"foo"
|
||||
(mkDictItem "foo" "hello")
|
||||
(mkNamedField "foo" "hello")
|
||||
10
|
||||
];
|
||||
expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }";
|
||||
|
@ -81,4 +88,8 @@ pkgs.lib.runTests {
|
|||
expr = toLua (mkLuaRaw "hello");
|
||||
expected = "hello";
|
||||
};
|
||||
"test throws an error when you try to use named field withoun table" = {
|
||||
expr = tryEval (toLua (mkNamedField "foo" "bar"));
|
||||
expected = failed;
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue