summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorJörg Thalheim <Mic92@users.noreply.github.com>2018-09-18 11:37:30 +0100
committerGitHub <noreply@github.com>2018-09-18 11:37:30 +0100
commit83d89edc6eaff506e42e099fa4cdf45d09e7ec5f (patch)
tree7058e7ee23ffb5c874a6e589461abbe762071b58 /lib
parent7d39e99a1ec67ac182796f517fc61c3824771c32 (diff)
parentc7104d97c87316a6062c88696d80a45918fab7a6 (diff)
downloadnixlib-83d89edc6eaff506e42e099fa4cdf45d09e7ec5f.tar
nixlib-83d89edc6eaff506e42e099fa4cdf45d09e7ec5f.tar.gz
nixlib-83d89edc6eaff506e42e099fa4cdf45d09e7ec5f.tar.bz2
nixlib-83d89edc6eaff506e42e099fa4cdf45d09e7ec5f.tar.lz
nixlib-83d89edc6eaff506e42e099fa4cdf45d09e7ec5f.tar.xz
nixlib-83d89edc6eaff506e42e099fa4cdf45d09e7ec5f.tar.zst
nixlib-83d89edc6eaff506e42e099fa4cdf45d09e7ec5f.zip
Merge pull request #46336 from Infinisil/overrideExisting
lib: Improve overrideExisting implementation
Diffstat (limited to 'lib')
-rw-r--r--lib/attrsets.nix11
-rw-r--r--lib/tests/misc.nix14
2 files changed, 21 insertions, 4 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix
index 1e4142562fa6..2a1b866dbc5e 100644
--- a/lib/attrsets.nix
+++ b/lib/attrsets.nix
@@ -435,12 +435,15 @@ rec {
     useful for deep-overriding.
 
     Example:
-      x = { a = { b = 4; c = 3; }; }
-      overrideExisting x { a = { b = 6; d = 2; }; }
-      => { a = { b = 6; d = 2; }; }
+      overrideExisting {} { a = 1; }
+      => {}
+      overrideExisting { b = 2; } { a = 1; }
+      => { b = 2; }
+      overrideExisting { a = 3; b = 2; } { a = 1; }
+      => { a = 1; b = 2; }
   */
   overrideExisting = old: new:
-    old // listToAttrs (map (attr: nameValuePair attr (attrByPath [attr] old.${attr} new)) (attrNames old));
+    mapAttrs (name: value: new.${name} or value) old;
 
   /* Get a package output.
      If no output is found, fallback to `.out` and then to the default.
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index d3bd7746d89c..d89bcfde4819 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -236,6 +236,20 @@ runTests {
     };
   };
 
+  testOverrideExistingEmpty = {
+    expr = overrideExisting {} { a = 1; };
+    expected = {};
+  };
+
+  testOverrideExistingDisjoint = {
+    expr = overrideExisting { b = 2; } { a = 1; };
+    expected = { b = 2; };
+  };
+
+  testOverrideExistingOverride = {
+    expr = overrideExisting { a = 3; b = 2; } { a = 1; };
+    expected = { a = 1; b = 2; };
+  };
 
 # GENERATORS
 # these tests assume attributes are converted to lists