From e212f2fc85f1e3826f57d5dd3eb48d2ddd019ff1 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Tue, 17 Sep 2024 15:58:37 -0400 Subject: [PATCH 1/3] Construct EagerMacroFunction from EagerCallTag and EagerFromTag --- .../java/com/hubspot/jinjava/lib/tag/eager/EagerCallTag.java | 3 ++- .../java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerCallTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerCallTag.java index 7e912d30c..128289fd5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerCallTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerCallTag.java @@ -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; @@ -45,7 +46,7 @@ public String eagerInterpret( LengthLimitingStringJoiner joiner; try (InterpreterScopeClosable c = interpreter.enterNonStackingScope()) { caller = - new MacroFunction( + new EagerMacroFunction( tagNode.getChildren(), "caller", new LinkedHashMap<>(), diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java index 937a4ca17..0b45a0a59 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java @@ -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; @@ -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, From 62bf49b7db6295f697443f6517fc378a856e119c Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Tue, 17 Sep 2024 18:05:27 -0400 Subject: [PATCH 2/3] Add cloneWithNewName to let successfully from-imported macros be imported as EagerMacroFunction to avoid ClassCastException --- .../java/com/hubspot/jinjava/lib/fn/MacroFunction.java | 4 ++++ .../hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java | 9 +++++++++ src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java | 2 +- src/test/java/com/hubspot/jinjava/EagerTest.java | 5 +++++ .../test.expected.jinja | 3 +++ .../eager/reconstructs-fromed-macro/has-macro.jinja | 3 +++ .../eager/reconstructs-fromed-macro/test.expected.jinja | 6 ++++++ .../resources/eager/reconstructs-fromed-macro/test.jinja | 3 +++ 8 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/eager/reconstructs-fromed-macro/has-macro.jinja create mode 100644 src/test/resources/eager/reconstructs-fromed-macro/test.expected.jinja create mode 100644 src/test/resources/eager/reconstructs-fromed-macro/test.jinja diff --git a/src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java b/src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java index 9e6cd9a17..89fbcaebf 100644 --- a/src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java +++ b/src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java @@ -202,6 +202,10 @@ public int hashCode() { ); } + public MacroFunction cloneWithNewName(String name) { + return new MacroFunction(this, name); + } + private boolean alreadyDeferredInEarlierCall( String key, JinjavaInterpreter interpreter diff --git a/src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java b/src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java index 9e805daa3..bea14deb2 100644 --- a/src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java +++ b/src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java @@ -58,6 +58,10 @@ public EagerMacroFunction( ); } + EagerMacroFunction(MacroFunction source, String name) { + super(source, name); + } + public Object doEvaluate( Map argMap, Map kwargMap, @@ -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); + } } diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java index ce00eceef..8bbb4df65 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java @@ -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 { diff --git a/src/test/java/com/hubspot/jinjava/EagerTest.java b/src/test/java/com/hubspot/jinjava/EagerTest.java index d62d359a2..713d15fca 100644 --- a/src/test/java/com/hubspot/jinjava/EagerTest.java +++ b/src/test/java/com/hubspot/jinjava/EagerTest.java @@ -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"); + } } diff --git a/src/test/resources/eager/handles-deferred-modification-in-caller/test.expected.jinja b/src/test/resources/eager/handles-deferred-modification-in-caller/test.expected.jinja index 7047aafb9..5933433f9 100644 --- a/src/test/resources/eager/handles-deferred-modification-in-caller/test.expected.jinja +++ b/src/test/resources/eager/handles-deferred-modification-in-caller/test.expected.jinja @@ -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__ %}\ diff --git a/src/test/resources/eager/reconstructs-fromed-macro/has-macro.jinja b/src/test/resources/eager/reconstructs-fromed-macro/has-macro.jinja new file mode 100644 index 000000000..f08325779 --- /dev/null +++ b/src/test/resources/eager/reconstructs-fromed-macro/has-macro.jinja @@ -0,0 +1,3 @@ +{% macro upper(param) %} + {{ param|upper }} +{% endmacro %} \ No newline at end of file diff --git a/src/test/resources/eager/reconstructs-fromed-macro/test.expected.jinja b/src/test/resources/eager/reconstructs-fromed-macro/test.expected.jinja new file mode 100644 index 000000000..c7213b008 --- /dev/null +++ b/src/test/resources/eager/reconstructs-fromed-macro/test.expected.jinja @@ -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) }} \ No newline at end of file diff --git a/src/test/resources/eager/reconstructs-fromed-macro/test.jinja b/src/test/resources/eager/reconstructs-fromed-macro/test.jinja new file mode 100644 index 000000000..6c934e61a --- /dev/null +++ b/src/test/resources/eager/reconstructs-fromed-macro/test.jinja @@ -0,0 +1,3 @@ +{% from './has-macro.jinja' import upper as to_upper %} + +{{ to_upper(deferred) }} \ No newline at end of file From 19cfb8a9024ea097962a79f308984f2d275973c2 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Fri, 13 Dec 2024 09:40:12 -0500 Subject: [PATCH 3/3] Update expected test results after merge from https://github.com/HubSpot/jinjava/pull/1205 --- src/test/resources/eager/defers-caller/test.expected.jinja | 4 +++- .../eager/handles-deferred-from-import-as/test.expected.jinja | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test/resources/eager/defers-caller/test.expected.jinja b/src/test/resources/eager/defers-caller/test.expected.jinja index 8063c5857..79657d9af 100644 --- a/src/test/resources/eager/defers-caller/test.expected.jinja +++ b/src/test/resources/eager/defers-caller/test.expected.jinja @@ -1,4 +1,6 @@ {% for __ignored__ in [0] %}\ Jack says: +{% for __ignored__ in [0] %}\ How do I get a {{ deferred }}\ -?{% endfor %} +?{% endfor %}\ +{% endfor %} diff --git a/src/test/resources/eager/handles-deferred-from-import-as/test.expected.jinja b/src/test/resources/eager/handles-deferred-from-import-as/test.expected.jinja index 8bca72eb8..f4b6093cd 100644 --- a/src/test/resources/eager/handles-deferred-from-import-as/test.expected.jinja +++ b/src/test/resources/eager/handles-deferred-from-import-as/test.expected.jinja @@ -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 }}