zaphyra's git: haumea

fork of https://github.com/nix-community/haumea

commit bac89b61897ef3833a7c6762fe65d268431551e1
parent 6cc337b687cc73409f6fbfc49e313168547ff5e2
Author: figsoda <figsoda@pm.me>
Date: Mon, 10 Apr 2023 18:34:53 -0400

load: accept a nested list of transformers
5 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
@@ -41,9 +41,9 @@ Arguments:
   `self`, `super`, and `root` are reserved names that cannot be passed as an input.
   To work around that, remove them using `removeAttrs`, or pass them by overriding the loader.
 
-- (optional) `transformer` : `(cursor : [ String ]) -> { ... } -> a`
+- (optional) `transformer` : `(cursor : [ String ]) -> { ... } -> a` or a list of transformers
 
-  Module transformer, defaults to `_: id` (no transformation).
+  Module transformer, defaults to `[ ]` (no transformation).
   This will transform each directory module in `src`, including the root.
 
 The main entry point of haumea. This is probably the function you are looking for.
diff --git a/src/load.nix b/src/load.nix
@@ -14,16 +14,15 @@ let
   inherit (lib)
     concatMapAttrs
     fix
+    flatten
     flip
     getAttrFromPath
     hasSuffix
-    id
     nameValuePair
     optionalAttrs
     pipe
     remove
     take
-    toList
     ;
 
   parsePath = suffix: path:

@@ -122,11 +121,11 @@ in
 { src
 , loader ? root.loaders.default
 , inputs ? { }
-, transformer ? _: id
+, transformer ? [ ]
 }:
 let
   transformer' = cursor: flip pipe
-    (map (t: t cursor) (toList transformer));
+    (map (t: t cursor) (flatten transformer));
 in
 
 assert all
diff --git a/tests/transformer/__fixture/default.nix b/tests/transformer/__fixture/default.nix
@@ -0,0 +1,3 @@
+{
+  answer = 42;
+}
diff --git a/tests/transformer/expected.nix b/tests/transformer/expected.nix
@@ -0,0 +1 @@
+"99"
diff --git a/tests/transformer/expr.nix b/tests/transformer/expr.nix
@@ -0,0 +1,17 @@
+{ haumea }:
+
+haumea.load {
+  src = ./__fixture;
+  transformer = [
+    haumea.transformers.liftDefault
+    (_: { answer }: answer)
+    [
+      (_: x: x / 6)
+      [
+        (_: x: x + 2)
+        (_: toString)
+      ]
+    ]
+    (_: x: x + x)
+  ];
+}