Skip to content

Commit d9502ab

Browse files
authored
improve heuristic for detecting if curried (#36)
* improve heuristic for detecting if curried This adds a check that ensures it is either a native function or an attribute set that only has __functor and/or __functionArgs attributes * add tests for provides with functors
1 parent 88bac0f commit d9502ab

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
mkFlake,
3+
evalMod,
4+
...
5+
}:
6+
{
7+
8+
flake.tests."test provides with functors" =
9+
let
10+
flake = mkFlake {
11+
flake.aspects =
12+
{ aspects, ... }:
13+
{
14+
aspectOne = {
15+
includes = [ aspects.aspectTwo._.aspectThree._.aspectFour ];
16+
classOne = { };
17+
};
18+
aspectTwo.provides.aspectThree = {
19+
provides.aspectFour.classOne.bar = [ "hello" ];
20+
__functor = self: _: self;
21+
};
22+
};
23+
};
24+
25+
expr = (evalMod "classOne" flake.modules.classOne.aspectOne).bar;
26+
expected = [
27+
"hello"
28+
];
29+
in
30+
builtins.trace expr {
31+
inherit expr expected;
32+
};
33+
}

nix/types.nix

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ let
4646
directProviderFn = lib.types.addCheck (lib.types.functionTo aspectSubmodule) isProviderFn;
4747

4848
# Curried provider function: (params) → provider (enables parametrization)
49-
curriedProviderFn = lib.types.functionTo providerType;
49+
curriedProviderFn = lib.types.addCheck (lib.types.functionTo providerType) (
50+
f:
51+
builtins.isFunction f
52+
|| lib.isAttrs f && lib.subtractLists [ "__functor" "__functionArgs" ] (lib.attrNames f) == [ ]
53+
);
5054

5155
# Any provider function: direct or curried
5256
providerFn = lib.types.either directProviderFn curriedProviderFn;

0 commit comments

Comments
 (0)