Skip to content

Commit e94fa9a

Browse files
authored
Merge pull request #1208 from HubSpot/construct-eager-macro-functions
Construct EagerMacroFunction when aliasing macro function from {% from %} tag
2 parents 5a55cfb + 19cfb8a commit e94fa9a

12 files changed

Lines changed: 44 additions & 5 deletions

File tree

src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ public int hashCode() {
207207
);
208208
}
209209

210+
public MacroFunction cloneWithNewName(String name) {
211+
return new MacroFunction(this, name);
212+
}
213+
210214
private boolean alreadyDeferredInEarlierCall(
211215
String key,
212216
JinjavaInterpreter interpreter

src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public EagerMacroFunction(
5858
);
5959
}
6060

61+
EagerMacroFunction(MacroFunction source, String name) {
62+
super(source, name);
63+
}
64+
6165
public Object doEvaluate(
6266
Map<String, Object> argMap,
6367
Map<String, Object> kwargMap,
@@ -340,4 +344,9 @@ public boolean equals(Object o) {
340344
public int hashCode() {
341345
return super.hashCode();
342346
}
347+
348+
@Override
349+
public EagerMacroFunction cloneWithNewName(String name) {
350+
return new EagerMacroFunction(this, name);
351+
}
343352
}

src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static boolean integrateChild(
170170
if (val != null) {
171171
MacroFunction toImport = (MacroFunction) val;
172172
if (!importMapping.getKey().equals(importMapping.getValue())) {
173-
toImport = new MacroFunction(toImport, importMapping.getValue());
173+
toImport = toImport.cloneWithNewName(importMapping.getValue());
174174
}
175175
interpreter.getContext().addGlobalMacro(toImport);
176176
} else {

src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerCallTag.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
88
import com.hubspot.jinjava.lib.expression.EagerExpressionStrategy;
99
import com.hubspot.jinjava.lib.fn.MacroFunction;
10+
import com.hubspot.jinjava.lib.fn.eager.EagerMacroFunction;
1011
import com.hubspot.jinjava.lib.tag.CallTag;
1112
import com.hubspot.jinjava.lib.tag.FlexibleTag;
1213
import com.hubspot.jinjava.tree.TagNode;
@@ -45,7 +46,7 @@ public String eagerInterpret(
4546
LengthLimitingStringJoiner joiner;
4647
try (InterpreterScopeClosable c = interpreter.enterNonStackingScope()) {
4748
caller =
48-
new MacroFunction(
49+
new EagerMacroFunction(
4950
tagNode.getChildren(),
5051
"caller",
5152
new LinkedHashMap<>(),

src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.hubspot.jinjava.interpret.InterpretException;
77
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
88
import com.hubspot.jinjava.lib.fn.MacroFunction;
9+
import com.hubspot.jinjava.lib.fn.eager.EagerMacroFunction;
910
import com.hubspot.jinjava.lib.tag.DoTag;
1011
import com.hubspot.jinjava.lib.tag.FromTag;
1112
import com.hubspot.jinjava.loader.RelativePathResolver;
@@ -41,7 +42,7 @@ public String getEagerTagImage(TagToken tagToken, JinjavaInterpreter interpreter
4142
imports
4243
.values()
4344
.forEach(value -> {
44-
MacroFunction deferredMacro = new MacroFunction(
45+
MacroFunction deferredMacro = new EagerMacroFunction(
4546
null,
4647
value,
4748
null,

src/test/java/com/hubspot/jinjava/EagerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,4 +1664,9 @@ public void itHandlesDeferredContinueInForLoop() {
16641664
// We don't support this yet
16651665
assertThat(interpreter.getContext().getDeferredNodes()).isNotEmpty();
16661666
}
1667+
1668+
@Test
1669+
public void itReconstructsFromedMacro() {
1670+
expectedTemplateInterpreter.assertExpectedOutput("reconstructs-fromed-macro/test");
1671+
}
16671672
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{% for __ignored__ in [0] %}\
22
Jack says:
3+
{% for __ignored__ in [0] %}\
34
How do I get a {{ deferred }}\
4-
?{% endfor %}
5+
?{% endfor %}\
6+
{% endfor %}

src/test/resources/eager/handles-deferred-from-import-as/test.expected.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ Hello {{ myname }}\
66
{% endfor %}
77
{% set from_bar = bar %}\
88
{% enddo %}\
9-
from_foo: Hello {{ myname }}
9+
from_foo: {% for __ignored__ in [0] %}\
10+
Hello {{ myname }}\
11+
{% endfor %}
1012
from_bar: {{ from_bar }}

src/test/resources/eager/handles-deferred-modification-in-caller/test.expected.jinja

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{% set my_list = ['a', 'b'] %}\
22
{% set __macro_callerino_729568755_temp_variable_0__ %}\
3+
{% set __macro_caller_172086791_temp_variable_0__ %}\
34
{% do my_list.append(deferred) %}\
45
{{ my_list }}\
6+
{% endset %}\
7+
{{ __macro_caller_172086791_temp_variable_0__ }}\
58
{% do my_list.append('d') %}\
69
{% endset %}\
710
{% call __macro_callerino_729568755_temp_variable_0__ %}\
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% macro upper(param) %}
2+
{{ param|upper }}
3+
{% endmacro %}

0 commit comments

Comments
 (0)