add helper to lib/attrsets: hasAttrByPath

Changed files
+21
lib
+11
lib/attrsets.nix
···
then attrByPath (tail attrPath) default e.${attr}
else default;
/* Return nested attribute set in which an attribute is set. For instance
["x" "y"] applied with some value v returns `x.y = v;' */
···
then attrByPath (tail attrPath) default e.${attr}
else default;
+
/* Return if an attribute from nested attribute set exists.
+
For instance ["x" "y"] applied to some set e returns true, if e.x.y exists. False
+
is returned otherwise. */
+
hasAttrByPath = attrPath: e:
+
let attr = head attrPath;
+
in
+
if attrPath == [] then true
+
else if e ? ${attr}
+
then hasAttrByPath (tail attrPath) e.${attr}
+
else false;
+
/* Return nested attribute set in which an attribute is set. For instance
["x" "y"] applied with some value v returns `x.y = v;' */
+10
lib/tests.nix
···
expected = { success = false; value = false; };
};
}
···
expected = { success = false; value = false; };
};
+
testHasAttrByPathTrue = {
+
expr = hasAttrByPath ["a" "b"] { a = { b = "yey"; }; };
+
expected = true;
+
};
+
+
testHasAttrByPathFalse = {
+
expr = hasAttrByPath ["a" "b"] { a = { c = "yey"; }; };
+
expected = false;
+
};
+
}