Skip to content

Commit 0eb7b0c

Browse files
committed
Add new key that's used to handle deferred imported macros
1 parent 57a937e commit 0eb7b0c

File tree

7 files changed

+42
-104
lines changed

7 files changed

+42
-104
lines changed

src/main/java/com/hubspot/jinjava/interpret/Context.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
public class Context extends ScopeMap<String, Object> {
5252
public static final String GLOBAL_MACROS_SCOPE_KEY = "__macros__";
5353
public static final String IMPORT_RESOURCE_PATH_KEY = "import_resource_path";
54+
public static final String DEFERRED_IMPORT_RESOURCE_PATH_KEY =
55+
"deferred_import_resource_path";
56+
5457
public static final String IMPORT_RESOURCE_ALIAS_KEY = "import_resource_alias";
5558

5659
private SetMultimap<String, String> dependencies = HashMultimap.create();

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

Lines changed: 27 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
import com.hubspot.jinjava.lib.fn.MacroFunction;
1111
import com.hubspot.jinjava.lib.tag.MacroTag;
1212
import com.hubspot.jinjava.lib.tag.eager.EagerTagDecorator;
13-
import com.hubspot.jinjava.objects.collections.PyMap;
1413
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
15-
import java.util.HashMap;
1614
import java.util.LinkedHashMap;
1715
import java.util.List;
1816
import java.util.Map;
@@ -110,39 +108,33 @@ public String reconstructImage() {
110108
Optional<String> importFile = macroFunction.getImportFile(interpreter);
111109
if (importFile.isPresent()) {
112110
interpreter.getContext().getCurrentPathStack().pop();
113-
if (fullName.indexOf('.') >= 0) {
114-
prefix = getAliasedImportResourcePrefix(importFile.get());
115-
} else {
116-
prefix =
117-
EagerTagDecorator.buildSetTagForDeferredInChildContext(
118-
ImmutableMap.of(
119-
Context.IMPORT_RESOURCE_PATH_KEY,
120-
interpreter
121-
.getContext()
122-
.getPyishObjectMapper()
123-
.getAsPyishString(importFile.get())
124-
),
125-
interpreter,
126-
false
127-
);
128-
String currentImportResource = interpreter
129-
.getContext()
130-
.getCurrentPathStack()
131-
.peek()
132-
.orElse("");
133-
suffix =
134-
EagerTagDecorator.buildSetTagForDeferredInChildContext(
135-
ImmutableMap.of(
136-
Context.IMPORT_RESOURCE_PATH_KEY,
137-
interpreter
138-
.getContext()
139-
.getPyishObjectMapper()
140-
.getAsPyishString(currentImportResource)
141-
),
142-
interpreter,
143-
false
144-
);
145-
}
111+
String currentDeferredImportResource = (String) interpreter
112+
.getContext()
113+
.get(Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY);
114+
prefix =
115+
EagerTagDecorator.buildSetTagForDeferredInChildContext(
116+
ImmutableMap.of(
117+
Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY,
118+
interpreter
119+
.getContext()
120+
.getPyishObjectMapper()
121+
.getAsPyishString(importFile.get())
122+
),
123+
interpreter,
124+
false
125+
);
126+
suffix =
127+
EagerTagDecorator.buildSetTagForDeferredInChildContext(
128+
ImmutableMap.of(
129+
Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY,
130+
interpreter
131+
.getContext()
132+
.getPyishObjectMapper()
133+
.getAsPyishString(currentDeferredImportResource)
134+
),
135+
interpreter,
136+
false
137+
);
146138
}
147139

148140
String result;
@@ -161,39 +153,4 @@ public String reconstructImage() {
161153
}
162154
return prefix + result + suffix;
163155
}
164-
165-
private String getAliasedImportResourcePrefix(String importResourcePath) {
166-
String prefix = "";
167-
String importAlias = fullName.split("\\.", 2)[0];
168-
Object aliasMap = interpreter.getContext().get(importAlias);
169-
if (aliasMap instanceof DeferredValue) {
170-
PyMap deferredAliasMap = new PyMap(new HashMap<>());
171-
deferredAliasMap.put(Context.IMPORT_RESOURCE_PATH_KEY, importResourcePath);
172-
prefix =
173-
EagerTagDecorator.buildDoUpdateTag(
174-
importAlias,
175-
interpreter
176-
.getContext()
177-
.getPyishObjectMapper()
178-
.getAsPyishString(deferredAliasMap),
179-
interpreter
180-
);
181-
} else if (aliasMap instanceof Map) {
182-
PyMap deferredAliasMap = new PyMap(new HashMap<>());
183-
deferredAliasMap.put(Context.IMPORT_RESOURCE_PATH_KEY, importResourcePath);
184-
prefix =
185-
EagerTagDecorator.buildSetTagForDeferredInChildContext(
186-
ImmutableMap.of(
187-
importAlias,
188-
interpreter
189-
.getContext()
190-
.getPyishObjectMapper()
191-
.getAsPyishString(deferredAliasMap)
192-
),
193-
interpreter,
194-
false
195-
);
196-
}
197-
return prefix;
198-
}
199156
}

src/main/java/com/hubspot/jinjava/lib/tag/MacroTag.java

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
1515
import com.hubspot.jinjava.lib.fn.MacroFunction;
1616
import com.hubspot.jinjava.tree.TagNode;
17-
import java.util.Collections;
1817
import java.util.HashMap;
1918
import java.util.LinkedHashMap;
2019
import java.util.List;
@@ -123,34 +122,16 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
123122
MacroFunction macro;
124123
String contextImportResourcePath = (String) interpreter
125124
.getContext()
126-
.get(Context.IMPORT_RESOURCE_PATH_KEY, "");
127-
String stackImportResourcePath = interpreter
128-
.getContext()
129-
.getCurrentPathStack()
130-
.peek()
131-
.orElse("");
125+
.get(Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY, "");
126+
boolean scopeEntered = false;
132127
try {
133-
if (!contextImportResourcePath.equals(stackImportResourcePath)) {
128+
if (StringUtils.isNotEmpty(contextImportResourcePath)) {
129+
scopeEntered = true;
134130
interpreter.enterScope();
135131
interpreter
136132
.getContext()
137133
.put(Context.IMPORT_RESOURCE_PATH_KEY, contextImportResourcePath);
138134
}
139-
if (StringUtils.isNotEmpty(parentName)) {
140-
interpreter.enterScope();
141-
Object parentAliasMap = interpreter
142-
.getContext()
143-
.get(parentName, Collections.emptyMap());
144-
if (parentAliasMap instanceof DeferredValue) {
145-
parentAliasMap = ((DeferredValue) parentAliasMap).getOriginalValue();
146-
}
147-
String parentImportResourcePath = (String) (
148-
(Map<String, Object>) parentAliasMap
149-
).get(Context.IMPORT_RESOURCE_PATH_KEY);
150-
interpreter
151-
.getContext()
152-
.put(Context.IMPORT_RESOURCE_PATH_KEY, parentImportResourcePath);
153-
}
154135
macro =
155136
new MacroFunction(
156137
tagNode.getChildren(),
@@ -162,10 +143,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
162143
interpreter.getPosition()
163144
);
164145
} finally {
165-
if (
166-
StringUtils.isNotEmpty(parentName) ||
167-
!contextImportResourcePath.equals(stackImportResourcePath)
168-
) {
146+
if (scopeEntered) {
169147
interpreter.leaveScope();
170148
}
171149
}

src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,10 @@ public void itCorrectlySetsAliasedPathForSecondPass() {
470470
);
471471
assertThat(firstPassResult)
472472
.isEqualTo(
473-
"{% set m = {'import_resource_path': 'import-macro.jinja'} %}{% macro m.print_path_macro(var) %}\n" +
473+
"{% set deferred_import_resource_path = 'import-macro.jinja' %}{% macro m.print_path_macro(var) %}\n" +
474474
"{{ var|print_path }}\n" +
475475
"{{ var }}\n" +
476-
"{% endmacro %}{{ m.print_path_macro(foo) }}"
476+
"{% endmacro %}{% set deferred_import_resource_path = null %}{{ m.print_path_macro(foo) }}"
477477
);
478478
context.put("foo", "foo");
479479
assertThat(interpreter.render(firstPassResult).trim())
@@ -489,10 +489,10 @@ public void itCorrectlySetsPathForSecondPass() {
489489
);
490490
assertThat(firstPassResult)
491491
.isEqualTo(
492-
"{% set import_resource_path = 'import-macro.jinja' %}{% macro print_path_macro(var) %}\n" +
492+
"{% set deferred_import_resource_path = 'import-macro.jinja' %}{% macro print_path_macro(var) %}\n" +
493493
"{{ var|print_path }}\n" +
494494
"{{ var }}\n" +
495-
"{% endmacro %}{% set import_resource_path = '' %}{{ print_path_macro(foo) }}"
495+
"{% endmacro %}{% set deferred_import_resource_path = null %}{{ print_path_macro(foo) }}"
496496
);
497497
context.put("foo", "foo");
498498
assertThat(interpreter.render(firstPassResult).trim())

src/test/resources/eager/handles-deferred-import-vars.expected.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ bar: {{ bar }}
88
{% set bar = myname + 19 %}{% set simple = {} %}{% do simple.update({'bar': bar}) %}
99
Hello {{ myname }}
1010
{% do simple.update({'import_resource_path': 'macro-and-set.jinja'}) %}
11-
simple.foo: {% do simple.update({'import_resource_path': 'macro-and-set.jinja'}) %}{% macro simple.foo() %}Hello {{ myname }}{% endmacro %}{{ simple.foo() }}
11+
simple.foo: {% set deferred_import_resource_path = 'macro-and-set.jinja' %}{% macro simple.foo() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{{ simple.foo() }}
1212
simple.bar: {{ simple.bar }}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
{% set myname = deferred + 3 %}
2-
{% set import_resource_path = 'simple-with-call.jinja' %}{% macro getPath() %}Hello {{ myname }}{% endmacro %}{% set import_resource_path = '' %}{% print getPath() %}
2+
{% set deferred_import_resource_path = 'simple-with-call.jinja' %}{% macro getPath() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{% print getPath() %}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
{% set myname = deferred + 3 %}
2-
{% set simple = {'import_resource_path': 'simple-with-call.jinja'} %}{% macro simple.getPath() %}Hello {{ myname }}{% endmacro %}{% print simple.getPath() %}
2+
{% set deferred_import_resource_path = 'simple-with-call.jinja' %}{% macro simple.getPath() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{% print simple.getPath() %}

0 commit comments

Comments
 (0)