Skip to content

Commit

Permalink
Merge pull request #1222 from HubSpot/deferred-execution-loop-extensions
Browse files Browse the repository at this point in the history
Defer BreakTag and ContinueTag if they're used in deferredExecutionMode
  • Loading branch information
jasmith-hs authored Dec 6, 2024
2 parents 1996571 + 9cf4ad6 commit 7c3bfc4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/hubspot/jinjava/lib/tag/BreakTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaTextMateSnippet;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.NotInLoopException;
import com.hubspot.jinjava.tree.TagNode;
Expand Down Expand Up @@ -31,6 +32,9 @@ public String getName() {
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
Object loop = interpreter.getContext().get(ForTag.LOOP);
if (loop instanceof ForLoop) {
if (interpreter.getContext().isDeferredExecutionMode()) {
throw new DeferredValueException("Deferred break");
}
ForLoop forLoop = (ForLoop) loop;
forLoop.doBreak();
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/hubspot/jinjava/lib/tag/ContinueTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaTextMateSnippet;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.NotInLoopException;
import com.hubspot.jinjava.tree.TagNode;
Expand Down Expand Up @@ -29,6 +30,9 @@ public String getName() {
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
Object loop = interpreter.getContext().get(ForTag.LOOP);
if (loop instanceof ForLoop) {
if (interpreter.getContext().isDeferredExecutionMode()) {
throw new DeferredValueException("Deferred continue");
}
ForLoop forLoop = (ForLoop) loop;
forLoop.doContinue();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.hubspot.jinjava.lib.tag.BlockTag;
import com.hubspot.jinjava.lib.tag.BreakTag;
import com.hubspot.jinjava.lib.tag.CallTag;
import com.hubspot.jinjava.lib.tag.ContinueTag;
import com.hubspot.jinjava.lib.tag.CycleTag;
import com.hubspot.jinjava.lib.tag.DoTag;
import com.hubspot.jinjava.lib.tag.ElseIfTag;
Expand Down Expand Up @@ -54,6 +56,8 @@ public class EagerTagFactory {
.add(ElseTag.class)
.add(RawTag.class)
.add(ExtendsTag.class) // TODO support reconstructing extends tags
.add(BreakTag.class) // TODO support eager break tag
.add(ContinueTag.class) // TODO support eager continue tag
.build();

@SuppressWarnings("unchecked")
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/hubspot/jinjava/EagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1644,4 +1644,24 @@ public void itWrapsMacroThatWouldChangeCurrentPathInChildScope() {
"wraps-macro-that-would-change-current-path-in-child-scope/test"
);
}

@Test
public void itHandlesDeferredBreakInForLoop() {
String input = expectedTemplateInterpreter.getFixtureTemplate(
"handles-deferred-break-in-for-loop/test"
);
interpreter.render(input);
// We don't support this yet
assertThat(interpreter.getContext().getDeferredNodes()).isNotEmpty();
}

@Test
public void itHandlesDeferredContinueInForLoop() {
String input = expectedTemplateInterpreter.getFixtureTemplate(
"handles-deferred-continue-in-for-loop/test"
);
interpreter.render(input);
// We don't support this yet
assertThat(interpreter.getContext().getDeferredNodes()).isNotEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Start loop
{% for i in range(5) %}
{% if deferred %}
{% break %}
{% endif %}
i is: {{ i }}
{% endfor %}
End loop
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Start loop
{% for i in range(5) %}
{% if deferred %}
{% continue %}
{% endif %}
i is: {{ i }}
{% endfor %}
End loop

0 comments on commit 7c3bfc4

Please sign in to comment.