mirror of
https://gitlab.com/rensa-nix/core.git
synced 2025-12-11 22:00:09 +01:00
feat: add support for wildcards (*) in filter & select
This commit is contained in:
parent
7f7c52d383
commit
abe19f9f13
2 changed files with 60 additions and 4 deletions
|
|
@ -1,6 +1,57 @@
|
||||||
{l}: let
|
{l}: let
|
||||||
filter = pred: t: p: let
|
filter = pred: t: p: let
|
||||||
multiplePaths = l.isList (l.elemAt p 0);
|
multiplePaths = l.isList (l.elemAt p 0);
|
||||||
|
|
||||||
|
# function to expand wildcards in a path
|
||||||
|
expandPath = path:
|
||||||
|
if !(l.elem "*" path)
|
||||||
|
then [path]
|
||||||
|
else let
|
||||||
|
availableCells = t.__ren.cells or [];
|
||||||
|
|
||||||
|
# get the first valid system
|
||||||
|
sampleSystem = l.head (l.filter (n: l.elem n l.systems.doubles.all) (l.attrNames t));
|
||||||
|
|
||||||
|
# handle different wildcard positions
|
||||||
|
expandWildcardAt = pos:
|
||||||
|
if pos >= l.length path
|
||||||
|
then [path]
|
||||||
|
else if (l.elemAt path pos) != "*"
|
||||||
|
then expandWildcardAt (pos + 1)
|
||||||
|
else let
|
||||||
|
prefix = l.take pos path;
|
||||||
|
suffix = l.drop (pos + 1) path;
|
||||||
|
|
||||||
|
choices =
|
||||||
|
if pos == 0 # cell names
|
||||||
|
then availableCells
|
||||||
|
else if pos == 1 # cell block names
|
||||||
|
then let
|
||||||
|
cellName = l.elemAt path 0;
|
||||||
|
cellAttrs = t.${sampleSystem}.${cellName} or {};
|
||||||
|
in
|
||||||
|
l.attrNames cellAttrs
|
||||||
|
else # target names (pos >= 2)
|
||||||
|
let
|
||||||
|
cellName = l.elemAt path 0;
|
||||||
|
blockName = l.elemAt path 1;
|
||||||
|
blockAttrs = t.${sampleSystem}.${cellName}.${blockName} or {};
|
||||||
|
in
|
||||||
|
l.attrNames blockAttrs;
|
||||||
|
|
||||||
|
expandedPaths = l.map (choice: prefix ++ [choice] ++ suffix) choices;
|
||||||
|
in
|
||||||
|
# recursively expand any remaining wildcards in each expanded path
|
||||||
|
l.concatMap expandPath expandedPaths;
|
||||||
|
in
|
||||||
|
expandWildcardAt 0;
|
||||||
|
|
||||||
|
# expand all paths that contain wildcards
|
||||||
|
expandedPaths =
|
||||||
|
if multiplePaths
|
||||||
|
then l.concatMap expandPath p
|
||||||
|
else expandPath p;
|
||||||
|
|
||||||
hoist = path: let
|
hoist = path: let
|
||||||
result =
|
result =
|
||||||
l.filterAttrs (
|
l.filterAttrs (
|
||||||
|
|
@ -11,7 +62,7 @@
|
||||||
t;
|
t;
|
||||||
in
|
in
|
||||||
if result == {}
|
if result == {}
|
||||||
then builtins.abort "[ren] filter: Path ${toString path} not found in any targets."
|
then null # return null for non-matching paths
|
||||||
else
|
else
|
||||||
l.mapAttrs (
|
l.mapAttrs (
|
||||||
_: v: let
|
_: v: let
|
||||||
|
|
@ -22,9 +73,13 @@
|
||||||
else l.filterAttrs pred attr
|
else l.filterAttrs pred attr
|
||||||
)
|
)
|
||||||
result;
|
result;
|
||||||
|
|
||||||
|
# process all expanded paths and merge results
|
||||||
|
results = l.map hoist expandedPaths;
|
||||||
|
validResults = l.filter (r: r != null) results;
|
||||||
in
|
in
|
||||||
if multiplePaths
|
if validResults == []
|
||||||
then l.foldl' l.recursiveUpdate {} (map (path: hoist path) p)
|
then builtins.abort "[ren] filter: No matching paths found for ${toString p}. Expanded to: ${toString expandedPaths}"
|
||||||
else hoist p;
|
else l.foldl' l.recursiveUpdate {} validResults;
|
||||||
in
|
in
|
||||||
filter
|
filter
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,7 @@
|
||||||
// {
|
// {
|
||||||
__ren = {
|
__ren = {
|
||||||
__schema = "v0";
|
__schema = "v0";
|
||||||
|
cells = builtins.attrNames (l.readDir cellsFrom);
|
||||||
init = l.listToAttrs res.init;
|
init = l.listToAttrs res.init;
|
||||||
actions = res.actions;
|
actions = res.actions;
|
||||||
cellsFrom = l.baseNameOf cellsFrom;
|
cellsFrom = l.baseNameOf cellsFrom;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue