lib.generators.toLua: asBindings option
Allows to generate code block for setting of global variables
This commit is contained in:
@@ -434,6 +434,7 @@ ${expr "" v}
|
|||||||
Configuration:
|
Configuration:
|
||||||
* multiline - by default is true which results in indented block-like view.
|
* multiline - by default is true which results in indented block-like view.
|
||||||
* indent - initial indent.
|
* indent - initial indent.
|
||||||
|
* asBindings - by default generate single value, but with this use attrset to set global vars.
|
||||||
|
|
||||||
Attention:
|
Attention:
|
||||||
Regardless of multiline parameter there is no trailing newline.
|
Regardless of multiline parameter there is no trailing newline.
|
||||||
@@ -464,18 +465,35 @@ ${expr "" v}
|
|||||||
/* If this option is true, the output is indented with newlines for attribute sets and lists */
|
/* If this option is true, the output is indented with newlines for attribute sets and lists */
|
||||||
multiline ? true,
|
multiline ? true,
|
||||||
/* Initial indentation level */
|
/* Initial indentation level */
|
||||||
indent ? ""
|
indent ? "",
|
||||||
|
/* Interpret as variable bindings */
|
||||||
|
asBindings ? false,
|
||||||
}@args: v:
|
}@args: v:
|
||||||
with builtins;
|
with builtins;
|
||||||
let
|
let
|
||||||
innerIndent = "${indent} ";
|
innerIndent = "${indent} ";
|
||||||
introSpace = if multiline then "\n${innerIndent}" else " ";
|
introSpace = if multiline then "\n${innerIndent}" else " ";
|
||||||
outroSpace = if multiline then "\n${indent}" else " ";
|
outroSpace = if multiline then "\n${indent}" else " ";
|
||||||
innerArgs = args // { indent = innerIndent; };
|
innerArgs = args // {
|
||||||
|
indent = if asBindings then indent else innerIndent;
|
||||||
|
asBindings = false;
|
||||||
|
};
|
||||||
concatItems = concatStringsSep ",${introSpace}";
|
concatItems = concatStringsSep ",${introSpace}";
|
||||||
isLuaInline = { _type ? null, ... }: _type == "lua-inline";
|
isLuaInline = { _type ? null, ... }: _type == "lua-inline";
|
||||||
|
|
||||||
|
generatedBindings =
|
||||||
|
assert lib.assertMsg (badVarNames == []) "Bad Lua var names: ${toPretty {} badVarNames}";
|
||||||
|
libStr.concatStrings (
|
||||||
|
lib.attrsets.mapAttrsToList (key: value: "${indent}${key} = ${toLua innerArgs value}\n") v
|
||||||
|
);
|
||||||
|
|
||||||
|
# https://en.wikibooks.org/wiki/Lua_Programming/variable#Variable_names
|
||||||
|
matchVarName = match "[[:alpha:]_][[:alnum:]_]*(\\.[[:alpha:]_][[:alnum:]_]*)*";
|
||||||
|
badVarNames = filter (name: matchVarName name == null) (attrNames v);
|
||||||
in
|
in
|
||||||
if v == null then
|
if asBindings then
|
||||||
|
generatedBindings
|
||||||
|
else if v == null then
|
||||||
"nil"
|
"nil"
|
||||||
else if isInt v || isFloat v || isString v || isBool v then
|
else if isInt v || isFloat v || isString v || isBool v then
|
||||||
builtins.toJSON v
|
builtins.toJSON v
|
||||||
|
|||||||
@@ -4,6 +4,11 @@
|
|||||||
with import ../default.nix;
|
with import ../default.nix;
|
||||||
|
|
||||||
let
|
let
|
||||||
|
testingThrow = expr: {
|
||||||
|
expr = (builtins.tryEval (builtins.seq expr "didn't throw"));
|
||||||
|
expected = { success = false; value = false; };
|
||||||
|
};
|
||||||
|
testingDeepThrow = expr: testingThrow (builtins.deepSeq expr expr);
|
||||||
|
|
||||||
testSanitizeDerivationName = { name, expected }:
|
testSanitizeDerivationName = { name, expected }:
|
||||||
let
|
let
|
||||||
@@ -962,6 +967,41 @@ runTests {
|
|||||||
expected = ''{ 41, 43 }'';
|
expected = ''{ 41, 43 }'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testToLuaEmptyBindings = {
|
||||||
|
expr = generators.toLua { asBindings = true; } {};
|
||||||
|
expected = "";
|
||||||
|
};
|
||||||
|
|
||||||
|
testToLuaBindings = {
|
||||||
|
expr = generators.toLua { asBindings = true; } { x1 = 41; _y = { a = 43; }; };
|
||||||
|
expected = ''
|
||||||
|
_y = {
|
||||||
|
["a"] = 43
|
||||||
|
}
|
||||||
|
x1 = 41
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
testToLuaPartialTableBindings = {
|
||||||
|
expr = generators.toLua { asBindings = true; } { "x.y" = 42; };
|
||||||
|
expected = ''
|
||||||
|
x.y = 42
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
testToLuaIndentedBindings = {
|
||||||
|
expr = generators.toLua { asBindings = true; indent = " "; } { x = { y = 42; }; };
|
||||||
|
expected = " x = {\n [\"y\"] = 42\n }\n";
|
||||||
|
};
|
||||||
|
|
||||||
|
testToLuaBindingsWithSpace = testingThrow (
|
||||||
|
generators.toLua { asBindings = true; } { "with space" = 42; }
|
||||||
|
);
|
||||||
|
|
||||||
|
testToLuaBindingsWithLeadingDigit = testingThrow (
|
||||||
|
generators.toLua { asBindings = true; } { "11eleven" = 42; }
|
||||||
|
);
|
||||||
|
|
||||||
testToLuaBasicExample = {
|
testToLuaBasicExample = {
|
||||||
expr = generators.toLua {} {
|
expr = generators.toLua {} {
|
||||||
cmd = [ "typescript-language-server" "--stdio" ];
|
cmd = [ "typescript-language-server" "--stdio" ];
|
||||||
|
|||||||
Reference in New Issue
Block a user