From 8e00dcd242f33fa34c8dba3bad2854cbb6bd0659 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Thu, 9 Nov 2017 15:58:14 +0100 Subject: [PATCH] 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. --- lib/generators.nix | 14 +++++++++----- lib/tests/misc.nix | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index 5f9da234f447..b27ab485f41e 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -22,11 +22,15 @@ rec { * character sep. If sep appears in k, it is escaped. * Helper for synaxes with different separators. * - * mkKeyValueDefault ":" "f:oo" "bar" + * mkValueString specifies how values should be formatted. + * + * mkKeyValueDefault {} ":" "f:oo" "bar" * > "f\:oo:bar" */ - mkKeyValueDefault = sep: k: v: - "${libStr.escape [sep] k}${sep}${toString v}"; + mkKeyValueDefault = { + mkValueString ? toString + }: sep: k: v: + "${libStr.escape [sep] k}${sep}${mkValueString v}"; /* Generate a key-value-style config file from an attrset. @@ -34,7 +38,7 @@ rec { * mkKeyValue is the same as in toINI. */ toKeyValue = { - mkKeyValue ? mkKeyValueDefault "=" + mkKeyValue ? mkKeyValueDefault {} "=" }: attrs: let mkLine = k: v: mkKeyValue k v + "\n"; in libStr.concatStrings (libAttr.mapAttrsToList mkLine attrs); @@ -64,7 +68,7 @@ rec { # apply transformations (e.g. escapes) to section names mkSectionName ? (name: libStr.escape [ "[" "]" ] name), # format a setting line from key and value - mkKeyValue ? mkKeyValueDefault "=" + mkKeyValue ? mkKeyValueDefault {} "=" }: attrsOfAttrs: let # map function to string for each key val diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 4c4c06fe78da..1657ec33a46c 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -201,7 +201,7 @@ runTests { # in alphabetical order testMkKeyValueDefault = { - expr = generators.mkKeyValueDefault ":" "f:oo" "bar"; + expr = generators.mkKeyValueDefault {} ":" "f:oo" "bar"; expected = ''f\:oo:bar''; };