add allAttrs function
This commit is contained in:
parent
28b360afe4
commit
c135b7222b
1 changed files with 20 additions and 6 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
{this, ...}: let
|
{this, ...}: let
|
||||||
inherit
|
inherit
|
||||||
(builtins)
|
(builtins)
|
||||||
|
all
|
||||||
attrNames
|
attrNames
|
||||||
elem
|
elem
|
||||||
elemAt
|
elemAt
|
||||||
|
|
@ -31,18 +32,16 @@ in rec {
|
||||||
assert enfIsAttrs xs msg;
|
assert enfIsAttrs xs msg;
|
||||||
hasAttr name xs || throw "${msg}: missing required attribute \"${name}\"";
|
hasAttr name xs || throw "${msg}: missing required attribute \"${name}\"";
|
||||||
|
|
||||||
getAttrOr = name: f: xs:
|
getAttrOr = name: f: xs: xs.${name} or f xs;
|
||||||
if xs ? ${name}
|
getAttrDefault = name: default: xs: xs.${name} or default;
|
||||||
then xs.${name}
|
|
||||||
else f xs;
|
|
||||||
|
|
||||||
getAttrDefault = name: default: getAttrOr name (_: default);
|
|
||||||
|
|
||||||
genAttrs = names: f:
|
genAttrs = names: f:
|
||||||
names
|
names
|
||||||
|> map (n: nameValuePair n (f n))
|
|> map (n: nameValuePair n (f n))
|
||||||
|> listToAttrs;
|
|> listToAttrs;
|
||||||
|
|
||||||
|
attrsToList = xs: mapAttrs nameValuePair xs;
|
||||||
|
|
||||||
mergeAttrsList = list: let
|
mergeAttrsList = list: let
|
||||||
# `binaryMerge start end` merges the elements at indices `index` of `list` such that `start <= index < end`
|
# `binaryMerge start end` merges the elements at indices `index` of `list` such that `start <= index < end`
|
||||||
# Type: Int -> Int -> Attrs
|
# Type: Int -> Int -> Attrs
|
||||||
|
|
@ -85,4 +84,19 @@ in rec {
|
||||||
|> removeAttrs xs;
|
|> removeAttrs xs;
|
||||||
|
|
||||||
nameValuePair = name: value: {inherit name value;};
|
nameValuePair = name: value: {inherit name value;};
|
||||||
|
|
||||||
|
# allAttrs :: (String -> Any -> Bool) -> AttrSet -> Bool
|
||||||
|
allAttrs = pred: xs:
|
||||||
|
attrNames xs |> all (name: pred name xs.${name});
|
||||||
|
|
||||||
|
enfAllAttrs = pred: xs: xsName: reason: msg: let
|
||||||
|
err = name: throw "${msg}: attribute \"${xsName}.${name}\" cannot ${reason}";
|
||||||
|
in
|
||||||
|
attrNames xs
|
||||||
|
|> all (name: pred name xs.${name} || err name);
|
||||||
|
|
||||||
|
# XXX: TODO: for every enf* function make an equivalent assert function
|
||||||
|
# XXX: TODO: (these MUST be chainable for the pipe operator)
|
||||||
|
assertAllAttrs = pred: xsName: reason: msg: xs:
|
||||||
|
assert enfAllAttrs pred xs xsName reason msg; xs;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue