diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2015-12-16 15:46:12 +0100 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2015-12-16 15:46:12 +0100 |
commit | 064ceef7926c612d7509a10e47eb2d2e3f338f42 (patch) | |
tree | d7c8c27ff2c359c59cfe6fa76355c9e27c07ae8c /lib | |
parent | 72014cade68674d478d155234c42e6b6fb52b74f (diff) | |
parent | ce96057588821156d08b48cf8647b6cb8730e63c (diff) |
Merge pull request #11455 from zagy/add/hasAttrByPath
add helper to lib/attrsets: hasAttrByPath
Diffstat (limited to 'lib')
-rw-r--r-- | lib/attrsets.nix | 11 | ||||
-rw-r--r-- | lib/tests.nix | 10 |
2 files changed, 21 insertions, 0 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 22ecc808679d0..84f6cb3658b90 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -23,6 +23,17 @@ rec { 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;' */ diff --git a/lib/tests.nix b/lib/tests.nix index 1fb2cbf5b5361..c3b8839fda956 100644 --- a/lib/tests.nix +++ b/lib/tests.nix @@ -120,4 +120,14 @@ runTests { 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; + }; + } |