Skip to content

Commit

Permalink
Merge pull request #1208 from HubSpot/construct-eager-macro-functions
Browse files Browse the repository at this point in the history
Construct EagerMacroFunction when aliasing macro function from {% from %} tag
  • Loading branch information
jasmith-hs authored Dec 13, 2024
2 parents 5a55cfb + 19cfb8a commit e94fa9a
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ public int hashCode() {
);
}

public MacroFunction cloneWithNewName(String name) {
return new MacroFunction(this, name);
}

private boolean alreadyDeferredInEarlierCall(
String key,
JinjavaInterpreter interpreter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public EagerMacroFunction(
);
}

EagerMacroFunction(MacroFunction source, String name) {
super(source, name);
}

public Object doEvaluate(
Map<String, Object> argMap,
Map<String, Object> kwargMap,
Expand Down Expand Up @@ -340,4 +344,9 @@ public boolean equals(Object o) {
public int hashCode() {
return super.hashCode();
}

@Override
public EagerMacroFunction cloneWithNewName(String name) {
return new EagerMacroFunction(this, name);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static boolean integrateChild(
if (val != null) {
MacroFunction toImport = (MacroFunction) val;
if (!importMapping.getKey().equals(importMapping.getValue())) {
toImport = new MacroFunction(toImport, importMapping.getValue());
toImport = toImport.cloneWithNewName(importMapping.getValue());
}
interpreter.getContext().addGlobalMacro(toImport);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.lib.expression.EagerExpressionStrategy;
import com.hubspot.jinjava.lib.fn.MacroFunction;
import com.hubspot.jinjava.lib.fn.eager.EagerMacroFunction;
import com.hubspot.jinjava.lib.tag.CallTag;
import com.hubspot.jinjava.lib.tag.FlexibleTag;
import com.hubspot.jinjava.tree.TagNode;
Expand Down Expand Up @@ -45,7 +46,7 @@ public String eagerInterpret(
LengthLimitingStringJoiner joiner;
try (InterpreterScopeClosable c = interpreter.enterNonStackingScope()) {
caller =
new MacroFunction(
new EagerMacroFunction(
tagNode.getChildren(),
"caller",
new LinkedHashMap<>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.lib.fn.MacroFunction;
import com.hubspot.jinjava.lib.fn.eager.EagerMacroFunction;
import com.hubspot.jinjava.lib.tag.DoTag;
import com.hubspot.jinjava.lib.tag.FromTag;
import com.hubspot.jinjava.loader.RelativePathResolver;
Expand Down Expand Up @@ -41,7 +42,7 @@ public String getEagerTagImage(TagToken tagToken, JinjavaInterpreter interpreter
imports
.values()
.forEach(value -> {
MacroFunction deferredMacro = new MacroFunction(
MacroFunction deferredMacro = new EagerMacroFunction(
null,
value,
null,
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/hubspot/jinjava/EagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1664,4 +1664,9 @@ public void itHandlesDeferredContinueInForLoop() {
// We don't support this yet
assertThat(interpreter.getContext().getDeferredNodes()).isNotEmpty();
}

@Test
public void itReconstructsFromedMacro() {
expectedTemplateInterpreter.assertExpectedOutput("reconstructs-fromed-macro/test");
}
}
4 changes: 3 additions & 1 deletion src/test/resources/eager/defers-caller/test.expected.jinja
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% for __ignored__ in [0] %}\
Jack says:
{% for __ignored__ in [0] %}\
How do I get a {{ deferred }}\
?{% endfor %}
?{% endfor %}\
{% endfor %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ Hello {{ myname }}\
{% endfor %}
{% set from_bar = bar %}\
{% enddo %}\
from_foo: Hello {{ myname }}
from_foo: {% for __ignored__ in [0] %}\
Hello {{ myname }}\
{% endfor %}
from_bar: {{ from_bar }}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{% set my_list = ['a', 'b'] %}\
{% set __macro_callerino_729568755_temp_variable_0__ %}\
{% set __macro_caller_172086791_temp_variable_0__ %}\
{% do my_list.append(deferred) %}\
{{ my_list }}\
{% endset %}\
{{ __macro_caller_172086791_temp_variable_0__ }}\
{% do my_list.append('d') %}\
{% endset %}\
{% call __macro_callerino_729568755_temp_variable_0__ %}\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro upper(param) %}
{{ param|upper }}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% set deferred_import_resource_path = 'eager/reconstructs-fromed-macro/has-macro.jinja' %}\
{% macro to_upper(param) %}
{{ filter:upper.filter(param, ____int3rpr3t3r____) }}
{% endmacro %}\
{% set deferred_import_resource_path = null %}\
{{ to_upper(deferred) }}
3 changes: 3 additions & 0 deletions src/test/resources/eager/reconstructs-fromed-macro/test.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% from './has-macro.jinja' import upper as to_upper %}

{{ to_upper(deferred) }}

0 comments on commit e94fa9a

Please sign in to comment.