git.iko.soy/typeable/nixpkgs
on master help sign in
formats.ini: Introduce `listToValue` argument (#121613)

Allows coercing lists to values. E.g. formats.ini { listToValue = lib.concatMapStringsSep ", " (lib.generators.mkValueStringDefault {}); }

commitb8336c2b8abaabb4307ef2f1c38ec48f2f1dbf6f authorSilvan Mosberger <contact@infinisil.com> dateTue 4 May 2021, 09:49 (6 years ago) parent3cc34f9
3 files changed+51−3
nixos/doc/manual/development/settings-options.xml+11−1 pkgs/pkgs-lib/formats.nix+24−2 pkgs/pkgs-lib/tests/formats.nix+16
nixos/doc/manual/development/settings-options.xml+11−1
@@ -50,7 +50,7 @@
5050 </varlistentry>
5151 <varlistentry>
5252 <term>
53− <varname>pkgs.formats.ini</varname> { <replaceable>listsAsDuplicateKeys</replaceable> ? false, ... }
53+ <varname>pkgs.formats.ini</varname> { <replaceable>listsAsDuplicateKeys</replaceable> ? false, <replaceable>listToValue</replaceable> ? null, ... }
5454 </term>
5555 <listitem>
5656 <para>
@@ -66,6 +66,16 @@
6666 </para>
6767 </listitem>
6868 </varlistentry>
69+ <varlistentry>
70+ <term>
71+ <varname>listToValue</varname>
72+ </term>
73+ <listitem>
74+ <para>
75+ A function for turning a list of values into a single value.
76+ </para>
77+ </listitem>
78+ </varlistentry>
6979 </variablelist>
7080 It returns a set with INI-specific attributes <varname>type</varname> and <varname>generate</varname> as specified <link linkend='pkgs-formats-result'>below</link>.
7181 </para>
pkgs/pkgs-lib/formats.nix+24−2
@@ -56,7 +56,16 @@ rec {
5656 };
5757 };
5858
59− ini = { listsAsDuplicateKeys ? false, ... }@args: {
59+ ini = {
60+ # Represents lists as duplicate keys
61+ listsAsDuplicateKeys ? false,
62+ # Alternative to listsAsDuplicateKeys, converts list to non-list
63+ # listToValue :: [IniAtom] -> IniAtom
64+ listToValue ? null,
65+ ...
66+ }@args:
67+ assert !listsAsDuplicateKeys || listToValue == null;
68+ {
6069
6170 type = with lib.types; let
6271
@@ -74,12 +83,25 @@ rec {
7483 coercedTo singleIniAtom lib.singleton (listOf singleIniAtom) // {
7584 description = singleIniAtom.description + " or a list of them for duplicate keys";
7685 }
86+ else if listToValue != null then
87+ coercedTo singleIniAtom lib.singleton (nonEmptyListOf singleIniAtom) // {
88+ description = singleIniAtom.description + " or a non-empty list of them";
89+ }
7790 else
7891 singleIniAtom;
7992
8093 in attrsOf (attrsOf iniAtom);
8194
82− generate = name: value: pkgs.writeText name (lib.generators.toINI args value);
95+ generate = name: value:
96+ let
97+ transformedValue =
98+ if listToValue != null
99+ then
100+ lib.mapAttrs (section: lib.mapAttrs (key: val:
101+ if lib.isList val then listToValue val else val
102+ )) value
103+ else value;
104+ in pkgs.writeText name (lib.generators.toINI (removeAttrs args ["listToValue"]) transformedValue);
83105
84106 };
85107
pkgs/pkgs-lib/tests/formats.nix+16
@@ -124,6 +124,22 @@ in runBuildTests {
124124 '';
125125 };
126126
127+ testIniListToValue = {
128+ drv = evalFormat formats.ini { listToValue = concatMapStringsSep ", " (generators.mkValueStringDefault {}); } {
129+ foo = {
130+ bar = [ null true "test" 1.2 10 ];
131+ baz = false;
132+ qux = "qux";
133+ };
134+ };
135+ expected = ''
136+ [foo]
137+ bar=null, true, test, 1.200000, 10
138+ baz=false
139+ qux=qux
140+ '';
141+ };
142+
127143 testTomlAtoms = {
128144 drv = evalFormat formats.toml {} {
129145 false = false;