From dc2ce818b854eaa1b4c7ada7ec1c2b921c8d395c Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Mon, 17 Jun 2024 03:59:11 -0700 Subject: [PATCH] feat: apply dag defaults --- lib/src/dag/default.nix | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/src/dag/default.nix b/lib/src/dag/default.nix index a904740..15bdb6b 100644 --- a/lib/src/dag/default.nix +++ b/lib/src/dag/default.nix @@ -59,7 +59,34 @@ lib: { }; apply = { - # defaults = graph: defaults: + ## Apply a set of defaults to a graph. This will ensure that missing entries are added + ## and any entries that do exist are given the appropriate `before` and `after` values. + ## + ## @type Dag a -> Dag a -> Dag a + defaults = graph: defaults: let + result = + builtins.mapAttrs + ( + name: entry: + if defaults ? ${name} + then + if builtins.isString entry + then { + value = entry; + before = defaults.${name}.before or []; + after = defaults.${name}.after or []; + } + else + entry + // { + before = (entry.before or []) ++ (defaults.${name}.before or []); + after = (entry.after or []) ++ (defaults.${name}.after or []); + } + else entry + ) + graph; + in + defaults // result; }; ## Map over the entries in a DAG and modify their values.