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

Construct EagerMacroFunction when aliasing macro function from {% from %} tag #1208

Merged
merged 4 commits into from
Dec 13, 2024
Merged
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
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) }}
Loading