feat(sterni/nix/fun): add hasEllipsis
As a complementation to builtins.functionArgs this function checks if
the function has a set pattern that contains an ellipsis
(i. e. `{ [arg, [ arg1, [ … ]]] ... }:`). The implementation of this is
pretty cursed however since there is no clean way to do this in vanilla
nix: We need to match on the output of builtins.toXML which does try to
serialize functions by outputting their argument and information about
it (whether it is a normal argument or a attribute set pattern, in the
latter case it also serialize every component of the pattern).
Change-Id: I0f33721811a3180cec205a0c98e6d92e10e92075
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2950
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
parent
e09b44bdec
commit
f59ab9aba5
2 changed files with 43 additions and 0 deletions
|
|
@ -26,6 +26,19 @@ let
|
||||||
lrs = x: fs:
|
lrs = x: fs:
|
||||||
builtins.foldl' (v: f: f v) x fs;
|
builtins.foldl' (v: f: f v) x fs;
|
||||||
|
|
||||||
|
# Warning: cursed function
|
||||||
|
#
|
||||||
|
# Check if a function has an attribute
|
||||||
|
# set pattern with an ellipsis as its argument.
|
||||||
|
#
|
||||||
|
# s/o to puck for discovering that you could use
|
||||||
|
# builtins.toXML to introspect functions more than
|
||||||
|
# you should be able to in Nix.
|
||||||
|
hasEllipsis = f:
|
||||||
|
builtins.isFunction f &&
|
||||||
|
builtins.match ".*<attrspat ellipsis=\"1\">.*"
|
||||||
|
(builtins.toXML f) != null;
|
||||||
|
|
||||||
in
|
in
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
@ -41,5 +54,6 @@ in
|
||||||
rls
|
rls
|
||||||
lr
|
lr
|
||||||
lrs
|
lrs
|
||||||
|
hasEllipsis
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
29
users/sterni/nix/fun/tests/default.nix
Normal file
29
users/sterni/nix/fun/tests/default.nix
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
{ depot, ... }:
|
||||||
|
|
||||||
|
let
|
||||||
|
inherit (depot.nix.runTestsuite)
|
||||||
|
runTestsuite
|
||||||
|
it
|
||||||
|
assertEq
|
||||||
|
;
|
||||||
|
|
||||||
|
inherit (depot.users.sterni.nix)
|
||||||
|
fun
|
||||||
|
;
|
||||||
|
|
||||||
|
hasEllipsisTests = it "checks fun.hasEllipsis" [
|
||||||
|
(assertEq "Malicious string" false
|
||||||
|
(fun.hasEllipsis (builtins.toXML ({ foo, ... }: 12))))
|
||||||
|
(assertEq "No function" false
|
||||||
|
(fun.hasEllipsis 23))
|
||||||
|
(assertEq "No attribute set pattern" false
|
||||||
|
(fun.hasEllipsis (a: a + 2)))
|
||||||
|
(assertEq "No ellipsis" false
|
||||||
|
(fun.hasEllipsis ({ foo, bar }: foo + bar)))
|
||||||
|
(assertEq "Ellipsis" true
|
||||||
|
(fun.hasEllipsis ({ depot, pkgs, ... }: 42)))
|
||||||
|
];
|
||||||
|
in
|
||||||
|
runTestsuite "nix.fun" [
|
||||||
|
hasEllipsisTests
|
||||||
|
]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue