Skip to content

Commit 15a775c

Browse files
committed
Add new DeferredInvocationResolutionException which extends DeferredParsingException
This is used in an EagerMacroFunction which will convert its output into a temporary variable so that the macro function's call will be replaced with that variable name. This same process should be usable within custom defined functions as well so this converts that functionality into a new type of exception which will allow custom functions to define what the partial resolution of their invocation is. Additionally, this check wasn't applied to EagerAstMethod, so aliased macro functions weren't being converted to temporary macro variables as they should have been.
1 parent d7ba71d commit 15a775c

5 files changed

Lines changed: 17 additions & 8 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.hubspot.jinjava.el.ext;
2+
3+
public class DeferredInvocationResolutionException extends DeferredParsingException {
4+
5+
public DeferredInvocationResolutionException(String invocationResultString) {
6+
super(DeferredInvocationResolutionException.class, invocationResultString);
7+
}
8+
}

src/main/java/com/hubspot/jinjava/el/ext/eager/EagerAstMacroFunction.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.hubspot.jinjava.el.ext.eager;
22

33
import com.hubspot.jinjava.el.ext.AstMacroFunction;
4+
import com.hubspot.jinjava.el.ext.DeferredInvocationResolutionException;
45
import com.hubspot.jinjava.el.ext.DeferredParsingException;
56
import com.hubspot.jinjava.el.ext.ExtendedParser;
67
import com.hubspot.jinjava.el.ext.IdentifierPreservationStrategy;
78
import com.hubspot.jinjava.interpret.Context.TemporaryValueClosable;
89
import com.hubspot.jinjava.interpret.DeferredValueException;
910
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
10-
import com.hubspot.jinjava.lib.fn.MacroFunction;
1111
import de.odysseus.el.tree.Bindings;
1212
import de.odysseus.el.tree.impl.ast.AstParameters;
1313
import java.lang.reflect.Array;
@@ -154,10 +154,7 @@ public String getPartiallyResolved(
154154
DeferredParsingException deferredParsingException,
155155
IdentifierPreservationStrategy identifierPreservationStrategy
156156
) {
157-
if (
158-
deferredParsingException != null &&
159-
deferredParsingException.getSourceNode() instanceof MacroFunction
160-
) {
157+
if (deferredParsingException instanceof DeferredInvocationResolutionException) {
161158
return deferredParsingException.getDeferredEvalResult();
162159
}
163160
String paramString;

src/main/java/com/hubspot/jinjava/el/ext/eager/EagerAstMethod.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.hubspot.jinjava.el.ext.eager;
22

3+
import com.hubspot.jinjava.el.ext.DeferredInvocationResolutionException;
34
import com.hubspot.jinjava.el.ext.DeferredParsingException;
45
import com.hubspot.jinjava.el.ext.IdentifierPreservationStrategy;
56
import com.hubspot.jinjava.interpret.DeferredValueException;
@@ -80,6 +81,9 @@ public String getPartiallyResolved(
8081
DeferredParsingException deferredParsingException,
8182
IdentifierPreservationStrategy identifierPreservationStrategy
8283
) {
84+
if (deferredParsingException instanceof DeferredInvocationResolutionException) {
85+
return deferredParsingException.getDeferredEvalResult();
86+
}
8387
String propertyResult;
8488
propertyResult =
8589
(property).getPartiallyResolved(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.google.common.annotations.Beta;
44
import com.hubspot.jinjava.el.ext.AstMacroFunction;
5-
import com.hubspot.jinjava.el.ext.DeferredParsingException;
5+
import com.hubspot.jinjava.el.ext.DeferredInvocationResolutionException;
66
import com.hubspot.jinjava.el.ext.eager.MacroFunctionTempVariable;
77
import com.hubspot.jinjava.interpret.Context;
88
import com.hubspot.jinjava.interpret.DeferredMacroValueImpl;
@@ -122,7 +122,7 @@ public Object doEvaluate(
122122
prefixToPreserveState + eagerExecutionResult.asTemplateString()
123123
)
124124
);
125-
throw new DeferredParsingException(this, tempVarName);
125+
throw new DeferredInvocationResolutionException(tempVarName);
126126
}
127127
return eagerExecutionResult.getResult().toString(true);
128128
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{% set myname = deferred + 3 %}{% set deferred_import_resource_path = 'simple-with-call.jinja' %}{% macro simple.getPath() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{% print simple.getPath() %}
1+
{% set myname = deferred + 3 %}{% set __macro_getPath_331491059_temp_variable_1__ %}Hello {{ myname }}{% endset %}{% print __macro_getPath_331491059_temp_variable_1__ %}

0 commit comments

Comments
 (0)