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"; };
|
mkLuaNil = { _type = "nil"; };
|
||||||
isLuaNil = val: getType val == "nil";
|
isLuaNil = val: getType val == "nil";
|
||||||
|
|
||||||
mkDictItem = name: value: {
|
mkNamedField = name: value: {
|
||||||
_type = "dict_item";
|
_type = "table_field";
|
||||||
name = validString name;
|
name = validString name;
|
||||||
value = toLua value;
|
value = toLua value;
|
||||||
};
|
};
|
||||||
isDictItem = val: getType val == "dict_item";
|
isNamedField = val: getType val == "table_field";
|
||||||
toLuaDictItem = name: value:
|
toLuaNamedField = name: value:
|
||||||
if isNull value then null
|
if isNull value then null
|
||||||
else "[${toLuaStr name}] = ${value}";
|
else "[${toLuaStr name}] = ${value}";
|
||||||
|
|
||||||
toLua = val:
|
toLua = val: toLuaInternal 0 val;
|
||||||
|
|
||||||
|
toLuaInternal = depth: val:
|
||||||
|
let nextDepth = depth + 1; in
|
||||||
if isLuaNil val then "nil"
|
if isLuaNil val then "nil"
|
||||||
else if isLuaRaw val then val.raw
|
else if isLuaRaw val then val.raw
|
||||||
else if isDictItem val then toLuaDictItem val.name val.value
|
else if isNamedField val then
|
||||||
else if isAttrs val then toLuaDict val
|
if depth > 0 then toLuaNamedField val.name val.value
|
||||||
else if isList val then toLuaList val
|
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 isString val then toLuaStr val
|
||||||
else if isFloat val || isInt val then toString val
|
else if isFloat val || isInt val then toString val
|
||||||
else if isBool val then toLuaBool val
|
else if isBool val then toLuaBool val
|
||||||
else if isNull val then null
|
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:
|
toLuaList = depth: val:
|
||||||
wrapObj (excludeNull (map toLua 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;
|
excludeNull = val: filter (v: !(isNull v)) val;
|
||||||
|
|
||||||
|
@ -47,9 +52,11 @@ let
|
||||||
|
|
||||||
validString = value:
|
validString = value:
|
||||||
if isString value then 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
|
in
|
||||||
{
|
{
|
||||||
inherit toLua;
|
inherit toLua;
|
||||||
inherit mkLuaNil mkDictItem mkLuaRaw;
|
inherit mkLuaNil mkLuaRaw mkNamedField;
|
||||||
}
|
}
|
||||||
|
|
27
lib.test.nix
27
lib.test.nix
|
@ -2,7 +2,10 @@
|
||||||
|
|
||||||
let
|
let
|
||||||
nix2lua = import ./lib.nix;
|
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
|
in
|
||||||
pkgs.lib.runTests {
|
pkgs.lib.runTests {
|
||||||
"test returns null" = {
|
"test returns null" = {
|
||||||
|
@ -37,15 +40,15 @@ pkgs.lib.runTests {
|
||||||
expr = toLua false;
|
expr = toLua false;
|
||||||
expected = "false";
|
expected = "false";
|
||||||
};
|
};
|
||||||
"test returns array with all primitive types" = {
|
"test returns table with all primitive types" = {
|
||||||
expr = toLua [ "hello" 10 10.1 true ];
|
expr = toLua [ "hello" 10 10.1 true ];
|
||||||
expected = "{ \"hello\", 10, 10.100000, 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 ];
|
expr = toLua [ null "hello" null 10 null 10.1 null true null ];
|
||||||
expected = "{ \"hello\", 10, 10.100000, true }";
|
expected = "{ \"hello\", 10, 10.100000, true }";
|
||||||
};
|
};
|
||||||
"test returns dict" = {
|
"test returns named table" = {
|
||||||
expr = toLua {
|
expr = toLua {
|
||||||
foo = "hello";
|
foo = "hello";
|
||||||
int = 10;
|
int = 10;
|
||||||
|
@ -55,11 +58,11 @@ pkgs.lib.runTests {
|
||||||
};
|
};
|
||||||
expected = "{ [\"fail\"] = false, [\"float\"] = 10.100000, [\"foo\"] = \"hello\", [\"int\"] = 10, [\"success\"] = true }";
|
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; };
|
expr = toLua { foo = "hello"; bar = null; };
|
||||||
expected = "{ [\"foo\"] = \"hello\" }";
|
expected = "{ [\"foo\"] = \"hello\" }";
|
||||||
};
|
};
|
||||||
"test returns recursive dict" = {
|
"test returns recursive named table" = {
|
||||||
expr = toLua {
|
expr = toLua {
|
||||||
first = {
|
first = {
|
||||||
second = {
|
second = {
|
||||||
|
@ -69,10 +72,14 @@ pkgs.lib.runTests {
|
||||||
};
|
};
|
||||||
expected = "{ [\"first\"] = { [\"second\"] = { [\"last\"] = \"hello\" } } }";
|
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 [
|
expr = toLua [
|
||||||
"foo"
|
"foo"
|
||||||
(mkDictItem "foo" "hello")
|
(mkNamedField "foo" "hello")
|
||||||
10
|
10
|
||||||
];
|
];
|
||||||
expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }";
|
expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }";
|
||||||
|
@ -81,4 +88,8 @@ pkgs.lib.runTests {
|
||||||
expr = toLua (mkLuaRaw "hello");
|
expr = toLua (mkLuaRaw "hello");
|
||||||
expected = "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