Skip to content

Commit 650afd0

Browse files
committed
Condense overrideable options in Context to ContextConfiguration
immutable
1 parent f59d5eb commit 650afd0

File tree

3 files changed

+91
-40
lines changed

3 files changed

+91
-40
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
<dep.plugin.jacoco.version>0.8.3</dep.plugin.jacoco.version>
2121
<dep.plugin.javadoc.version>3.0.1</dep.plugin.javadoc.version>
22+
<dep.hubspot-immutables.version>1.9</dep.hubspot-immutables.version>
23+
2224

2325
<basepom.test.add.opens>
2426
--add-opens=java.base/java.lang=ALL-UNNAMED
@@ -187,6 +189,16 @@
187189
<artifactId>mockito-core</artifactId>
188190
<scope>test</scope>
189191
</dependency>
192+
<dependency>
193+
<groupId>org.immutables</groupId>
194+
<artifactId>value</artifactId>
195+
<scope>provided</scope>
196+
</dependency>
197+
<dependency>
198+
<groupId>com.hubspot.immutables</groupId>
199+
<artifactId>hubspot-style</artifactId>
200+
<version>${dep.hubspot-immutables.version}</version>
201+
</dependency>
190202
</dependencies>
191203

192204
<build>

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

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.common.collect.SetMultimap;
2323
import com.google.common.collect.Sets;
2424
import com.hubspot.jinjava.lib.Importable;
25-
import com.hubspot.jinjava.lib.expression.DefaultExpressionStrategy;
2625
import com.hubspot.jinjava.lib.expression.ExpressionStrategy;
2726
import com.hubspot.jinjava.lib.exptest.ExpTest;
2827
import com.hubspot.jinjava.lib.exptest.ExpTestLibrary;
@@ -65,11 +64,11 @@ public class Context extends ScopeMap<String, Object> {
6564
private Map<Library, Set<String>> disabled;
6665

6766
public boolean isValidationMode() {
68-
return validationMode;
67+
return contextConfiguration.isValidationMode();
6968
}
7069

7170
public Context setValidationMode(boolean validationMode) {
72-
this.validationMode = validationMode;
71+
contextConfiguration = contextConfiguration.withValidationMode(validationMode);
7372
return this;
7473
}
7574

@@ -101,23 +100,14 @@ public enum Library {
101100
private final FunctionLibrary functionLibrary;
102101
private final TagLibrary tagLibrary;
103102

104-
private ExpressionStrategy expressionStrategy = new DefaultExpressionStrategy();
105-
106103
private final Context parent;
107104

108105
private int renderDepth = -1;
109106
private Boolean autoEscape;
110107
private List<? extends Node> superBlock;
111108

112109
private final Stack<String> renderStack = new Stack<>();
113-
114-
private boolean validationMode = false;
115-
private boolean deferredExecutionMode = false;
116-
private boolean deferLargeObjects = false;
117-
private boolean throwInterpreterErrors = false;
118-
private boolean partialMacroEvaluation = false;
119-
private boolean unwrapRawOverride = false;
120-
private DynamicVariableResolver dynamicVariableResolver = null;
110+
private ContextConfiguration contextConfiguration = ContextConfiguration.of();
121111
private final Set<String> metaContextVariables; // These variable names aren't tracked in eager execution
122112
private final Set<String> overriddenNonMetaContextVariables;
123113
private Node currentNode;
@@ -215,13 +205,7 @@ public Context(
215205
this.overriddenNonMetaContextVariables =
216206
parent == null ? new HashSet<>() : parent.overriddenNonMetaContextVariables;
217207
if (parent != null) {
218-
this.expressionStrategy = parent.expressionStrategy;
219-
this.partialMacroEvaluation = parent.partialMacroEvaluation;
220-
this.unwrapRawOverride = parent.unwrapRawOverride;
221-
this.dynamicVariableResolver = parent.dynamicVariableResolver;
222-
this.deferredExecutionMode = parent.deferredExecutionMode;
223-
this.deferLargeObjects = parent.deferLargeObjects;
224-
this.throwInterpreterErrors = parent.throwInterpreterErrors;
208+
this.contextConfiguration = parent.contextConfiguration;
225209
}
226210
}
227211

@@ -654,21 +638,23 @@ public void registerTag(Tag t) {
654638
}
655639

656640
public DynamicVariableResolver getDynamicVariableResolver() {
657-
return dynamicVariableResolver;
641+
return contextConfiguration.getDynamicVariableResolver();
658642
}
659643

660644
public void setDynamicVariableResolver(
661645
final DynamicVariableResolver dynamicVariableResolver
662646
) {
663-
this.dynamicVariableResolver = dynamicVariableResolver;
647+
contextConfiguration =
648+
contextConfiguration.withDynamicVariableResolver(dynamicVariableResolver);
664649
}
665650

666651
public ExpressionStrategy getExpressionStrategy() {
667-
return expressionStrategy;
652+
return contextConfiguration.getExpressionStrategy();
668653
}
669654

670655
public void setExpressionStrategy(ExpressionStrategy expressionStrategy) {
671-
this.expressionStrategy = expressionStrategy;
656+
contextConfiguration =
657+
contextConfiguration.withExpressionStrategy(expressionStrategy);
672658
}
673659

674660
public Optional<String> getImportResourceAlias() {
@@ -754,48 +740,51 @@ public SetMultimap<String, String> getDependencies() {
754740
}
755741

756742
public boolean isDeferredExecutionMode() {
757-
return deferredExecutionMode;
743+
return contextConfiguration.isDeferredExecutionMode();
758744
}
759745

760746
public Context setDeferredExecutionMode(boolean deferredExecutionMode) {
761-
this.deferredExecutionMode = deferredExecutionMode;
747+
contextConfiguration =
748+
contextConfiguration.withDeferredExecutionMode(deferredExecutionMode);
762749
return this;
763750
}
764751

765752
public boolean isDeferLargeObjects() {
766-
return deferLargeObjects;
753+
return contextConfiguration.isDeferLargeObjects();
767754
}
768755

769756
public Context setDeferLargeObjects(boolean deferLargeObjects) {
770-
this.deferLargeObjects = deferLargeObjects;
757+
contextConfiguration = contextConfiguration.withDeferLargeObjects(deferLargeObjects);
771758
return this;
772759
}
773760

774761
public TemporaryValueClosable<Boolean> withDeferLargeObjects(
775762
boolean deferLargeObjects
776763
) {
777764
TemporaryValueClosable<Boolean> temporaryValueClosable = new TemporaryValueClosable<>(
778-
this.deferLargeObjects,
765+
isDeferLargeObjects(),
779766
this::setDeferLargeObjects
780767
);
781-
this.deferLargeObjects = deferLargeObjects;
768+
setDeferLargeObjects(deferLargeObjects);
782769
return temporaryValueClosable;
783770
}
784771

785772
public boolean getThrowInterpreterErrors() {
786-
return throwInterpreterErrors;
773+
return contextConfiguration.isThrowInterpreterErrors();
787774
}
788775

789776
public void setThrowInterpreterErrors(boolean throwInterpreterErrors) {
790-
this.throwInterpreterErrors = throwInterpreterErrors;
777+
contextConfiguration =
778+
contextConfiguration.withThrowInterpreterErrors(throwInterpreterErrors);
791779
}
792780

793781
public boolean isPartialMacroEvaluation() {
794-
return partialMacroEvaluation;
782+
return contextConfiguration.isPartialMacroEvaluation();
795783
}
796784

797785
public void setPartialMacroEvaluation(boolean partialMacroEvaluation) {
798-
this.partialMacroEvaluation = partialMacroEvaluation;
786+
contextConfiguration =
787+
contextConfiguration.withPartialMacroEvaluation(partialMacroEvaluation);
799788
}
800789

801790
public TemporaryValueClosable<Boolean> withPartialMacroEvaluation() {
@@ -806,27 +795,27 @@ public TemporaryValueClosable<Boolean> withPartialMacroEvaluation(
806795
boolean partialMacroEvaluation
807796
) {
808797
TemporaryValueClosable<Boolean> temporaryValueClosable = new TemporaryValueClosable<>(
809-
this.partialMacroEvaluation,
798+
isPartialMacroEvaluation(),
810799
this::setPartialMacroEvaluation
811800
);
812-
this.partialMacroEvaluation = partialMacroEvaluation;
801+
setPartialMacroEvaluation(partialMacroEvaluation);
813802
return temporaryValueClosable;
814803
}
815804

816805
public boolean isUnwrapRawOverride() {
817-
return unwrapRawOverride;
806+
return contextConfiguration.isUnwrapRawOverride();
818807
}
819808

820809
public void setUnwrapRawOverride(boolean unwrapRawOverride) {
821-
this.unwrapRawOverride = unwrapRawOverride;
810+
contextConfiguration = contextConfiguration.withUnwrapRawOverride(unwrapRawOverride);
822811
}
823812

824813
public TemporaryValueClosable<Boolean> withUnwrapRawOverride() {
825814
TemporaryValueClosable<Boolean> temporaryValueClosable = new TemporaryValueClosable<>(
826-
this.unwrapRawOverride,
815+
isUnwrapRawOverride(),
827816
this::setUnwrapRawOverride
828817
);
829-
this.unwrapRawOverride = true;
818+
setUnwrapRawOverride(true);
830819
return temporaryValueClosable;
831820
}
832821

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.hubspot.jinjava.interpret;
2+
3+
import com.hubspot.immutables.style.HubSpotImmutableStyle;
4+
import com.hubspot.jinjava.lib.expression.DefaultExpressionStrategy;
5+
import com.hubspot.jinjava.lib.expression.ExpressionStrategy;
6+
import javax.annotation.Nullable;
7+
import org.immutables.value.Value.Default;
8+
import org.immutables.value.Value.Immutable;
9+
10+
@Immutable(singleton = true)
11+
@HubSpotImmutableStyle
12+
public interface ContextConfigurationIF {
13+
@Default
14+
default ExpressionStrategy getExpressionStrategy() {
15+
return new DefaultExpressionStrategy();
16+
}
17+
18+
@Nullable
19+
DynamicVariableResolver getDynamicVariableResolver();
20+
21+
@Default
22+
default boolean isValidationMode() {
23+
return false;
24+
}
25+
26+
@Default
27+
default boolean isDeferredExecutionMode() {
28+
return false;
29+
}
30+
31+
@Default
32+
default boolean isDeferLargeObjects() {
33+
return false;
34+
}
35+
36+
@Default
37+
default boolean isThrowInterpreterErrors() {
38+
return false;
39+
}
40+
41+
@Default
42+
default boolean isPartialMacroEvaluation() {
43+
return false;
44+
}
45+
46+
@Default
47+
default boolean isUnwrapRawOverride() {
48+
return false;
49+
}
50+
}

0 commit comments

Comments
 (0)