formats.ini: Introduce listToValue argument (#121613)

Allows coercing lists to values. E.g.

  formats.ini { listToValue = lib.concatMapStringsSep ", " (lib.generators.mkValueStringDefault {}); }
This commit is contained in:
Silvan Mosberger 2021-05-04 09:49:25 +02:00 committed by GitHub
parent 3cc34f9e55
commit b8336c2b8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 3 deletions

View File

@ -50,7 +50,7 @@
</varlistentry>
<varlistentry>
<term>
<varname>pkgs.formats.ini</varname> { <replaceable>listsAsDuplicateKeys</replaceable> ? false, ... }
<varname>pkgs.formats.ini</varname> { <replaceable>listsAsDuplicateKeys</replaceable> ? false, <replaceable>listToValue</replaceable> ? null, ... }
</term>
<listitem>
<para>
@ -66,6 +66,16 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<varname>listToValue</varname>
</term>
<listitem>
<para>
A function for turning a list of values into a single value.
</para>
</listitem>
</varlistentry>
</variablelist>
It returns a set with INI-specific attributes <varname>type</varname> and <varname>generate</varname> as specified <link linkend='pkgs-formats-result'>below</link>.
</para>

View File

@ -56,7 +56,16 @@ rec {
};
};
ini = { listsAsDuplicateKeys ? false, ... }@args: {
ini = {
# Represents lists as duplicate keys
listsAsDuplicateKeys ? false,
# Alternative to listsAsDuplicateKeys, converts list to non-list
# listToValue :: [IniAtom] -> IniAtom
listToValue ? null,
...
}@args:
assert !listsAsDuplicateKeys || listToValue == null;
{
type = with lib.types; let
@ -74,12 +83,25 @@ rec {
coercedTo singleIniAtom lib.singleton (listOf singleIniAtom) // {
description = singleIniAtom.description + " or a list of them for duplicate keys";
}
else if listToValue != null then
coercedTo singleIniAtom lib.singleton (nonEmptyListOf singleIniAtom) // {
description = singleIniAtom.description + " or a non-empty list of them";
}
else
singleIniAtom;
in attrsOf (attrsOf iniAtom);
generate = name: value: pkgs.writeText name (lib.generators.toINI args value);
generate = name: value:
let
transformedValue =
if listToValue != null
then
lib.mapAttrs (section: lib.mapAttrs (key: val:
if lib.isList val then listToValue val else val
)) value
else value;
in pkgs.writeText name (lib.generators.toINI (removeAttrs args ["listToValue"]) transformedValue);
};

View File

@ -124,6 +124,22 @@ in runBuildTests {
'';
};
testIniListToValue = {
drv = evalFormat formats.ini { listToValue = concatMapStringsSep ", " (generators.mkValueStringDefault {}); } {
foo = {
bar = [ null true "test" 1.2 10 ];
baz = false;
qux = "qux";
};
};
expected = ''
[foo]
bar=null, true, test, 1.200000, 10
baz=false
qux=qux
'';
};
testTomlAtoms = {
drv = evalFormat formats.toml {} {
false = false;