lib.types: fix loaOf behavior for long lists
Assigning a list of 10 or more elements to an option having the type `loaOf a` produces a configuration value that is not honoring the order of the original list. This commit fixes this and a related issue arising when 10 or more lists are merged into this type of option.
This commit is contained in:
parent
0b04ed6177
commit
08e8701673
|
@ -143,6 +143,12 @@ checkConfigOutput "12" config.value ./declare-coerced-value-unsound.nix
|
||||||
checkConfigError 'The option value .* in .* is not.*8 bit signed integer.* or string convertible to it' config.value ./declare-coerced-value-unsound.nix ./define-value-string-bigint.nix
|
checkConfigError 'The option value .* in .* is not.*8 bit signed integer.* or string convertible to it' config.value ./declare-coerced-value-unsound.nix ./define-value-string-bigint.nix
|
||||||
checkConfigError 'unrecognised JSON value' config.value ./declare-coerced-value-unsound.nix ./define-value-string-arbitrary.nix
|
checkConfigError 'unrecognised JSON value' config.value ./declare-coerced-value-unsound.nix ./define-value-string-arbitrary.nix
|
||||||
|
|
||||||
|
# Check loaOf with long list.
|
||||||
|
checkConfigOutput "1 2 3 4 5 6 7 8 9 10" config.result ./loaOf-with-long-list.nix
|
||||||
|
|
||||||
|
# Check loaOf with many merges of lists.
|
||||||
|
checkConfigOutput "1 2 3 4 5 6 7 8 9 10" config.result ./loaOf-with-many-list-merges.nix
|
||||||
|
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
====== module tests ======
|
====== module tests ======
|
||||||
$pass Pass
|
$pass Pass
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
{ config, lib, ... }:
|
||||||
|
|
||||||
|
{
|
||||||
|
options = {
|
||||||
|
loaOfInt = lib.mkOption {
|
||||||
|
type = lib.types.loaOf lib.types.int;
|
||||||
|
};
|
||||||
|
|
||||||
|
result = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
config = {
|
||||||
|
loaOfInt = [ 1 2 3 4 5 6 7 8 9 10 ];
|
||||||
|
|
||||||
|
result = toString (lib.attrValues config.loaOfInt);
|
||||||
|
};
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
{ config, lib, ... }:
|
||||||
|
|
||||||
|
{
|
||||||
|
options = {
|
||||||
|
loaOfInt = lib.mkOption {
|
||||||
|
type = lib.types.loaOf lib.types.int;
|
||||||
|
};
|
||||||
|
|
||||||
|
result = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
config = {
|
||||||
|
loaOfInt = lib.mkMerge (map lib.singleton [ 1 2 3 4 5 6 7 8 9 10 ]);
|
||||||
|
|
||||||
|
result = toString (lib.attrValues config.loaOfInt);
|
||||||
|
};
|
||||||
|
}
|
|
@ -280,15 +280,26 @@ rec {
|
||||||
# List or attribute set of ...
|
# List or attribute set of ...
|
||||||
loaOf = elemType:
|
loaOf = elemType:
|
||||||
let
|
let
|
||||||
convertIfList = defIdx: def:
|
convertAllLists = defs:
|
||||||
|
let
|
||||||
|
padWidth = stringLength (toString (length defs));
|
||||||
|
unnamedPrefix = i: "unnamed-" + fixedWidthNumber padWidth i + ".";
|
||||||
|
in
|
||||||
|
imap1 (i: convertIfList (unnamedPrefix i)) defs;
|
||||||
|
|
||||||
|
convertIfList = unnamedPrefix: def:
|
||||||
if isList def.value then
|
if isList def.value then
|
||||||
{ inherit (def) file;
|
let
|
||||||
value = listToAttrs (
|
padWidth = stringLength (toString (length def.value));
|
||||||
imap1 (elemIdx: elem:
|
unnamed = i: unnamedPrefix + fixedWidthNumber padWidth i;
|
||||||
{ name = elem.name or "unnamed-${toString defIdx}.${toString elemIdx}";
|
in
|
||||||
value = elem;
|
{ inherit (def) file;
|
||||||
}) def.value);
|
value = listToAttrs (
|
||||||
}
|
imap1 (elemIdx: elem:
|
||||||
|
{ name = elem.name or (unnamed elemIdx);
|
||||||
|
value = elem;
|
||||||
|
}) def.value);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
def;
|
def;
|
||||||
listOnly = listOf elemType;
|
listOnly = listOf elemType;
|
||||||
|
@ -297,7 +308,7 @@ rec {
|
||||||
name = "loaOf";
|
name = "loaOf";
|
||||||
description = "list or attribute set of ${elemType.description}s";
|
description = "list or attribute set of ${elemType.description}s";
|
||||||
check = x: isList x || isAttrs x;
|
check = x: isList x || isAttrs x;
|
||||||
merge = loc: defs: attrOnly.merge loc (imap1 convertIfList defs);
|
merge = loc: defs: attrOnly.merge loc (convertAllLists defs);
|
||||||
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name?>"]);
|
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name?>"]);
|
||||||
getSubModules = elemType.getSubModules;
|
getSubModules = elemType.getSubModules;
|
||||||
substSubModules = m: loaOf (elemType.substSubModules m);
|
substSubModules = m: loaOf (elemType.substSubModules m);
|
||||||
|
|
Loading…
Reference in New Issue