···1718rec {
1920- /* Generates an INI-style config file from an
000000000000000000000021 * attrset of sections to an attrset of key-value pairs.
22 *
23 * generators.toINI {} {
···41 # apply transformations (e.g. escapes) to section names
42 mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
43 # format a setting line from key and value
44- mkKeyValue ? (k: v: "${libStr.escape ["="] k}=${toString v}")
45 }: attrsOfAttrs:
46 let
47 # map function to string for each key val
48 mapAttrsToStringsSep = sep: mapFn: attrs:
49 libStr.concatStringsSep sep
50 (libAttr.mapAttrsToList mapFn attrs);
51- mkLine = k: v: mkKeyValue k v + "\n";
52 mkSection = sectName: sectValues: ''
53 [${mkSectionName sectName}]
54- '' + libStr.concatStrings (libAttr.mapAttrsToList mkLine sectValues);
55 in
56 # map input to ini sections
57 mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
···1718rec {
1920+ /* Generate a line of key k and value v, separated by
21+ * character sep. If sep appears in k, it is escaped.
22+ * Helper for synaxes with different separators.
23+ *
24+ * mkKeyValueLine ":" "f:oo" "bar"
25+ * > "f\:oo:bar"
26+ */
27+ mkKeyValueLine = sep: k: v:
28+ "${libStr.escape [sep] k}${sep}${toString v}";
29+30+31+ /* Generate a key-value-style config file from an attrset.
32+ *
33+ * mkKeyValue is the same as in toINI.
34+ */
35+ toKeyValue = {
36+ mkKeyValue ? mkKeyValueLine "="
37+ }: attrs:
38+ let mkLine = k: v: mkKeyValue k v + "\n";
39+ in libStr.concatStrings (libAttr.mapAttrsToList mkLine attrs);
40+41+42+ /* Generate an INI-style config file from an
43 * attrset of sections to an attrset of key-value pairs.
44 *
45 * generators.toINI {} {
···63 # apply transformations (e.g. escapes) to section names
64 mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
65 # format a setting line from key and value
66+ mkKeyValue ? mkKeyValueLine "="
67 }: attrsOfAttrs:
68 let
69 # map function to string for each key val
70 mapAttrsToStringsSep = sep: mapFn: attrs:
71 libStr.concatStringsSep sep
72 (libAttr.mapAttrsToList mapFn attrs);
073 mkSection = sectName: sectValues: ''
74 [${mkSectionName sectName}]
75+ '' + toKeyValue { inherit mkKeyValue; } sectValues;
76 in
77 # map input to ini sections
78 mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
+16
lib/tests.nix
···135 # these tests assume attributes are converted to lists
136 # in alphabetical order
1370000000000000000138 testToINIEmpty = {
139 expr = generators.toINI {} {};
140 expected = "";