Skip to content

Commit

Permalink
fix: injection logic overhaul
Browse files Browse the repository at this point in the history
Allow `_else` clauses in `when` checked sets, also exports `inject` via
the std library and uses it to fix the std import tests.
  • Loading branch information
nrdxp committed Dec 22, 2024
1 parent 51ebe94 commit affbdc7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
6 changes: 5 additions & 1 deletion atom-nix/core/compose.nix
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,18 @@ let
__getEnv = errors.getEnv "";
__getFlake = errors.import;
get = extern;
std = if __isStd__ then atomScope else std;
};

scope'' = core.set.inject scope' [
preOpt
{
_if = !__isStd__;
atom = atomScope;
_else.std = atomScope;
}
{
_if = !__isStd__ && l.elem "std" coreFeatures';
inherit std;
}
{
_if = __internal__test;
Expand Down
10 changes: 7 additions & 3 deletions atom-nix/core/mod.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ let
importAtom = import ./importAtom.nix;
fix = import ../std/fix.nix;
when = scopedImport { std = builtins; } ../std/set/when.nix;
inject = scopedImport {
std = builtins;
mod = {
inherit when;
};
} ../std/set/inject.nix;
filterMap = scopedImport { std = builtins; } ../std/set/filterMap.nix;
make = scopedImport { std = builtins; } ../std/path/make.nix;
parse = scopedImport { std = builtins; } ../std/file/parse.nix;
Expand Down Expand Up @@ -36,7 +42,7 @@ rec {
inherit parse;
};
set = {
inherit when;
inherit when inject;
};

compose = import ./compose.nix;
Expand Down Expand Up @@ -82,8 +88,6 @@ rec {
${toString dir}/mod.nix
'';

set.inject = l.foldl' (acc: x: acc // when x);

pureBuiltinsForStd =
std:
filterMap (
Expand Down
1 change: 1 addition & 0 deletions atom-nix/std/set/inject.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.foldl' (acc: x: acc // mod.when x)
1 change: 1 addition & 0 deletions atom-nix/std/set/mod.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
When = mod.when;
FilterMap = mod.filterMap;
Merge = mod.merge;
Inject = mod.inject;
}
5 changes: 3 additions & 2 deletions atom-nix/std/set/when.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
```nix
when { _if = true; foo = "bar"; } => { foo = "bar"; }
when { _if = false; foo = "bar"; _else.foo = "buz"; } => { foo = "buz"; }
when { _if = false; foo = "bar"; } => { }
```
Expand All @@ -20,6 +21,6 @@
# Return Value
If the `_if` attribute of the input set is `true` or missing, returns the input set with the `_if` attribute removed. Otherwise, returns an empty attribute set.
If the `_if` attribute of the input set is `true` or missing, returns the input set with the `_if` attribute removed. Otherwise, returns the `_else` attribute or an empty attribute set if missing.
*/
set: if set._if or true then std.removeAttrs set [ "_if" ] else { }
set: if set._if or true then std.removeAttrs set [ "_if" ] else set._else or { }

0 comments on commit affbdc7

Please sign in to comment.