Skip to content

Commit

Permalink
feat(runner): add index var to each hop
Browse files Browse the repository at this point in the history
  • Loading branch information
sabberworm committed Oct 28, 2024
1 parent 8e6a8f2 commit 95ce249
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/main/frontend/sections/editor/types/EachStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ export const EachStep = forwardRef<HTMLDivElement, { parentHops: Hop[]; hop: Typ
<h5>JEXL Expression</h5>
<p>The sub-pipeline given will be repeated for every element this expression produces.</p>
<h5>Iterator</h5>
<p>This is the name of the variable used to address the individual item.</p>
<p>
This is the name of the variable used to address the individual item.
<br />
The item index will be available as{' '}
<code>
${'{'}iterator{'}'}_index
</code>
</p>
</Help>
</StepEditor>
);
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/com/swisscom/aem/tools/impl/hops/Each.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,25 +27,30 @@ public class Each implements Hop<Each.Config> {
@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) {
Expand All @@ -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<String, Object> vars = new HashMap<>();
vars.put(config.iterator, item);
vars.put(config.iterator + "_index", index);
context.runHops(node, config.hops, vars);
}

@Nonnull
Expand Down

0 comments on commit 95ce249

Please sign in to comment.