Skip to content

Commit

Permalink
Add cloneWithNewName to let successfully from-imported macros be
Browse files Browse the repository at this point in the history
imported as EagerMacroFunction to avoid ClassCastException
  • Loading branch information
jasmith-hs committed Sep 17, 2024
1 parent e212f2f commit 7aa5983
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 1 deletion.
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 @@ -202,6 +202,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 @@ -334,4 +338,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
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 @@ -1634,4 +1634,9 @@ public void prepareContext(Context context) {
"keeps-meta-context-variables-through-import/test"
);
}

@Test
public void itReconstructsFromedMacro() {
expectedTemplateInterpreter.assertExpectedOutput("reconstructs-fromed-macro/test");
}
}
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,3 @@
{% from './has-macro.jinja' import upper as to_upper %}

{{ 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 7aa5983

Please sign in to comment.