2222import com .google .common .collect .SetMultimap ;
2323import com .google .common .collect .Sets ;
2424import com .hubspot .jinjava .lib .Importable ;
25- import com .hubspot .jinjava .lib .expression .DefaultExpressionStrategy ;
2625import com .hubspot .jinjava .lib .expression .ExpressionStrategy ;
2726import com .hubspot .jinjava .lib .exptest .ExpTest ;
2827import 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
0 commit comments