mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-22 15:03:28 +00:00
lib/types: Add oneOf, extension of either to a list of types
This commit is contained in:
parent
c6e7bc2977
commit
9a44f44d4c
@ -71,6 +71,15 @@ checkConfigError 'The option value .* in .* is not of type.*positive integer.*'
|
||||
checkConfigOutput "42" config.value ./declare-int-between-value.nix ./define-value-int-positive.nix
|
||||
checkConfigError 'The option value .* in .* is not of type.*between.*-21 and 43.*inclusive.*' config.value ./declare-int-between-value.nix ./define-value-int-negative.nix
|
||||
|
||||
# Check either types
|
||||
# types.either
|
||||
checkConfigOutput "42" config.value ./declare-either.nix ./define-value-int-positive.nix
|
||||
checkConfigOutput "\"24\"" config.value ./declare-either.nix ./define-value-string.nix
|
||||
# types.oneOf
|
||||
checkConfigOutput "42" config.value ./declare-oneOf.nix ./define-value-int-positive.nix
|
||||
checkConfigOutput "[ ]" config.value ./declare-oneOf.nix ./define-value-list.nix
|
||||
checkConfigOutput "\"24\"" config.value ./declare-oneOf.nix ./define-value-string.nix
|
||||
|
||||
# Check mkForce without submodules.
|
||||
set -- config.enable ./declare-enable.nix ./define-enable.nix
|
||||
checkConfigOutput "true" "$@"
|
||||
|
5
lib/tests/modules/declare-either.nix
Normal file
5
lib/tests/modules/declare-either.nix
Normal file
@ -0,0 +1,5 @@
|
||||
{ lib, ... }: {
|
||||
options.value = lib.mkOption {
|
||||
type = lib.types.either lib.types.int lib.types.str;
|
||||
};
|
||||
}
|
9
lib/tests/modules/declare-oneOf.nix
Normal file
9
lib/tests/modules/declare-oneOf.nix
Normal file
@ -0,0 +1,9 @@
|
||||
{ lib, ... }: {
|
||||
options.value = lib.mkOption {
|
||||
type = lib.types.oneOf [
|
||||
lib.types.int
|
||||
(lib.types.listOf lib.types.int)
|
||||
lib.types.str
|
||||
];
|
||||
};
|
||||
}
|
@ -443,6 +443,13 @@ rec {
|
||||
functor = (defaultFunctor name) // { wrapped = [ t1 t2 ]; };
|
||||
};
|
||||
|
||||
# Any of the types in the given list
|
||||
oneOf = ts:
|
||||
let
|
||||
head' = if ts == [] then throw "types.oneOf needs to get at least one type in its argument" else head ts;
|
||||
tail' = tail ts;
|
||||
in foldl' either head' tail';
|
||||
|
||||
# Either value of type `finalType` or `coercedType`, the latter is
|
||||
# converted to `finalType` using `coerceFunc`.
|
||||
coercedTo = coercedType: coerceFunc: finalType:
|
||||
|
@ -346,6 +346,18 @@
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term>
|
||||
<varname>types.oneOf</varname> [ <replaceable>t1</replaceable> <replaceable>t2</replaceable> ... ]
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
Type <replaceable>t1</replaceable> or type <replaceable>t2</replaceable> and so forth,
|
||||
e.g. <literal>with types; oneOf [ int str bool ]</literal>. Multiple definitions
|
||||
cannot be merged.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term>
|
||||
<varname>types.coercedTo</varname> <replaceable>from</replaceable> <replaceable>f</replaceable> <replaceable>to</replaceable>
|
||||
|
Loading…
Reference in New Issue
Block a user