lib: improve filterAttrs (#345547)
This commit is contained in:
commit
5690833b4f
@ -7,7 +7,7 @@ let
|
|||||||
inherit (builtins) head length;
|
inherit (builtins) head length;
|
||||||
inherit (lib.trivial) oldestSupportedReleaseIsAtLeast mergeAttrs warn warnIf;
|
inherit (lib.trivial) oldestSupportedReleaseIsAtLeast mergeAttrs warn warnIf;
|
||||||
inherit (lib.strings) concatStringsSep concatMapStringsSep escapeNixIdentifier sanitizeDerivationName;
|
inherit (lib.strings) concatStringsSep concatMapStringsSep escapeNixIdentifier sanitizeDerivationName;
|
||||||
inherit (lib.lists) foldr foldl' concatMap elemAt all partition groupBy take foldl;
|
inherit (lib.lists) filter foldr foldl' concatMap elemAt all partition groupBy take foldl;
|
||||||
in
|
in
|
||||||
|
|
||||||
rec {
|
rec {
|
||||||
@ -644,8 +644,7 @@ rec {
|
|||||||
filterAttrs =
|
filterAttrs =
|
||||||
pred:
|
pred:
|
||||||
set:
|
set:
|
||||||
listToAttrs (concatMap (name: let v = set.${name}; in if pred name v then [(nameValuePair name v)] else []) (attrNames set));
|
removeAttrs set (filter (name: ! pred name set.${name}) (attrNames set));
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Filter an attribute set recursively by removing all attributes for
|
Filter an attribute set recursively by removing all attributes for
|
||||||
|
@ -47,6 +47,7 @@ let
|
|||||||
evalModules
|
evalModules
|
||||||
extends
|
extends
|
||||||
filter
|
filter
|
||||||
|
filterAttrs
|
||||||
fix
|
fix
|
||||||
fold
|
fold
|
||||||
foldAttrs
|
foldAttrs
|
||||||
@ -1102,6 +1103,25 @@ runTests {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testFilterAttrs = {
|
||||||
|
expr = filterAttrs (n: v: n != "a" && (v.hello or false) == true) {
|
||||||
|
a.hello = true;
|
||||||
|
b.hello = true;
|
||||||
|
c = {
|
||||||
|
hello = true;
|
||||||
|
world = false;
|
||||||
|
};
|
||||||
|
d.hello = false;
|
||||||
|
};
|
||||||
|
expected = {
|
||||||
|
b.hello = true;
|
||||||
|
c = {
|
||||||
|
hello = true;
|
||||||
|
world = false;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
# code from example
|
# code from example
|
||||||
testFoldlAttrs = {
|
testFoldlAttrs = {
|
||||||
expr = {
|
expr = {
|
||||||
|
Loading…
Reference in New Issue
Block a user