|
1 | 1 | package com.hubspot.jinjava.lib.fn.eager; |
2 | 2 |
|
| 3 | +import static com.hubspot.jinjava.loader.RelativePathResolver.CURRENT_PATH_CONTEXT_KEY; |
| 4 | + |
3 | 5 | import com.google.common.annotations.Beta; |
4 | 6 | import com.hubspot.jinjava.el.ext.AstMacroFunction; |
5 | 7 | import com.hubspot.jinjava.el.ext.DeferredInvocationResolutionException; |
|
13 | 15 | import com.hubspot.jinjava.lib.fn.MacroFunction; |
14 | 16 | import com.hubspot.jinjava.lib.tag.MacroTag; |
15 | 17 | import com.hubspot.jinjava.lib.tag.eager.EagerExecutionResult; |
| 18 | +import com.hubspot.jinjava.lib.tag.eager.importing.EagerImportingStrategyFactory; |
16 | 19 | import com.hubspot.jinjava.objects.serialization.PyishObjectMapper; |
17 | 20 | import com.hubspot.jinjava.tree.Node; |
18 | 21 | import com.hubspot.jinjava.util.EagerContextWatcher; |
@@ -80,6 +83,11 @@ public Object doEvaluate( |
80 | 83 | () -> getEvaluationResultDirectly(argMap, kwargMap, varArgs, interpreter), |
81 | 84 | interpreter |
82 | 85 | ); |
| 86 | + return wrapCurrentPathSetting( |
| 87 | + interpreter, |
| 88 | + importFile, |
| 89 | + result.asTemplateString() |
| 90 | + ); |
83 | 91 | } |
84 | 92 | return result.asTemplateString(); |
85 | 93 | } finally { |
@@ -121,12 +129,45 @@ public Object doEvaluate( |
121 | 129 | .put( |
122 | 130 | tempVarName, |
123 | 131 | new MacroFunctionTempVariable( |
124 | | - prefixToPreserveState + eagerExecutionResult.asTemplateString() |
| 132 | + wrapCurrentPathSetting( |
| 133 | + interpreter, |
| 134 | + Optional |
| 135 | + .ofNullable(localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY)) |
| 136 | + .map(Object::toString), |
| 137 | + prefixToPreserveState + eagerExecutionResult.asTemplateString() |
| 138 | + ) |
125 | 139 | ) |
126 | 140 | ); |
127 | 141 | throw new DeferredInvocationResolutionException(tempVarName); |
128 | 142 | } |
129 | | - return eagerExecutionResult.getResult().toString(true); |
| 143 | + return wrapCurrentPathSetting( |
| 144 | + interpreter, |
| 145 | + Optional |
| 146 | + .ofNullable(localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY)) |
| 147 | + .map(Object::toString), |
| 148 | + eagerExecutionResult.getResult().toString(true) |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + private static String wrapCurrentPathSetting( |
| 153 | + JinjavaInterpreter interpreter, |
| 154 | + Optional<String> importFile, |
| 155 | + String resultToWrap |
| 156 | + ) { |
| 157 | + if (!importFile.isPresent()) { |
| 158 | + return resultToWrap; |
| 159 | + } |
| 160 | + final String initialPathSetter = |
| 161 | + EagerImportingStrategyFactory.getSetTagForCurrentPath(interpreter); |
| 162 | + final String newPathSetter = EagerReconstructionUtils.buildBlockOrInlineSetTag( |
| 163 | + CURRENT_PATH_CONTEXT_KEY, |
| 164 | + importFile.get(), |
| 165 | + interpreter |
| 166 | + ); |
| 167 | + if (initialPathSetter.equals(newPathSetter)) { |
| 168 | + return resultToWrap; |
| 169 | + } |
| 170 | + return newPathSetter + resultToWrap + initialPathSetter; |
130 | 171 | } |
131 | 172 |
|
132 | 173 | private String getEvaluationResultDirectly( |
@@ -312,6 +353,7 @@ private boolean differentMacroWithSameNameExists(JinjavaInterpreter interpreter) |
312 | 353 | return false; |
313 | 354 | } |
314 | 355 | MacroFunction mostRecent = context.getGlobalMacro(getName()); |
| 356 | + //noinspection ErrorProne |
315 | 357 | if (this != mostRecent) { |
316 | 358 | return true; |
317 | 359 | } |
|
0 commit comments