forked from mirrors/nixpkgs
Merge pull request #46336 from Infinisil/overrideExisting
lib: Improve overrideExisting implementation
This commit is contained in:
commit
83d89edc6e
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue