From 95ce249a088c29208f32cadab6609bf5cd4a0a51 Mon Sep 17 00:00:00 2001 From: Raphael Schweikert Date: Mon, 28 Oct 2024 21:49:58 +0100 Subject: [PATCH] feat(runner): add index var to `each` hop --- .../sections/editor/types/EachStep.tsx | 9 ++++++- .../swisscom/aem/tools/impl/hops/Each.java | 24 +++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/main/frontend/sections/editor/types/EachStep.tsx b/src/main/frontend/sections/editor/types/EachStep.tsx index 4ca1b85..2a559a8 100644 --- a/src/main/frontend/sections/editor/types/EachStep.tsx +++ b/src/main/frontend/sections/editor/types/EachStep.tsx @@ -36,7 +36,14 @@ export const EachStep = forwardRefJEXL Expression

The sub-pipeline given will be repeated for every element this expression produces.

Iterator
-

This is the name of the variable used to address the individual item.

+

+ This is the name of the variable used to address the individual item. +
+ The item index will be available as{' '} + + ${'{'}iterator{'}'}_index + +

); diff --git a/src/main/java/com/swisscom/aem/tools/impl/hops/Each.java b/src/main/java/com/swisscom/aem/tools/impl/hops/Each.java index e5f31ce..1cc9744 100644 --- a/src/main/java/com/swisscom/aem/tools/impl/hops/Each.java +++ b/src/main/java/com/swisscom/aem/tools/impl/hops/Each.java @@ -6,8 +6,10 @@ import com.swisscom.aem.tools.jcrhopper.context.HopContext; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import javax.annotation.Nonnull; import javax.jcr.Node; import javax.jcr.RepositoryException; @@ -25,25 +27,30 @@ public class Each implements Hop { @Override public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException { final Object items = context.evaluate(config.expression); + int index = 0; if (items instanceof Iterable) { for (Object item : (Iterable) items) { - runWith(config, item, node, context); + runWith(config, item, index++, node, context); } } else if (items instanceof Iterator) { while (((Iterator) items).hasNext()) { - runWith(config, ((Iterator) items).next(), node, context); + runWith(config, ((Iterator) items).next(), index++, node, context); } } else if (items.getClass().isArray()) { for (Object item : (Object[]) items) { - runWith(config, item, node, context); + runWith(config, item, index++, node, context); } } else { - runWith(config, items, node, context); + runWith(config, items, index, node, context); } } - @SuppressFBWarnings(value = "ITC_INHERITANCE_TYPE_CHECKING", justification = "The item comes from scripting and can be an arbitrary type") - private void runWith(Config config, Object item, Node initialNode, HopContext context) throws HopperException, RepositoryException { + @SuppressFBWarnings( + value = { "ITC_INHERITANCE_TYPE_CHECKING", "STT_TOSTRING_MAP_KEYING" }, + justification = "The item comes from scripting and can be an arbitrary type. Dynamic Lookup Required." + ) + private void runWith(Config config, Object item, int index, Node initialNode, HopContext context) + throws HopperException, RepositoryException { Node node = initialNode; if (config.assumeNodes) { if (item instanceof Node) { @@ -59,7 +66,10 @@ private void runWith(Config config, Object item, Node initialNode, HopContext co } else { context.debug("Iterating non-node value {} accessible as {}", item, config.iterator); } - context.runHops(node, config.hops, Collections.singletonMap(config.iterator, item)); + final Map vars = new HashMap<>(); + vars.put(config.iterator, item); + vars.put(config.iterator + "_index", index); + context.runHops(node, config.hops, vars); } @Nonnull