Skip to content

Commit db765bf

Browse files
committed
Merge branch 'master' of github.com:HubSpot/jinjava into revert-616-revert-604-eager-import-path
2 parents 236d1b8 + 535c9d4 commit db765bf

31 files changed

+321
-183
lines changed

src/main/java/com/hubspot/jinjava/Jinjava.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ public JinjavaDoc getJinjavaDoc() {
143143
return new JinjavaDocFactory(this).get();
144144
}
145145

146+
/**
147+
* @return code snippets of all available filters, functions, and tags registered on this jinjava instance.
148+
*/
149+
public String getJinjavaSnippetDoc() {
150+
return new JinjavaDocFactory(this).getCodeEditorTagSnippets();
151+
}
152+
146153
/**
147154
* Render the given template using the given context bindings.
148155
*

src/main/java/com/hubspot/jinjava/JinjavaConfig.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public class JinjavaConfig {
5151
private final Map<Context.Library, Set<String>> disabled;
5252
private final boolean failOnUnknownTokens;
5353
private final boolean nestedInterpretationEnabled;
54-
private final boolean usePyishObjectMapper;
5554
private final RandomNumberGeneratorStrategy randomNumberGenerator;
5655
private final boolean validationMode;
5756
private final long maxStringLength;
@@ -103,7 +102,6 @@ private JinjavaConfig(Builder builder) {
103102
failOnUnknownTokens = builder.failOnUnknownTokens;
104103
maxOutputSize = builder.maxOutputSize;
105104
nestedInterpretationEnabled = builder.nestedInterpretationEnabled;
106-
usePyishObjectMapper = builder.usePyishObjectMapper;
107105
randomNumberGenerator = builder.randomNumberGeneratorStrategy;
108106
validationMode = builder.validationMode;
109107
maxStringLength = builder.maxStringLength;
@@ -176,10 +174,6 @@ public boolean isNestedInterpretationEnabled() {
176174
return nestedInterpretationEnabled;
177175
}
178176

179-
public boolean isUsePyishObjectMapper() {
180-
return usePyishObjectMapper;
181-
}
182-
183177
public boolean isValidationMode() {
184178
return validationMode;
185179
}
@@ -235,7 +229,6 @@ public static class Builder {
235229
private int maxMacroRecursionDepth;
236230
private boolean failOnUnknownTokens;
237231
private boolean nestedInterpretationEnabled = true;
238-
private boolean usePyishObjectMapper = false;
239232
private RandomNumberGeneratorStrategy randomNumberGeneratorStrategy =
240233
RandomNumberGeneratorStrategy.THREAD_LOCAL;
241234
private boolean validationMode = false;
@@ -330,11 +323,6 @@ public Builder withNestedInterpretationEnabled(boolean nestedInterpretationEnabl
330323
return this;
331324
}
332325

333-
public Builder withUsePyishObjectMapper(boolean usePyishObjectMapper) {
334-
this.usePyishObjectMapper = usePyishObjectMapper;
335-
return this;
336-
}
337-
338326
public Builder withValidationMode(boolean validationMode) {
339327
this.validationMode = validationMode;
340328
return this;
@@ -366,7 +354,7 @@ public Builder withTokenScannerSymbols(TokenScannerSymbols tokenScannerSymbols)
366354
}
367355

368356
/**
369-
* @deprecated Replaced by {@link LegacyOverrides.Builder#withIterateOverMapKeys(boolean)} ()}
357+
* @deprecated Replaced by {@link LegacyOverrides.Builder#withIterateOverMapKeys(boolean)}}
370358
*/
371359
@Deprecated
372360
public Builder withIterateOverMapKeys(boolean iterateOverMapKeys) {

src/main/java/com/hubspot/jinjava/LegacyOverrides.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ public class LegacyOverrides {
88
public static final LegacyOverrides NONE = new LegacyOverrides.Builder().build();
99
private final boolean evaluateMapKeys;
1010
private final boolean iterateOverMapKeys;
11+
private final boolean usePyishObjectMapper;
1112

1213
private LegacyOverrides(Builder builder) {
1314
evaluateMapKeys = builder.evaluateMapKeys;
1415
iterateOverMapKeys = builder.iterateOverMapKeys;
16+
usePyishObjectMapper = builder.usePyishObjectMapper;
1517
}
1618

1719
public static Builder newBuilder() {
@@ -26,9 +28,14 @@ public boolean isIterateOverMapKeys() {
2628
return iterateOverMapKeys;
2729
}
2830

31+
public boolean isUsePyishObjectMapper() {
32+
return usePyishObjectMapper;
33+
}
34+
2935
public static class Builder {
3036
private boolean evaluateMapKeys = false;
3137
private boolean iterateOverMapKeys = false;
38+
private boolean usePyishObjectMapper = false;
3239

3340
private Builder() {}
3441

@@ -39,7 +46,8 @@ public LegacyOverrides build() {
3946
public static Builder from(LegacyOverrides legacyOverrides) {
4047
return new Builder()
4148
.withEvaluateMapKeys(legacyOverrides.evaluateMapKeys)
42-
.withIterateOverMapKeys(legacyOverrides.iterateOverMapKeys);
49+
.withIterateOverMapKeys(legacyOverrides.iterateOverMapKeys)
50+
.withUsePyishObjectMapper(legacyOverrides.usePyishObjectMapper);
4351
}
4452

4553
public Builder withEvaluateMapKeys(boolean evaluateMapKeys) {
@@ -51,5 +59,10 @@ public Builder withIterateOverMapKeys(boolean iterateOverMapKeys) {
5159
this.iterateOverMapKeys = iterateOverMapKeys;
5260
return this;
5361
}
62+
63+
public Builder withUsePyishObjectMapper(boolean usePyishObjectMapper) {
64+
this.usePyishObjectMapper = usePyishObjectMapper;
65+
return this;
66+
}
5467
}
5568
}

src/main/java/com/hubspot/jinjava/doc/JinjavaDocFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public JinjavaDoc get() {
4545
return doc;
4646
}
4747

48-
public String getVSCodeTagSnippets() {
48+
public String getCodeEditorTagSnippets() {
4949
StringBuffer snippets = new StringBuffer();
5050
for (Tag tag : jinjava.getGlobalContextCopy().getAllTags()) {
5151
if (tag instanceof EndTag) {

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

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.hubspot.jinjava.lib.tag.Tag;
3333
import com.hubspot.jinjava.lib.tag.TagLibrary;
3434
import com.hubspot.jinjava.lib.tag.eager.EagerToken;
35-
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
3635
import com.hubspot.jinjava.tree.Node;
3736
import com.hubspot.jinjava.util.DeferredValueUtils;
3837
import com.hubspot.jinjava.util.ScopeMap;
@@ -46,6 +45,7 @@
4645
import java.util.Optional;
4746
import java.util.Set;
4847
import java.util.Stack;
48+
import java.util.function.Consumer;
4949
import java.util.stream.Collectors;
5050

5151
public class Context extends ScopeMap<String, Object> {
@@ -93,7 +93,6 @@ public enum Library {
9393
private final FilterLibrary filterLibrary;
9494
private final FunctionLibrary functionLibrary;
9595
private final TagLibrary tagLibrary;
96-
private final PyishObjectMapper pyishObjectMapper;
9796

9897
private ExpressionStrategy expressionStrategy = new DefaultExpressionStrategy();
9998

@@ -109,6 +108,7 @@ public enum Library {
109108
private boolean deferredExecutionMode = false;
110109
private boolean throwInterpreterErrors = false;
111110
private boolean partialMacroEvaluation = false;
111+
private boolean unwrapRawOverride = false;
112112

113113
public Context() {
114114
this(null, null, null, true);
@@ -198,11 +198,10 @@ public Context(
198198
this.tagLibrary = new TagLibrary(parent == null, disabled.get(Library.TAG));
199199
this.functionLibrary =
200200
new FunctionLibrary(parent == null, disabled.get(Library.FUNCTION));
201-
this.pyishObjectMapper =
202-
new PyishObjectMapper(parent != null ? parent.pyishObjectMapper : null);
203201
if (parent != null) {
204202
this.expressionStrategy = parent.expressionStrategy;
205203
this.partialMacroEvaluation = parent.partialMacroEvaluation;
204+
this.unwrapRawOverride = parent.unwrapRawOverride;
206205
}
207206
}
208207

@@ -530,14 +529,6 @@ public void registerTag(Tag t) {
530529
tagLibrary.addTag(t);
531530
}
532531

533-
public void registerNonPyishClasses(Class<?>... classes) {
534-
pyishObjectMapper.registerNonPyishClasses(classes);
535-
}
536-
537-
public PyishObjectMapper getPyishObjectMapper() {
538-
return pyishObjectMapper;
539-
}
540-
541532
public ExpressionStrategy getExpressionStrategy() {
542533
return expressionStrategy;
543534
}
@@ -653,24 +644,44 @@ public void setPartialMacroEvaluation(boolean partialMacroEvaluation) {
653644
this.partialMacroEvaluation = partialMacroEvaluation;
654645
}
655646

656-
public PartialMacroEvaluationClosable withPartialMacroEvaluation() {
657-
PartialMacroEvaluationClosable partialMacroEvaluationClosable = new PartialMacroEvaluationClosable(
658-
this.partialMacroEvaluation
647+
public TemporaryValueClosable<Boolean> withPartialMacroEvaluation() {
648+
TemporaryValueClosable<Boolean> temporaryValueClosable = new TemporaryValueClosable<>(
649+
this.partialMacroEvaluation,
650+
this::setPartialMacroEvaluation
659651
);
660652
this.partialMacroEvaluation = true;
661-
return partialMacroEvaluationClosable;
653+
return temporaryValueClosable;
654+
}
655+
656+
public boolean isUnwrapRawOverride() {
657+
return unwrapRawOverride;
658+
}
659+
660+
public void setUnwrapRawOverride(boolean unwrapRawOverride) {
661+
this.unwrapRawOverride = unwrapRawOverride;
662+
}
663+
664+
public TemporaryValueClosable<Boolean> withUnwrapRawOverride() {
665+
TemporaryValueClosable<Boolean> temporaryValueClosable = new TemporaryValueClosable<>(
666+
this.unwrapRawOverride,
667+
this::setUnwrapRawOverride
668+
);
669+
this.unwrapRawOverride = true;
670+
return temporaryValueClosable;
662671
}
663672

664-
public class PartialMacroEvaluationClosable implements AutoCloseable {
665-
private final boolean previousPartialMacroEvaluation;
673+
public static class TemporaryValueClosable<T> implements AutoCloseable {
674+
private final T previousValue;
675+
private final Consumer<T> resetValueConsumer;
666676

667-
private PartialMacroEvaluationClosable(boolean previousPartialMacroEvaluation) {
668-
this.previousPartialMacroEvaluation = previousPartialMacroEvaluation;
677+
private TemporaryValueClosable(T previousValue, Consumer<T> resetValueConsumer) {
678+
this.previousValue = previousValue;
679+
this.resetValueConsumer = resetValueConsumer;
669680
}
670681

671682
@Override
672683
public void close() {
673-
setPartialMacroEvaluation(previousPartialMacroEvaluation);
684+
resetValueConsumer.accept(previousValue);
674685
}
675686
}
676687
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import com.hubspot.jinjava.Jinjava;
2828
import com.hubspot.jinjava.JinjavaConfig;
2929
import com.hubspot.jinjava.el.ExpressionResolver;
30+
import com.hubspot.jinjava.interpret.Context.TemporaryValueClosable;
3031
import com.hubspot.jinjava.interpret.TemplateError.ErrorItem;
3132
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
3233
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
3334
import com.hubspot.jinjava.interpret.errorcategory.BasicTemplateErrorCategory;
35+
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
3436
import com.hubspot.jinjava.random.ConstantZeroRandomNumberGenerator;
3537
import com.hubspot.jinjava.random.DeferredRandomNumberGenerator;
3638
import com.hubspot.jinjava.tree.Node;
@@ -202,7 +204,9 @@ public String renderFlat(String template) {
202204
return template;
203205
} else {
204206
context.setRenderDepth(depth + 1);
205-
return render(parse(template), false);
207+
try (TemporaryValueClosable<Boolean> c = context.withUnwrapRawOverride()) {
208+
return render(parse(template), false);
209+
}
206210
}
207211
} finally {
208212
context.setRenderDepth(depth);
@@ -478,8 +482,8 @@ public String resolveString(String variable, int lineNumber, int startPosition)
478482
}
479483

480484
public String getAsString(Object object) {
481-
if (config.isUsePyishObjectMapper()) {
482-
return context.getPyishObjectMapper().getAsUnquotedPyishString(object);
485+
if (config.getLegacyOverrides().isUsePyishObjectMapper()) {
486+
return PyishObjectMapper.getAsUnquotedPyishString(object);
483487
}
484488
return Objects.toString(object, "");
485489
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.hubspot.jinjava.tree.parse.ExpressionToken;
1212
import com.hubspot.jinjava.util.ChunkResolver;
1313
import com.hubspot.jinjava.util.Logging;
14-
import com.hubspot.jinjava.util.WhitespaceUtils;
1514
import org.apache.commons.lang3.StringUtils;
1615

1716
public class EagerExpressionStrategy implements ExpressionStrategy {
@@ -48,11 +47,12 @@ private EagerStringResult eagerResolveExpression(
4847
: ""
4948
);
5049
if (chunkResolver.getDeferredWords().isEmpty()) {
51-
String fullyResolved = chunkResolver.resolveChunk(
52-
resolvedExpression.getResult(),
53-
""
50+
String result = interpreter.getAsString(
51+
interpreter.resolveELExpression(
52+
resolvedExpression.getResult(),
53+
interpreter.getLineNumber()
54+
)
5455
);
55-
String result = WhitespaceUtils.unquoteAndUnescape(fullyResolved);
5656
if (
5757
!StringUtils.equals(result, master.getImage()) &&
5858
(
@@ -114,6 +114,7 @@ private static String wrapInRawOrExpressionIfNeeded(
114114
JinjavaConfig config = interpreter.getConfig();
115115
if (
116116
config.getExecutionMode().isPreserveRawTags() &&
117+
!interpreter.getContext().isUnwrapRawOverride() &&
117118
(
118119
output.contains(config.getTokenScannerSymbols().getExpressionStart()) ||
119120
output.contains(config.getTokenScannerSymbols().getExpressionStartWithTag())

src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ public String getEvaluationResult(
143143
for (Node node : content) {
144144
result.append(node.render(interpreter));
145145
}
146+
if (interpreter.getConfig().getExecutionMode().isPreserveRawTags()) {
147+
return interpreter.renderFlat(result.toString());
148+
}
146149
return result.toString();
147150
}
148151

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,13 @@ public Object doEvaluate(
6363

6464
public String getStartTag(JinjavaInterpreter interpreter) {
6565
StringJoiner argJoiner = new StringJoiner(", ");
66-
PyishObjectMapper pyishObjectMapper = interpreter.getContext().getPyishObjectMapper();
6766
for (String arg : macroFunction.getArguments()) {
6867
if (macroFunction.getDefaults().get(arg) != null) {
6968
argJoiner.add(
7069
String.format(
7170
"%s=%s",
7271
arg,
73-
pyishObjectMapper.getAsPyishString(macroFunction.getDefaults().get(arg))
72+
PyishObjectMapper.getAsPyishString(macroFunction.getDefaults().get(arg))
7473
)
7574
);
7675
continue;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public String getName() {
3030

3131
@Override
3232
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
33-
if (interpreter.getConfig().getExecutionMode().isPreserveRawTags()) {
33+
if (
34+
interpreter.getConfig().getExecutionMode().isPreserveRawTags() &&
35+
!interpreter.getContext().isUnwrapRawOverride()
36+
) {
3437
return renderNodeRaw(tagNode);
3538
}
3639

0 commit comments

Comments
 (0)