mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-22 23:13:19 +00:00
Merge pull request #270133 from adisbladis/lib-attrbypath-allocs
lib.(has|)attrByPath: Don't needlessly allocate lists in loop
This commit is contained in:
commit
bbb4adee2f
@ -34,12 +34,20 @@ rec {
|
||||
default:
|
||||
# The nested attribute set to select values from
|
||||
set:
|
||||
let attr = head attrPath;
|
||||
let
|
||||
lenAttrPath = length attrPath;
|
||||
attrByPath' = n: s: (
|
||||
if n == lenAttrPath then s
|
||||
else (
|
||||
let
|
||||
attr = elemAt attrPath n;
|
||||
in
|
||||
if s ? ${attr} then attrByPath' (n + 1) s.${attr}
|
||||
else default
|
||||
)
|
||||
);
|
||||
in
|
||||
if attrPath == [] then set
|
||||
else if set ? ${attr}
|
||||
then attrByPath (tail attrPath) default set.${attr}
|
||||
else default;
|
||||
attrByPath' 0 set;
|
||||
|
||||
/* Return if an attribute from nested attribute set exists.
|
||||
|
||||
@ -58,13 +66,19 @@ rec {
|
||||
attrPath:
|
||||
# The nested attribute set to check
|
||||
e:
|
||||
let attr = head attrPath;
|
||||
let
|
||||
lenAttrPath = length attrPath;
|
||||
hasAttrByPath' = n: s: (
|
||||
n == lenAttrPath || (
|
||||
let
|
||||
attr = elemAt attrPath n;
|
||||
in
|
||||
if s ? ${attr} then hasAttrByPath' (n + 1) s.${attr}
|
||||
else false
|
||||
)
|
||||
);
|
||||
in
|
||||
if attrPath == [] then true
|
||||
else if e ? ${attr}
|
||||
then hasAttrByPath (tail attrPath) e.${attr}
|
||||
else false;
|
||||
|
||||
hasAttrByPath' 0 e;
|
||||
|
||||
/* Create a new attribute set with `value` set at the nested attribute location specified in `attrPath`.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user