Skip to content

Commit 3d8ddbc

Browse files
committed
Update javadocs and deprecate old methods
1 parent 9eaf68e commit 3d8ddbc

6 files changed

Lines changed: 102 additions & 26 deletions

File tree

src/main/java/com/hubspot/jinjava/lib/expression/EagerExpressionStrategy.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ private String eagerResolveExpression(
6767
prefixToPreserveState.toString() + postProcessResult(master, result, interpreter)
6868
);
6969
}
70-
prefixToPreserveState.putAll(
71-
EagerReconstructionUtils.reconstructFromContextBeforeDeferringAsMap(
72-
eagerExecutionResult.getResult().getDeferredWords(),
73-
interpreter
74-
)
70+
EagerReconstructionUtils.hydrateReconstructionFromContextBeforeDeferring(
71+
prefixToPreserveState,
72+
eagerExecutionResult.getResult().getDeferredWords(),
73+
interpreter
7574
);
7675
String deferredExpressionImage = wrapInExpression(
7776
eagerExecutionResult.getResult().toString(),

src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerExecutionResult.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.hubspot.jinjava.lib.tag.eager;
22

3-
import static com.hubspot.jinjava.util.EagerReconstructionUtils.buildBlockOrInlineSetTag;
43
import static com.hubspot.jinjava.util.EagerReconstructionUtils.buildSetTag;
54

65
import com.google.common.annotations.Beta;
@@ -10,6 +9,7 @@
109
import com.hubspot.jinjava.interpret.LazyReference;
1110
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
1211
import com.hubspot.jinjava.util.EagerExpressionResolver.EagerExpressionResult;
12+
import com.hubspot.jinjava.util.EagerReconstructionUtils;
1313
import com.hubspot.jinjava.util.PrefixToPreserveState;
1414
import java.util.AbstractMap;
1515
import java.util.Collection;
@@ -69,9 +69,11 @@ public PrefixToPreserveState getPrefixToPreserveState() {
6969
.filter(entry -> !(entry.getValue() instanceof LazyReference))
7070
.forEach(
7171
entry ->
72-
prefixToPreserveState.put(
72+
EagerReconstructionUtils.hydrateBlockOrInlineSetTagRecursively(
73+
prefixToPreserveState,
7374
entry.getKey(),
74-
buildBlockOrInlineSetTag(entry.getKey(), entry.getValue(), interpreter)
75+
entry.getValue(),
76+
interpreter
7577
)
7678
);
7779
filteredEntries

src/main/java/com/hubspot/jinjava/util/EagerReconstructionUtils.java

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,15 @@ public static EagerExecutionResult executeInChildContext(
107107
* Reconstruct the macro functions and variables from the context before they
108108
* get deferred.
109109
* Those macro functions and variables found within {@code deferredWords} are
110-
* reconstructed with {@link MacroTag}(s) and a {@link SetTag}, respectively to
110+
* reconstructed with {@link MacroTag}(s) and {@link SetTag}(s), respectively to
111111
* preserve the context within the Jinjava template itself.
112112
* @param deferredWords set of words that will need to be deferred based on the
113113
* previously performed operation.
114114
* @param interpreter the Jinjava interpreter.
115-
* @return a Jinjava-syntax string of 0 or more macro tags and 0 or 1 set tags.
115+
* @return a Jinjava-syntax string of 0 or more macro tags and 0 or more set tags.
116+
* @deprecated use {@link #hydrateReconstructionFromContextBeforeDeferring(PrefixToPreserveState, Set, JinjavaInterpreter)}
116117
*/
118+
@Deprecated
117119
public static String reconstructFromContextBeforeDeferring(
118120
Set<String> deferredWords,
119121
JinjavaInterpreter interpreter
@@ -124,12 +126,25 @@ public static String reconstructFromContextBeforeDeferring(
124126
);
125127
}
126128

127-
public static Map<String, String> reconstructFromContextBeforeDeferringAsMap(
129+
/**
130+
* Reconstruct the macro functions and variables from the context before they
131+
* get deferred.
132+
* Those macro functions and variables found within {@code deferredWords} are
133+
* reconstructed with {@link MacroTag}(s) and {@link SetTag}(s), respectively to
134+
* preserve the context within the Jinjava template itself.
135+
* @param deferredWords set of words that will need to be deferred based on the
136+
* previously performed operation.
137+
* @param interpreter the Jinjava interpreter.
138+
* @return a PrefixToPreserveState map of 0 or more macro tags and 0 or more set tags.
139+
* @deprecated use {@link #hydrateReconstructionFromContextBeforeDeferring(PrefixToPreserveState, Set, JinjavaInterpreter)}
140+
*/
141+
@Deprecated
142+
public static PrefixToPreserveState reconstructFromContextBeforeDeferringAsMap(
128143
Set<String> deferredWords,
129144
JinjavaInterpreter interpreter
130145
) {
131146
PrefixToPreserveState prefixToPreserveState = new PrefixToPreserveState();
132-
reconstructFromContextBeforeDeferringAsMap(
147+
hydrateReconstructionFromContextBeforeDeferring(
133148
prefixToPreserveState,
134149
deferredWords,
135150
interpreter,
@@ -138,14 +153,38 @@ public static Map<String, String> reconstructFromContextBeforeDeferringAsMap(
138153
return prefixToPreserveState;
139154
}
140155

141-
private static void reconstructFromContextBeforeDeferringAsMap(
156+
/**
157+
* Reconstruct the macro functions and variables from the context before they
158+
* get deferred.
159+
* Those macro functions and variables found within {@code deferredWords} are
160+
* reconstructed with {@link MacroTag}(s) and {@link SetTag}(s), respectively to
161+
* preserve the context within the Jinjava template itself.
162+
* @param prefixToPreserveState This PrefixToPreserveState will be hydrated with the Macro tag images and set tag images
163+
* @param deferredWords set of words that will need to be deferred based on the
164+
* previously performed operation.
165+
* @param interpreter the Jinjava interpreter.
166+
*/
167+
public static void hydrateReconstructionFromContextBeforeDeferring(
168+
PrefixToPreserveState prefixToPreserveState,
169+
Set<String> deferredWords,
170+
JinjavaInterpreter interpreter
171+
) {
172+
hydrateReconstructionFromContextBeforeDeferring(
173+
prefixToPreserveState,
174+
deferredWords,
175+
interpreter,
176+
0
177+
);
178+
}
179+
180+
private static void hydrateReconstructionFromContextBeforeDeferring(
142181
PrefixToPreserveState prefixToPreserveState,
143182
Set<String> deferredWords,
144183
JinjavaInterpreter interpreter,
145184
int depth
146185
) {
147186
if (depth <= interpreter.getConfig().getMaxRenderDepth()) {
148-
reconstructMacroFunctionsBeforeDeferring(
187+
hydrateReconstructionOfMacroFunctionsBeforeDeferring(
149188
prefixToPreserveState,
150189
deferredWords,
151190
interpreter
@@ -155,7 +194,7 @@ private static void reconstructFromContextBeforeDeferringAsMap(
155194
return;
156195
}
157196

158-
reconstructSetVariablesBeforeDeferring(
197+
hydrateReconstructionOfVariablesBeforeDeferring(
159198
prefixToPreserveState,
160199
deferredWordBases,
161200
interpreter,
@@ -202,13 +241,12 @@ private static Set<String> filterToRelevantBases(
202241
* and remove those macro functions from the deferredWords set.
203242
* These macro functions are either global or local macro functions, with local
204243
* meaning they've been imported under an alias such as "simple.multiply()".
244+
* @param prefixToPreserveState This PrefixToPreserveState will be hydrated with the Macro tag images
205245
* @param deferredWords Set of words that were encountered and their evaluation has
206246
* to be deferred for a later render.
207247
* @param interpreter The Jinjava interpreter.
208-
* @return A jinjava-syntax string that is the images of any macro functions that must
209-
* be evaluated at a later time.
210248
*/
211-
private static void reconstructMacroFunctionsBeforeDeferring(
249+
private static void hydrateReconstructionOfMacroFunctionsBeforeDeferring(
212250
PrefixToPreserveState prefixToPreserveState,
213251
Set<String> deferredWords,
214252
JinjavaInterpreter interpreter
@@ -261,7 +299,7 @@ private static void reconstructMacroFunctionsBeforeDeferring(
261299
deferredWords.removeAll(toRemove);
262300
}
263301

264-
private static void reconstructSetVariablesBeforeDeferring(
302+
private static void hydrateReconstructionOfVariablesBeforeDeferring(
265303
PrefixToPreserveState prefixToPreserveState,
266304
Set<String> deferredWords,
267305
JinjavaInterpreter interpreter,
@@ -281,7 +319,7 @@ private static void reconstructSetVariablesBeforeDeferring(
281319
)
282320
.forEach(
283321
entry ->
284-
buildBlockOrInlineSetTagRecursively(
322+
hydrateBlockOrInlineSetTagRecursively(
285323
prefixToPreserveState,
286324
entry.getKey(),
287325
entry.getValue(),
@@ -307,24 +345,49 @@ public static String buildBlockOrInlineSetTagAndRegisterDeferredToken(
307345
return buildBlockOrInlineSetTag(name, value, interpreter, true);
308346
}
309347

310-
private static void buildBlockOrInlineSetTagRecursively(
348+
public static void hydrateBlockOrInlineSetTagRecursively(
349+
PrefixToPreserveState prefixToPreserveState,
350+
String name,
351+
Object value,
352+
JinjavaInterpreter interpreter
353+
) {
354+
hydrateBlockOrInlineSetTagRecursively(
355+
prefixToPreserveState,
356+
name,
357+
value,
358+
interpreter,
359+
0
360+
);
361+
}
362+
363+
private static void hydrateBlockOrInlineSetTagRecursively(
311364
PrefixToPreserveState prefixToPreserveState,
312365
String name,
313366
Object value,
314367
JinjavaInterpreter interpreter,
315368
int depth
316369
) {
317-
if (value instanceof DeferredValue || value instanceof PyishBlockSetSerializable) {
370+
if (
371+
value instanceof DeferredValue &&
372+
!(value instanceof PyishBlockSetSerializable || value instanceof PyishSerializable)
373+
) {
374+
value = ((DeferredValue) value).getOriginalValue();
375+
}
376+
if (value instanceof PyishBlockSetSerializable) {
318377
prefixToPreserveState.put(
319378
name,
320-
buildBlockOrInlineSetTag(name, value, interpreter, false)
379+
buildBlockSetTag(
380+
name,
381+
((PyishBlockSetSerializable) value).getBlockSetBody(),
382+
interpreter,
383+
false
384+
)
321385
);
322386
return;
323387
}
324388
String pyishStringRepresentation = PyishObjectMapper.getAsPyishString(value);
325389

326390
if (
327-
depth > 0 &&
328391
depth < interpreter.getConfig().getMaxRenderDepth() &&
329392
interpreter.getConfig().isNestedInterpretationEnabled()
330393
) {
@@ -333,7 +396,7 @@ private static void buildBlockOrInlineSetTagRecursively(
333396
interpreter
334397
);
335398
if (!dependentWords.isEmpty()) {
336-
reconstructFromContextBeforeDeferringAsMap(
399+
hydrateReconstructionFromContextBeforeDeferring(
337400
prefixToPreserveState,
338401
dependentWords,
339402
interpreter,

src/test/java/com/hubspot/jinjava/EagerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,4 +1397,11 @@ public void itReconstructsNestedValueInStringRepresentation() {
13971397
"reconstructs-nested-value-in-string-representation"
13981398
);
13991399
}
1400+
1401+
@Test
1402+
public void itReconstructsNestedValueInStringRepresentationSecondPass() {
1403+
expectedTemplateInterpreter.assertExpectedOutput(
1404+
"reconstructs-nested-value-in-string-representation.expected"
1405+
);
1406+
}
14001407
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% set i_am = 'I am' %}{% set bar = '{{ i_am }} bar' %}{% set foo = '{{ i_am }} foo' %}{% set map_with_vals = {'foo': 'Foo is {{ foo }}.'} %}{% if deferred %}
2+
{% do map_with_vals.put('bar', 'Bar is {{ bar }}.') %}
3+
{% endif %}
4+
map.foo: {{ map_with_vals.foo }}
5+
map.bar: {{ map_with_vals.bar }}

src/test/resources/eager/reconstructs-nested-value-in-string-representation.expected.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% set foo = 'I am foo' %}{% set bar = 'I am bar' %}{% set map_with_vals = {'foo': 'Foo is {{ foo }}.'} %}{% if deferred %}
1+
{% set i_am = 'I am' %}{% set bar = '{{ i_am }} bar' %}{% set foo = '{{ i_am }} foo' %}{% set map_with_vals = {'foo': 'Foo is {{ foo }}.'} %}{% if deferred %}
22
{% do map_with_vals.put('bar', 'Bar is {{ bar }}.') %}
33
{% endif %}
44
map.foo: {{ map_with_vals.foo }}

0 commit comments

Comments
 (0)