summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authordanbst <abcz2.uprola@gmail.com>2019-08-18 18:00:25 +0300
committerdanbst <abcz2.uprola@gmail.com>2019-08-18 18:00:25 +0300
commitd80cd26ff981f1c4be96b82757811b21f280f1a6 (patch)
tree28375ed14a49fe86c03ef21868833faa23b56215 /lib
parent29ba0a0adff274f5ea0d93246be3f2957d234a7b (diff)
parent2ca09a94be5182c8f8cc688eaded558d827312f1 (diff)
downloadnixpkgs-d80cd26ff981f1c4be96b82757811b21f280f1a6.tar
nixpkgs-d80cd26ff981f1c4be96b82757811b21f280f1a6.tar.gz
nixpkgs-d80cd26ff981f1c4be96b82757811b21f280f1a6.tar.bz2
nixpkgs-d80cd26ff981f1c4be96b82757811b21f280f1a6.tar.lz
nixpkgs-d80cd26ff981f1c4be96b82757811b21f280f1a6.tar.xz
nixpkgs-d80cd26ff981f1c4be96b82757811b21f280f1a6.tar.zst
nixpkgs-d80cd26ff981f1c4be96b82757811b21f280f1a6.zip
Merge branch 'master' into flip-map-foreach
Diffstat (limited to 'lib')
-rwxr-xr-xlib/tests/modules.sh9
-rw-r--r--lib/tests/modules/declare-either.nix5
-rw-r--r--lib/tests/modules/declare-oneOf.nix9
-rw-r--r--lib/types.nix7
4 files changed, 30 insertions, 0 deletions
diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh
index eadaa0521b3..cf344122cf4 100755
--- a/lib/tests/modules.sh
+++ b/lib/tests/modules.sh
@@ -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" "$@"
diff --git a/lib/tests/modules/declare-either.nix b/lib/tests/modules/declare-either.nix
new file mode 100644
index 00000000000..5a0fa978a13
--- /dev/null
+++ b/lib/tests/modules/declare-either.nix
@@ -0,0 +1,5 @@
+{ lib, ... }: {
+  options.value = lib.mkOption {
+    type = lib.types.either lib.types.int lib.types.str;
+  };
+}
diff --git a/lib/tests/modules/declare-oneOf.nix b/lib/tests/modules/declare-oneOf.nix
new file mode 100644
index 00000000000..df092a14f81
--- /dev/null
+++ b/lib/tests/modules/declare-oneOf.nix
@@ -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
+    ];
+  };
+}
diff --git a/lib/types.nix b/lib/types.nix
index e22bcd326c8..9c00656ab91 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -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: