add support of raw key to the named field

This commit is contained in:
Dmitriy Pleshevskiy 2024-05-20 11:52:42 +03:00
parent da13688260
commit 3abddcaf77
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
2 changed files with 11 additions and 1 deletions

View file

@ -39,6 +39,8 @@ let
else if isString expr || isPath expr then toString expr
else validationError expr "is not a valid string";
validStringOrRaw = expr: if isRaw expr then expr else validString expr;
validFuncName = fnName:
if isString fnName && builtins.stringLength fnName > 0 then raw fnName
else validationError fnName "is not a valid function name";
@ -169,7 +171,7 @@ let
isNamedField = expr: getType expr == "table_field";
namedField = name: expr: {
_type = "table_field";
name = validString name;
name = validStringOrRaw name;
value = toLua expr;
};
@ -187,6 +189,7 @@ let
isValidFieldName = name: isString name && match "^[[:alpha:]_][[:alnum:]_]*$" name == [ ];
toLuaNamedField = name: expr:
if isNull expr then null
else if isRaw name then "[${name.raw}] = ${expr}"
else if isValidFieldName name then "${name} = ${expr}"
else "[${toLuaString name}] = ${expr}";
toLuaTable = onValue: expr: onValue (attrValues (mapAttrs namedField expr));

View file

@ -127,6 +127,13 @@ with nix2lua; pkgs.lib.runTests {
];
expected = ''{ "foo", 10 }'';
};
"test returns named field with a raw in a key" = {
expr = toLua [
(namedField (raw "foo") 1)
(namedField "foo" 2)
];
expected = ''{ [foo] = 1, foo = 2 }'';
};
"test returns raw string" = {
expr = toLua (raw "hello");
expected = "hello";