lib.makeOverridable: simplify function arguments preservation

Rename temporary variable copyArgs -> mirrorArgs.
Use lib.mirrorFunctionArgs to define `mirrorArgs`.
Apply mirrorArgs also to the returned function.
This commit is contained in:
Yueh-Shun Li 2023-05-29 05:52:21 +08:00
parent a16319908e
commit 6a8b6b8f72

View file

@ -76,19 +76,22 @@ rec {
Type: Type:
makeOverridable :: (AttrSet -> a) -> AttrSet -> a makeOverridable :: (AttrSet -> a) -> AttrSet -> a
*/ */
makeOverridable = f: lib.setFunctionArgs makeOverridable = f:
(origArgs: let let
# Creates a functor with the same arguments as f
mirrorArgs = lib.mirrorFunctionArgs f;
in
mirrorArgs (origArgs:
let
result = f origArgs; result = f origArgs;
# Creates a functor with the same arguments as f
copyArgs = g: lib.setFunctionArgs g (lib.functionArgs f);
# Changes the original arguments with (potentially a function that returns) a set of new attributes # Changes the original arguments with (potentially a function that returns) a set of new attributes
overrideWith = newArgs: origArgs // (if lib.isFunction newArgs then newArgs origArgs else newArgs); overrideWith = newArgs: origArgs // (if lib.isFunction newArgs then newArgs origArgs else newArgs);
# Re-call the function but with different arguments # Re-call the function but with different arguments
overrideArgs = copyArgs (newArgs: makeOverridable f (overrideWith newArgs)); overrideArgs = mirrorArgs (newArgs: makeOverridable f (overrideWith newArgs));
# Change the result of the function call by applying g to it # Change the result of the function call by applying g to it
overrideResult = g: makeOverridable (copyArgs (args: g (f args))) origArgs; overrideResult = g: makeOverridable (mirrorArgs (args: g (f args))) origArgs;
in in
if builtins.isAttrs result then if builtins.isAttrs result then
result // { result // {
@ -102,8 +105,7 @@ rec {
lib.setFunctionArgs result (lib.functionArgs result) // { lib.setFunctionArgs result (lib.functionArgs result) // {
override = overrideArgs; override = overrideArgs;
} }
else result) else result);
(lib.functionArgs f);
/* Call the package function in the file `fn` with the required /* Call the package function in the file `fn` with the required