lib/types: prioritise coercedType in coercedTo

This more intuitively matches `types.either` and allows paths to be
coerced to submodules again, which was inhibited by #76861
This commit is contained in:
arcnmx 2020-01-14 09:06:59 -08:00
parent f4c645a3a4
commit 92b464d57d

View File

@ -590,7 +590,7 @@ rec {
tail' = tail ts; tail' = tail ts;
in foldl' either head' tail'; in foldl' either head' tail';
# Either value of type `finalType` or `coercedType`, the latter is # Either value of type `coercedType` or `finalType`, the former is
# converted to `finalType` using `coerceFunc`. # converted to `finalType` using `coerceFunc`.
coercedTo = coercedType: coerceFunc: finalType: coercedTo = coercedType: coerceFunc: finalType:
assert lib.assertMsg (coercedType.getSubModules == null) assert lib.assertMsg (coercedType.getSubModules == null)
@ -599,12 +599,12 @@ rec {
mkOptionType rec { mkOptionType rec {
name = "coercedTo"; name = "coercedTo";
description = "${finalType.description} or ${coercedType.description} convertible to it"; description = "${finalType.description} or ${coercedType.description} convertible to it";
check = x: finalType.check x || (coercedType.check x && finalType.check (coerceFunc x)); check = x: (coercedType.check x && finalType.check (coerceFunc x)) || finalType.check x;
merge = loc: defs: merge = loc: defs:
let let
coerceVal = val: coerceVal = val:
if finalType.check val then val if coercedType.check val then coerceFunc val
else coerceFunc val; else val;
in finalType.merge loc (map (def: def // { value = coerceVal def.value; }) defs); in finalType.merge loc (map (def: def // { value = coerceVal def.value; }) defs);
emptyValue = finalType.emptyValue; emptyValue = finalType.emptyValue;
getSubOptions = finalType.getSubOptions; getSubOptions = finalType.getSubOptions;