lib/generators: add mkValueString to mkKeyValueDefault generator

This means the generation of values can now be influenced, even down from e.g.
an INI generator.
Breaks the interface of `mkKeyValueDefault` to match its interface to other
generator functions. It might me sensible to rename `mkKeyValue` and
`mkKeyValueSet` to conform to the `toX`-style of generator functions.
This commit is contained in:
Profpatsch 2017-11-09 15:58:14 +01:00
parent f09f49d483
commit 8e00dcd242
2 changed files with 10 additions and 6 deletions

View file

@ -22,11 +22,15 @@ rec {
* character sep. If sep appears in k, it is escaped. * character sep. If sep appears in k, it is escaped.
* Helper for synaxes with different separators. * Helper for synaxes with different separators.
* *
* mkKeyValueDefault ":" "f:oo" "bar" * mkValueString specifies how values should be formatted.
*
* mkKeyValueDefault {} ":" "f:oo" "bar"
* > "f\:oo:bar" * > "f\:oo:bar"
*/ */
mkKeyValueDefault = sep: k: v: mkKeyValueDefault = {
"${libStr.escape [sep] k}${sep}${toString v}"; mkValueString ? toString
}: sep: k: v:
"${libStr.escape [sep] k}${sep}${mkValueString v}";
/* Generate a key-value-style config file from an attrset. /* Generate a key-value-style config file from an attrset.
@ -34,7 +38,7 @@ rec {
* mkKeyValue is the same as in toINI. * mkKeyValue is the same as in toINI.
*/ */
toKeyValue = { toKeyValue = {
mkKeyValue ? mkKeyValueDefault "=" mkKeyValue ? mkKeyValueDefault {} "="
}: attrs: }: attrs:
let mkLine = k: v: mkKeyValue k v + "\n"; let mkLine = k: v: mkKeyValue k v + "\n";
in libStr.concatStrings (libAttr.mapAttrsToList mkLine attrs); in libStr.concatStrings (libAttr.mapAttrsToList mkLine attrs);
@ -64,7 +68,7 @@ rec {
# apply transformations (e.g. escapes) to section names # apply transformations (e.g. escapes) to section names
mkSectionName ? (name: libStr.escape [ "[" "]" ] name), mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
# format a setting line from key and value # format a setting line from key and value
mkKeyValue ? mkKeyValueDefault "=" mkKeyValue ? mkKeyValueDefault {} "="
}: attrsOfAttrs: }: attrsOfAttrs:
let let
# map function to string for each key val # map function to string for each key val

View file

@ -201,7 +201,7 @@ runTests {
# in alphabetical order # in alphabetical order
testMkKeyValueDefault = { testMkKeyValueDefault = {
expr = generators.mkKeyValueDefault ":" "f:oo" "bar"; expr = generators.mkKeyValueDefault {} ":" "f:oo" "bar";
expected = ''f\:oo:bar''; expected = ''f\:oo:bar'';
}; };