lib/digga: generalize flattenTree

This commit is contained in:
Infinidoge 2024-10-31 13:00:15 -04:00
parent fc3f03beb3
commit 610b97519d
Signed by: Infinidoge
SSH key fingerprint: SHA256:oAMyvotlNFraMmZmr+p6AxnNfW/GioTs1pOn3V4tQ7A

View file

@ -2,12 +2,14 @@
# Importers from digga: https://github.com/divnix/digga/blob/main/src/importers.nix # Importers from digga: https://github.com/divnix/digga/blob/main/src/importers.nix
# Plus the mkHomeConfigurations generator from digga: https://github.com/divnix/digga/blob/main/src/generators.nix # Plus the mkHomeConfigurations generator from digga: https://github.com/divnix/digga/blob/main/src/generators.nix
let let
flattenTree = flattenTree' =
/* /*
* *
Synopsis: flattenTree _tree_ Synopsis: flattenTree' _cond_ _sep_ _tree_
Flattens a _tree_ of the shape that is produced by rakeLeaves. Flattens a _tree_ of the shape that is produced by rakeLeaves.
_cond_ determines when to stop recursing
_sep_ is the separator to join the path with
Output Format: Output Format:
An attrset with names in the spirit of the Reverse DNS Notation form An attrset with names in the spirit of the Reverse DNS Notation form
@ -16,35 +18,34 @@ let
Example input: Example input:
``` ```
{ {
a = { a = {
b = { b = {
c = <path>; c = <path>;
}; };
}; };
} }
``` ```
Example output: Example output:
``` ```
{ {
"a.b.c" = <path>; "a.b.c" = <path>;
} }
``` ```
* *
*/ */
cond:
sep:
tree: tree:
let let
op = sum: path: val: op = sum: path: val:
let let
pathStr = builtins.concatStringsSep "." path; # dot-based reverse DNS notation pathStr = builtins.concatStringsSep sep path; # dot-based reverse DNS notation
in in
if builtins.isPath val if cond val
then then
# builtins.trace "${toString val} is a path" # builtins.trace "${toString val} matches condition"
(sum (sum // { "${pathStr}" = val; })
// {
"${pathStr}" = val;
})
else if builtins.isAttrs val else if builtins.isAttrs val
then then
# builtins.trace "${builtins.toJSON val} is an attrset" # builtins.trace "${builtins.toJSON val} is an attrset"
@ -63,6 +64,8 @@ let
in in
recurse { } [ ] tree; recurse { } [ ] tree;
flattenTree = flattenTree' builtins.isPath ".";
rakeLeaves = rakeLeaves =
/* /*
* *
@ -133,7 +136,7 @@ let
fqdn; fqdn;
in in
{ {
inherit rakeLeaves flattenTree flattenLeaves; inherit rakeLeaves flattenTree flattenTree' flattenLeaves;
leaves = dir: builtins.attrValues (flattenLeaves dir); leaves = dir: builtins.attrValues (flattenLeaves dir);