-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #920 from HubSpot/store-temporary-macro-vars
Store temporary macro function result vars
- Loading branch information
Showing
9 changed files
with
155 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/hubspot/jinjava/el/ext/eager/MacroFunctionTempVariable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.hubspot.jinjava.el.ext.eager; | ||
|
||
import com.hubspot.jinjava.objects.serialization.PyishBlockSetSerializable; | ||
import java.util.Objects; | ||
|
||
public class MacroFunctionTempVariable implements PyishBlockSetSerializable { | ||
private static final String CONTEXT_KEY_PREFIX = "__macro_%s_temp_variable_%d__"; | ||
private final String deferredResult; | ||
|
||
public MacroFunctionTempVariable(String deferredResult) { | ||
this.deferredResult = deferredResult; | ||
} | ||
|
||
public static String getVarName(String macroFunctionName, int callCount) { | ||
return String.format(CONTEXT_KEY_PREFIX, macroFunctionName, callCount); | ||
} | ||
|
||
@Override | ||
public String getBlockSetBody() { | ||
return deferredResult; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
MacroFunctionTempVariable that = (MacroFunctionTempVariable) o; | ||
return deferredResult.equals(that.deferredResult); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(deferredResult); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/com/hubspot/jinjava/objects/serialization/PyishBlockSetSerializable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.hubspot.jinjava.objects.serialization; | ||
|
||
public interface PyishBlockSetSerializable { | ||
String getBlockSetBody(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{% macro big_guy() %} | ||
{% set __macro_big_guy_temp_variable_0__ %} | ||
{% if deferred %}I am foo{% else %}I am bar{% endif %} | ||
{% endmacro %}{% print big_guy() %} | ||
{% macro big_guy() %} | ||
{% endset %}{% print __macro_big_guy_temp_variable_0__ %} | ||
{% set __macro_big_guy_temp_variable_1__ %} | ||
{% if deferred %}No more foo{% else %}I am bar{% endif %} | ||
{% endmacro %}{% print big_guy() %} | ||
{% endset %}{% print __macro_big_guy_temp_variable_1__ %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
src/test/resources/eager/handles-deferred-for-loop-var-from-macro.expected.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
{% macro getData() %} | ||
{% set __macro_getData_temp_variable_0__ %} | ||
|
||
|
||
{% for __ignored__ in [0] %} | ||
{% macro doIt(val) %} | ||
{{ deferred ~ filter:tojson.filter(val, ____int3rpr3t3r____) }} | ||
{% endmacro %}{% set val = {'a': 'a'} %}{{ filter:upper.filter(doIt(val), ____int3rpr3t3r____) }} | ||
{% set __macro_doIt_temp_variable_0__ %} | ||
{{ deferred ~ '{\"a\":\"a\"}' }} | ||
{% endset %}{{ filter:upper.filter(__macro_doIt_temp_variable_0__, ____int3rpr3t3r____) }} | ||
|
||
{% macro doIt(val) %} | ||
{{ deferred ~ filter:tojson.filter(val, ____int3rpr3t3r____) }} | ||
{% endmacro %}{% set val = {'b': 'b'} %}{{ filter:upper.filter(doIt(val), ____int3rpr3t3r____) }} | ||
{% set __macro_doIt_temp_variable_1__ %} | ||
{{ deferred ~ '{\"b\":\"b\"}' }} | ||
{% endset %}{{ filter:upper.filter(__macro_doIt_temp_variable_1__, ____int3rpr3t3r____) }} | ||
{% endfor %} | ||
{% endmacro %}{{ filter:upper.filter(getData(), ____int3rpr3t3r____) }} | ||
{% endset %}{{ filter:upper.filter(__macro_getData_temp_variable_0__, ____int3rpr3t3r____) }} |
2 changes: 1 addition & 1 deletion
2
src/test/resources/eager/puts-deferred-fromed-macro-in-output.expected.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{% set myname = deferred + 3 %}{% set deferred_import_resource_path = 'simple-with-call.jinja' %}{% macro getPath() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{% print getPath() %} | ||
{% set myname = deferred + 3 %}{% set __macro_getPath_temp_variable_1__ %}Hello {{ myname }}{% endset %}{% print __macro_getPath_temp_variable_1__ %} |