Skip to content

Commit

Permalink
generators: toKDL, convert ' to " in name
Browse files Browse the repository at this point in the history
  • Loading branch information
Constantin Gahr committed Oct 26, 2023
1 parent 6045b68 commit 6d32759
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
64 changes: 36 additions & 28 deletions modules/lib/generators.nix
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
{ lib }:

{
toKDL = { }:
toKDL = {}:
let
inherit (lib) concatStringsSep splitString mapAttrsToList any;
inherit (builtins) typeOf replaceStrings elem;

# ListOf String -> String
indentStrings = let
# Although the input of this function is a list of strings,
# the strings themselves *will* contain newlines, so you need
# to normalize the list by joining and resplitting them.
unlines = lib.splitString "\n";
lines = lib.concatStringsSep "\n";
indentAll = lines: concatStringsSep "\n" (map (x: " " + x) lines);
in stringsWithNewlines: indentAll (unlines (lines stringsWithNewlines));
indentStrings =
let
# Although the input of this function is a list of strings,
# the strings themselves *will* contain newlines, so you need
# to normalize the list by joining and resplitting them.
unlines = lib.splitString "\n";
lines = lib.concatStringsSep "\n";
indentAll = lines: concatStringsSep "\n" (map (x: " " + x) lines);
in
stringsWithNewlines: indentAll (unlines (lines stringsWithNewlines));

# String -> String
sanitizeString = replaceStrings [ "\n" ''"'' ] [ "\\n" ''\"'' ];
replaceSingleQuotes = replaceStrings [ "'" ] [ ''"'' ];

# OneOf [Int Float String Bool Null] -> String
literalValueToString = element:
lib.throwIfNot
(elem (typeOf element) [ "int" "float" "string" "bool" "null" ])
"Cannot convert value of type ${typeOf element} to KDL literal."
(if typeOf element == "null" then
"null"
else if element == false then
"false"
else if element == true then
"true"
else if typeOf element == "string" then
''"${sanitizeString element}"''
else
toString element);
(elem (typeOf element) [ "int" "float" "string" "bool" "null" ])
"Cannot convert value of type ${typeOf element} to KDL literal."
(if typeOf element == "null" then
"null"
else if element == false then
"false"
else if element == true then
"true"
else if typeOf element == "string" then
''"${sanitizeString element}"''
else
toString element);

# Attrset Conversion
# String -> AttrsOf Anything -> String
Expand All @@ -56,7 +59,8 @@

children =
lib.filterAttrs (name: _: !(elem name [ "_args" "_props" ])) attrs;
in ''
in
''
${name} ${optArgsString}${optPropsString}{
${indentStrings (mapAttrsToList convertAttributeToKDL children)}
}'';
Expand Down Expand Up @@ -84,19 +88,23 @@
# Combined Conversion
# String -> Anything -> String
convertAttributeToKDL = name: value:
let vType = typeOf value;
in if elem vType [ "int" "float" "bool" "null" "string" ] then
"${name} ${literalValueToString value}"
let
vType = typeOf value;
normName = replaceSingleQuotes name;
in
if elem vType [ "int" "float" "bool" "null" "string" ] then
"${normName} ${literalValueToString value}"
else if vType == "set" then
convertAttrsToKDL name value
convertAttrsToKDL normName value
else if vType == "list" then
convertListToKDL name value
convertListToKDL normName value
else
throw ''
Cannot convert type `(${typeOf value})` to KDL:
${name} = ${toString value}
'';
in attrs: ''
in
attrs: ''
${concatStringsSep "\n" (mapAttrsToList convertAttributeToKDL attrs)}
'';
}
2 changes: 2 additions & 0 deletions tests/lib/generators/tokdl-result.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ nested {
-
- null
}
singleQuote "test" true
singleQuote "test" 2 "don't change them here"
unsafeString " \" \n "
2 changes: 2 additions & 0 deletions tests/lib/generators/tokdl.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
\t \n \" "
'';
unsafeString = " \" \n ";
"singleQuote 'test'" = true;
"singleQuote 'test' 2" = "don't change them here";
flatItems = [ 1 2 "asdf" true null ];
bigFlatItems = [
23847590283751
Expand Down

0 comments on commit 6d32759

Please sign in to comment.