2023-04-17 23:01:45 +00:00
# Helpers for converting values to lua
2023-11-06 09:33:38 +00:00
{ lib }: let
2024-03-24 00:14:39 +00:00
inherit ( builtins ) hasAttr head throw typeOf isList isAttrs isBool isInt isString isPath isFloat toJSON ;
inherit ( lib . attrsets ) mapAttrsToList filterAttrs ;
2024-07-08 21:57:58 +00:00
inherit ( lib . strings ) concatStringsSep concatMapStringsSep stringToCharacters concatLines ;
2024-05-21 11:08:17 +00:00
inherit ( lib . trivial ) boolToString warn ;
2023-11-06 09:33:38 +00:00
in rec {
2024-04-17 00:00:27 +00:00
wrapLuaConfig = {
luaBefore ? " " ,
luaConfig ,
luaAfter ? " " ,
} : ''
lua < < EOF
2024-07-08 21:57:58 +00:00
$ { concatLines [ luaBefore luaConfig luaAfter ] }
2024-04-17 00:00:27 +00:00
EOF
'' ;
2023-06-04 11:12:08 +00:00
# Convert a null value to lua's nil
2023-04-17 23:01:45 +00:00
nullString = value :
if value == null
then " n i l "
else " ' ${ value } ' " ;
2023-06-04 11:12:08 +00:00
2023-06-05 18:37:12 +00:00
# convert an expression to lua
2023-06-04 14:36:01 +00:00
expToLua = exp :
2024-03-24 00:14:39 +00:00
if isList exp
2023-07-30 08:41:52 +00:00
then listToLuaTable exp # if list, convert to lua table
2024-03-24 00:14:39 +00:00
else if isAttrs exp
2023-07-30 08:41:52 +00:00
then attrsetToLuaTable exp # if attrs, convert to table
2024-03-24 00:14:39 +00:00
else if isBool exp
then boolToString exp # if bool, convert to string
else if isInt exp
then toString exp # if int, convert to string
2024-02-17 15:44:05 +00:00
else if exp == null
then " n i l "
2024-03-24 00:14:39 +00:00
else ( toJSON exp ) ; # otherwise jsonify the value and print as is
2023-06-04 14:36:01 +00:00
2023-06-05 18:37:12 +00:00
# convert list to a lua table
2023-06-04 14:36:01 +00:00
listToLuaTable = list :
2024-03-24 00:14:39 +00:00
" { " + ( concatStringsSep " , " ( map expToLua list ) ) + " } " ;
2023-06-04 11:12:08 +00:00
2023-06-05 18:37:12 +00:00
# convert attrset to a lua table
2023-06-04 11:12:08 +00:00
attrsetToLuaTable = attrset :
" { "
+ (
2024-03-24 00:14:39 +00:00
concatStringsSep " , "
2023-06-04 14:36:01 +00:00
(
2024-03-24 00:14:39 +00:00
mapAttrsToList (
2023-06-04 11:12:08 +00:00
name : value :
2023-06-04 14:36:01 +00:00
name
+ " = "
+ ( expToLua value )
2023-06-04 11:12:08 +00:00
)
attrset
2023-06-04 14:36:01 +00:00
)
2023-06-04 11:12:08 +00:00
)
+ " } " ;
2023-10-24 07:18:44 +00:00
# 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
2024-03-24 00:14:39 +00:00
luaTable = items : '' { ${ concatStringsSep " , " items } } '' ;
2023-11-06 09:33:38 +00:00
2024-04-27 14:01:37 +00:00
isLuaInline = object : ( object . _type or null ) == " l u a - i n l i n e " ;
2024-03-16 09:29:07 +00:00
2023-11-06 09:33:38 +00:00
toLuaObject = args :
2024-03-24 00:14:39 +00:00
if isAttrs args
2023-11-06 09:33:38 +00:00
then
2024-03-16 09:29:07 +00:00
if isLuaInline args
then args . expr
2024-05-21 10:51:56 +00:00
else if hasAttr " _ _ e m p t y " args
then
2024-05-21 11:08:17 +00:00
warn ''
2024-05-21 10:51:56 +00:00
Using ` __empty ` to define an empty lua table is deprecated . Use an empty attrset instead .
'' " { } "
2023-11-06 09:33:38 +00:00
else
" { "
+ ( concatStringsSep " , "
( mapAttrsToList
( n : v :
if head ( stringToCharacters n ) == " @ "
then toLuaObject v
else " [ ${ toLuaObject n } ] = " + ( toLuaObject v ) )
( filterAttrs
2024-05-12 01:17:04 +00:00
( _ : v : v != null )
2023-11-06 09:33:38 +00:00
args ) ) )
+ " } "
2024-03-24 00:14:39 +00:00
else if isList args
2023-11-06 09:33:38 +00:00
then " { " + concatMapStringsSep " , " toLuaObject args + " } "
2024-03-24 00:14:39 +00:00
else if isString args
2023-11-06 09:33:38 +00:00
then
# This should be enough!
2024-03-24 00:14:39 +00:00
toJSON args
else if isPath args
then toJSON ( toString args )
else if isBool args
2023-11-06 09:33:38 +00:00
then " ${ boolToString args } "
2024-03-24 00:14:39 +00:00
else if isFloat args
2023-11-06 09:33:38 +00:00
then " ${ toString args } "
2024-03-24 00:14:39 +00:00
else if isInt args
2023-11-06 09:33:38 +00:00
then " ${ toString args } "
2024-02-17 15:44:41 +00:00
else if ( args == null )
2023-11-06 09:33:38 +00:00
then " n i l "
2024-02-17 20:55:07 +00:00
else throw " c o u l d n o t c o n v e r t o b j e c t o f t y p e ` ${ typeOf args } ` t o l u a o b j e c t " ;
2023-04-17 23:01:45 +00:00
}