Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discussion] Handling variables in deferred scopes #421

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/main/java/com/hubspot/jinjava/interpret/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.SetMultimap;
import com.hubspot.jinjava.lib.Importable;
import com.hubspot.jinjava.lib.exptest.ExpTest;
Expand Down Expand Up @@ -269,9 +270,21 @@ public void addResolvedFunction(String function) {
}

public void addDeferredNode(Node node) {
addDeferredNodeAndContext(node, 1);
}

//depth is "height" back up the context tree here
private void addDeferredNodeAndContext(Node node, int currentDepth) {
deferredNodes.add(node);

if (getParent() != null && currentDepth++ == 0) {
Map<String, Object> localDifference = Maps
.difference(getParent(), this)
.entriesOnlyOnRight();
getParent().putAll(localDifference);
}
if (getParent() != null) {
getParent().addDeferredNode(node);
getParent().addDeferredNodeAndContext(node, currentDepth);
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/test/java/com/hubspot/jinjava/interpret/DeferredTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.base.Joiner;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.random.RandomNumberGeneratorStrategy;
Expand Down Expand Up @@ -183,4 +184,49 @@ public void itDefersMacro() {
String output = interpreter.render(deferredOutput);
assertThat(output).isEqualTo("0,10,15,25");
}

@Test
public void itPreservesVariablesUsedInDeferredBlock() {
String template = "";
template += "{% for item in resolved %}";
template += "{% set varUsedInForScope = 'outside if statement' %}";
template += " {% if deferred %}";
template += " {{ varUsedInForScope }}";
template += " {% set varUsedInForScope = 'entered if statement' %}";
template += " {% endif %}";
template += " {{ varUsedInForScope }}";
template += "{% endfor %}";

String desiredDeferredOutput =
" {% if deferred %} {{ varUsedInForScope }} {% set varUsedInForScope = 'entered if statement' %} {% endif %} {{ varUsedInForScope }}";
String output = interpreter.render(template);
//No Scope Copying:
//Interpreter Scope after render:
// deferred=com.hubspot.jinjava.interpret.DeferredValue@1af2d44a
// resolved=resolvedValue
//Note that varUsedInForScope is lost
//Output 1st render: {% if deferred %} {{ varUsedInForScope }} {% set varUsedInForScope = 'entered if statement' %} {% endif %} outside if statement
//Output 2nd render: outside if statement
//Note the missing first print statement from inside the deferred statement. The printed statement is simply preserved from the 1st render and should have the value "entered if statement"

//With Scope Copying:
// resolved=resolvedValue
// loop=com.hubspot.jinjava.util.ForLoop@c88a337
// varUsedInForScope=outside if statement
// item=resolvedValue
// deferred=com.hubspot.jinjava.interpret.DeferredValue@5d0a1059
//Output 1st render: {% if deferred %} {{ varUsedInForScope }} {% set varUsedInForScope = 'entered if statement' %} {% endif %} outside if statement
//Output 2nd render: outside if statement outside if statement
//This result is more functional as it allows the var in the if block to be evaluated but it does not have the expected new value for varUsedInForScope

Joiner.MapJoiner mapJoiner = Joiner.on(",").withKeyValueSeparator("=");

System.out.println(mapJoiner.join(interpreter.getContext()));
//assertThat(output).isEqualTo(desiredDeferredOutput);

interpreter.getContext().put("deferred", "resolved");
String outputWithNoDeferredValues = interpreter.render(output);
String expectedOutputWithNoDeferred = "outside if statement entered if statement";
assertThat(outputWithNoDeferredValues).isEqualTo(expectedOutputWithNoDeferred);
}
}