|
1 | 1 | # Helpers for converting values to lua |
2 | 2 | {lib}: let |
3 | | - inherit (builtins) hasAttr head throw typeOf isList isAttrs isBool isInt isString isPath isFloat toJSON; |
4 | | - inherit (lib.attrsets) mapAttrsToList filterAttrs; |
5 | | - inherit (lib.strings) concatStringsSep concatMapStringsSep stringToCharacters; |
6 | | - inherit (lib.trivial) boolToString warn; |
7 | | -in rec { |
8 | | - # Convert a null value to lua's nil |
9 | | - nullString = value: |
10 | | - if value == null |
11 | | - then "nil" |
12 | | - else "'${value}'"; |
| 3 | + isLuaInline = object: (object._type or null) == "lua-inline"; |
13 | 4 |
|
14 | | - # convert an expression to lua |
15 | | - expToLua = exp: |
16 | | - if isList exp |
17 | | - then listToLuaTable exp # if list, convert to lua table |
18 | | - else if isAttrs exp |
19 | | - then attrsetToLuaTable exp # if attrs, convert to table |
20 | | - else if isBool exp |
21 | | - then boolToString exp # if bool, convert to string |
22 | | - else if isInt exp |
23 | | - then toString exp # if int, convert to string |
24 | | - else if exp == null |
25 | | - then "nil" |
26 | | - else (toJSON exp); # otherwise jsonify the value and print as is |
| 5 | + toLuaObject = args: |
| 6 | + { |
| 7 | + int = toString args; |
| 8 | + float = toString args; |
27 | 9 |
|
28 | | - # convert list to a lua table |
29 | | - listToLuaTable = list: |
30 | | - "{ " + (concatStringsSep ", " (map expToLua list)) + " }"; |
| 10 | + # escapes \ and quotes |
| 11 | + string = builtins.toJSON args; |
| 12 | + path = builtins.toJSON args; |
31 | 13 |
|
32 | | - # convert attrset to a lua table |
33 | | - attrsetToLuaTable = attrset: |
34 | | - "{ " |
35 | | - + ( |
36 | | - concatStringsSep ", " |
37 | | - ( |
38 | | - mapAttrsToList ( |
39 | | - name: value: |
40 | | - name |
41 | | - + " = " |
42 | | - + (expToLua value) |
43 | | - ) |
44 | | - attrset |
45 | | - ) |
46 | | - ) |
47 | | - + " }"; |
48 | | - # Convert a list of lua expressions to a lua table. The difference to listToLuaTable is that the elements here are expected to be lua expressions already, whereas listToLuaTable converts from nix types to lua first |
49 | | - luaTable = items: ''{${concatStringsSep "," items}}''; |
| 14 | + bool = lib.boolToString args; |
| 15 | + null = "nil"; |
50 | 16 |
|
51 | | - isLuaInline = object: (object._type or null) == "lua-inline"; |
| 17 | + list = "{${lib.concatMapStringsSep ",\n" toLuaObject args}}"; |
52 | 18 |
|
53 | | - toLuaObject = args: |
54 | | - if isAttrs args |
55 | | - then |
56 | | - if isLuaInline args |
57 | | - then args.expr |
58 | | - else if hasAttr "__empty" args |
59 | | - then |
60 | | - warn '' |
61 | | - Using `__empty` to define an empty lua table is deprecated. Use an empty attrset instead. |
62 | | - '' "{ }" |
63 | | - else |
64 | | - "{" |
65 | | - + (concatStringsSep "," |
66 | | - (mapAttrsToList |
67 | | - (n: v: |
68 | | - if head (stringToCharacters n) == "@" |
69 | | - then toLuaObject v |
70 | | - else "[${toLuaObject n}] = " + (toLuaObject v)) |
71 | | - (filterAttrs |
72 | | - (_: v: v != null) |
73 | | - args))) |
74 | | - + "}" |
75 | | - else if isList args |
76 | | - then "{" + concatMapStringsSep "," toLuaObject args + "}" |
77 | | - else if isString args |
78 | | - then |
79 | | - # This should be enough! |
80 | | - toJSON args |
81 | | - else if isPath args |
82 | | - then toJSON (toString args) |
83 | | - else if isBool args |
84 | | - then "${boolToString args}" |
85 | | - else if isFloat args |
86 | | - then "${toString args}" |
87 | | - else if isInt args |
88 | | - then "${toString args}" |
89 | | - else if (args == null) |
90 | | - then "nil" |
91 | | - else throw "could not convert object of type `${typeOf args}` to lua object"; |
92 | | -} |
| 19 | + set = |
| 20 | + if lib.isDerivation args |
| 21 | + then ''"${args}"'' |
| 22 | + else if isLuaInline args |
| 23 | + then args.expr |
| 24 | + else "{${ |
| 25 | + lib.pipe args [ |
| 26 | + (lib.filterAttrs (_: v: v != null)) |
| 27 | + (builtins.mapAttrs ( |
| 28 | + n: v: |
| 29 | + if lib.hasPrefix "@" n |
| 30 | + then toLuaObject v |
| 31 | + else "[${toLuaObject n}] = ${toLuaObject v}" |
| 32 | + )) |
| 33 | + builtins.attrValues |
| 34 | + (lib.concatStringsSep ",\n") |
| 35 | + ] |
| 36 | + }}"; |
| 37 | + } |
| 38 | + .${ |
| 39 | + builtins.typeOf args |
| 40 | + } |
| 41 | + or (builtins.throw "Could not convert object of type `${builtins.typeOf args}` to lua object"); |
| 42 | +in |
| 43 | + { |
| 44 | + inherit isLuaInline toLuaObject; |
| 45 | + luaTable = x: (toLuaObject (map lib.mkLuaInline x)); |
| 46 | + } |
| 47 | + // lib.genAttrs [ |
| 48 | + "nullString" |
| 49 | + "expToLua" |
| 50 | + "listToLuaTable" |
| 51 | + "attrsetToLuaTable" |
| 52 | + ] (name: lib.warn "${name} is deprecated use toLuaObject instead" toLuaObject) |
0 commit comments