Share common code for merging option definitions.
This move idioms which were used in `evalOptionValue` and in the `merge` functions of `listOf` and `attrsOf` types, such that we can use a names such as `isDefined` and `optionalValue` instead or repeating identical comparisons of `defsFinal == []`.
This commit is contained in:
parent
dd4f5f6b78
commit
ed91474e9b
|
@ -250,14 +250,14 @@ rec {
|
||||||
# Add in the default value for this option, if any.
|
# Add in the default value for this option, if any.
|
||||||
defs' = (optional (opt ? default)
|
defs' = (optional (opt ? default)
|
||||||
{ file = head opt.declarations; value = mkOptionDefault opt.default; }) ++ defs;
|
{ file = head opt.declarations; value = mkOptionDefault opt.default; }) ++ defs;
|
||||||
|
|
||||||
# Handle properties, check types, and merge everything together
|
# Handle properties, check types, and merge everything together
|
||||||
inherit (mergeDefinitions loc opt.type defs') defsFinal mergedValue;
|
inherit (mergeDefinitions loc opt.type defs') isDefined defsFinal mergedValue;
|
||||||
files = map (def: def.file) defsFinal;
|
files = map (def: def.file) defsFinal;
|
||||||
merged =
|
merged =
|
||||||
if defsFinal == [] then
|
if isDefined then mergedValue
|
||||||
throw "The option `${showOption loc}' is used but not defined."
|
else throw "The option `${showOption loc}' is used but not defined.";
|
||||||
else
|
|
||||||
mergedValue;
|
|
||||||
# Finally, apply the ‘apply’ function to the merged
|
# Finally, apply the ‘apply’ function to the merged
|
||||||
# value. This allows options to yield a value computed
|
# value. This allows options to yield a value computed
|
||||||
# from the definitions.
|
# from the definitions.
|
||||||
|
@ -265,8 +265,7 @@ rec {
|
||||||
in opt //
|
in opt //
|
||||||
{ value = addErrorContext "while evaluating the option `${showOption loc}':" value;
|
{ value = addErrorContext "while evaluating the option `${showOption loc}':" value;
|
||||||
definitions = map (def: def.value) defsFinal;
|
definitions = map (def: def.value) defsFinal;
|
||||||
isDefined = defsFinal != [];
|
inherit isDefined files;
|
||||||
inherit files;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
# Merge definitions of a value of a given type
|
# Merge definitions of a value of a given type
|
||||||
|
@ -295,6 +294,11 @@ rec {
|
||||||
if type.check def.value then res
|
if type.check def.value then res
|
||||||
else throw "The option value `${showOption loc}' in `${def.file}' is not a ${type.name}.")
|
else throw "The option value `${showOption loc}' in `${def.file}' is not a ${type.name}.")
|
||||||
(type.merge loc defsFinal) defsFinal;
|
(type.merge loc defsFinal) defsFinal;
|
||||||
|
|
||||||
|
isDefined = defsFinal != [];
|
||||||
|
optionalValue =
|
||||||
|
if isDefined then { value = mergedValue; }
|
||||||
|
else {};
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Given a config set, expand mkMerge properties, and push down the
|
/* Given a config set, expand mkMerge properties, and push down the
|
||||||
|
|
|
@ -6,10 +6,9 @@ with import ./attrsets.nix;
|
||||||
with import ./options.nix;
|
with import ./options.nix;
|
||||||
with import ./trivial.nix;
|
with import ./trivial.nix;
|
||||||
with import ./strings.nix;
|
with import ./strings.nix;
|
||||||
|
with {inherit (import ./modules.nix) mergeDefinitions; };
|
||||||
|
|
||||||
let
|
rec {
|
||||||
inherit (import ./modules.nix) mergeDefinitions;
|
|
||||||
in rec {
|
|
||||||
|
|
||||||
isType = type: x: (x._type or "") == type;
|
isType = type: x: (x._type or "") == type;
|
||||||
|
|
||||||
|
@ -114,11 +113,12 @@ in rec {
|
||||||
check = isList;
|
check = isList;
|
||||||
merge = loc: defs:
|
merge = loc: defs:
|
||||||
map (x: x.value) (filter (x: x ? value) (concatLists (imap (n: def: imap (m: def':
|
map (x: x.value) (filter (x: x ? value) (concatLists (imap (n: def: imap (m: def':
|
||||||
let
|
(mergeDefinitions
|
||||||
inherit (mergeDefinitions (loc ++ ["[definition ${toString n}-entry ${toString m}]"])
|
(loc ++ ["[definition ${toString n}-entry ${toString m}]"])
|
||||||
elemType [{ inherit (def) file; value = def'; }]
|
elemType
|
||||||
) defsFinal mergedValue;
|
[{ inherit (def) file; value = def'; }]
|
||||||
in if defsFinal == [] then {} else { value = mergedValue; }) def.value) defs)));
|
).optionalValue
|
||||||
|
) def.value) defs)));
|
||||||
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["*"]);
|
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["*"]);
|
||||||
getSubModules = elemType.getSubModules;
|
getSubModules = elemType.getSubModules;
|
||||||
substSubModules = m: listOf (elemType.substSubModules m);
|
substSubModules = m: listOf (elemType.substSubModules m);
|
||||||
|
@ -129,10 +129,8 @@ in rec {
|
||||||
check = isAttrs;
|
check = isAttrs;
|
||||||
merge = loc: defs:
|
merge = loc: defs:
|
||||||
mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs:
|
mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs:
|
||||||
let
|
(mergeDefinitions (loc ++ [name]) elemType defs).optionalValue
|
||||||
inherit (mergeDefinitions (loc ++ [name]) elemType defs)
|
)
|
||||||
defsFinal mergedValue;
|
|
||||||
in if defsFinal == [] then {} else { value = mergedValue; })
|
|
||||||
# Push down position info.
|
# Push down position info.
|
||||||
(map (def: listToAttrs (mapAttrsToList (n: def':
|
(map (def: listToAttrs (mapAttrsToList (n: def':
|
||||||
{ name = n; value = { inherit (def) file; value = def'; }; }) def.value)) defs)));
|
{ name = n; value = { inherit (def) file; value = def'; }; }) def.value)) defs)));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user