Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Condense overrideable options in Context to ContextConfiguration immutable #1195

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

<dep.plugin.jacoco.version>0.8.3</dep.plugin.jacoco.version>
<dep.plugin.javadoc.version>3.0.1</dep.plugin.javadoc.version>
<dep.hubspot-immutables.version>1.9</dep.hubspot-immutables.version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about adding immutables into this. In the past, non-HS users have disliked any other dependencies as it makes it difficult to manage version conflicts. You could manually create a builder object for this instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is just a provided dependency and we don't need it at runtime, there's should be no potential for that kind of problem. It's not going to be included in the final jar since it's just used for compiling.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point.



<basepom.test.add.opens>
--add-opens=java.base/java.lang=ALL-UNNAMED
Expand Down Expand Up @@ -89,6 +91,16 @@
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.hubspot.immutables</groupId>
<artifactId>hubspot-style</artifactId>
<version>${dep.hubspot-immutables.version}</version>
</dependency>
<dependency>
<groupId>com.hubspot.immutables</groupId>
<artifactId>immutables-exceptions</artifactId>
<version>${dep.hubspot-immutables.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -187,6 +199,20 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.hubspot.immutables</groupId>
<artifactId>hubspot-style</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.hubspot.immutables</groupId>
<artifactId>immutables-exceptions</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
69 changes: 29 additions & 40 deletions src/main/java/com/hubspot/jinjava/interpret/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.hubspot.jinjava.lib.Importable;
import com.hubspot.jinjava.lib.expression.DefaultExpressionStrategy;
import com.hubspot.jinjava.lib.expression.ExpressionStrategy;
import com.hubspot.jinjava.lib.exptest.ExpTest;
import com.hubspot.jinjava.lib.exptest.ExpTestLibrary;
Expand Down Expand Up @@ -65,11 +64,11 @@ public class Context extends ScopeMap<String, Object> {
private Map<Library, Set<String>> disabled;

public boolean isValidationMode() {
return validationMode;
return contextConfiguration.isValidationMode();
}

public Context setValidationMode(boolean validationMode) {
this.validationMode = validationMode;
contextConfiguration = contextConfiguration.withValidationMode(validationMode);
return this;
}

Expand Down Expand Up @@ -101,23 +100,14 @@ public enum Library {
private final FunctionLibrary functionLibrary;
private final TagLibrary tagLibrary;

private ExpressionStrategy expressionStrategy = new DefaultExpressionStrategy();

private final Context parent;

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

private final Stack<String> renderStack = new Stack<>();

private boolean validationMode = false;
private boolean deferredExecutionMode = false;
private boolean deferLargeObjects = false;
private boolean throwInterpreterErrors = false;
private boolean partialMacroEvaluation = false;
private boolean unwrapRawOverride = false;
private DynamicVariableResolver dynamicVariableResolver = null;
private ContextConfiguration contextConfiguration = ContextConfiguration.of();
private final Set<String> metaContextVariables; // These variable names aren't tracked in eager execution
private final Set<String> overriddenNonMetaContextVariables;
private Node currentNode;
Expand Down Expand Up @@ -215,13 +205,7 @@ public Context(
this.overriddenNonMetaContextVariables =
parent == null ? new HashSet<>() : parent.overriddenNonMetaContextVariables;
if (parent != null) {
this.expressionStrategy = parent.expressionStrategy;
this.partialMacroEvaluation = parent.partialMacroEvaluation;
this.unwrapRawOverride = parent.unwrapRawOverride;
this.dynamicVariableResolver = parent.dynamicVariableResolver;
this.deferredExecutionMode = parent.deferredExecutionMode;
this.deferLargeObjects = parent.deferLargeObjects;
this.throwInterpreterErrors = parent.throwInterpreterErrors;
this.contextConfiguration = parent.contextConfiguration;
}
}

Expand Down Expand Up @@ -654,21 +638,23 @@ public void registerTag(Tag t) {
}

public DynamicVariableResolver getDynamicVariableResolver() {
return dynamicVariableResolver;
return contextConfiguration.getDynamicVariableResolver();
}

public void setDynamicVariableResolver(
final DynamicVariableResolver dynamicVariableResolver
) {
this.dynamicVariableResolver = dynamicVariableResolver;
contextConfiguration =
contextConfiguration.withDynamicVariableResolver(dynamicVariableResolver);
}

public ExpressionStrategy getExpressionStrategy() {
return expressionStrategy;
return contextConfiguration.getExpressionStrategy();
}

public void setExpressionStrategy(ExpressionStrategy expressionStrategy) {
this.expressionStrategy = expressionStrategy;
contextConfiguration =
contextConfiguration.withExpressionStrategy(expressionStrategy);
}

public Optional<String> getImportResourceAlias() {
Expand Down Expand Up @@ -754,48 +740,51 @@ public SetMultimap<String, String> getDependencies() {
}

public boolean isDeferredExecutionMode() {
return deferredExecutionMode;
return contextConfiguration.isDeferredExecutionMode();
}

public Context setDeferredExecutionMode(boolean deferredExecutionMode) {
this.deferredExecutionMode = deferredExecutionMode;
contextConfiguration =
contextConfiguration.withDeferredExecutionMode(deferredExecutionMode);
return this;
}

public boolean isDeferLargeObjects() {
return deferLargeObjects;
return contextConfiguration.isDeferLargeObjects();
}

public Context setDeferLargeObjects(boolean deferLargeObjects) {
this.deferLargeObjects = deferLargeObjects;
contextConfiguration = contextConfiguration.withDeferLargeObjects(deferLargeObjects);
return this;
}

public TemporaryValueClosable<Boolean> withDeferLargeObjects(
boolean deferLargeObjects
) {
TemporaryValueClosable<Boolean> temporaryValueClosable = new TemporaryValueClosable<>(
this.deferLargeObjects,
isDeferLargeObjects(),
this::setDeferLargeObjects
);
this.deferLargeObjects = deferLargeObjects;
setDeferLargeObjects(deferLargeObjects);
return temporaryValueClosable;
}

public boolean getThrowInterpreterErrors() {
return throwInterpreterErrors;
return contextConfiguration.isThrowInterpreterErrors();
}

public void setThrowInterpreterErrors(boolean throwInterpreterErrors) {
this.throwInterpreterErrors = throwInterpreterErrors;
contextConfiguration =
contextConfiguration.withThrowInterpreterErrors(throwInterpreterErrors);
}

public boolean isPartialMacroEvaluation() {
return partialMacroEvaluation;
return contextConfiguration.isPartialMacroEvaluation();
}

public void setPartialMacroEvaluation(boolean partialMacroEvaluation) {
this.partialMacroEvaluation = partialMacroEvaluation;
contextConfiguration =
contextConfiguration.withPartialMacroEvaluation(partialMacroEvaluation);
}

public TemporaryValueClosable<Boolean> withPartialMacroEvaluation() {
Expand All @@ -806,27 +795,27 @@ public TemporaryValueClosable<Boolean> withPartialMacroEvaluation(
boolean partialMacroEvaluation
) {
TemporaryValueClosable<Boolean> temporaryValueClosable = new TemporaryValueClosable<>(
this.partialMacroEvaluation,
isPartialMacroEvaluation(),
this::setPartialMacroEvaluation
);
this.partialMacroEvaluation = partialMacroEvaluation;
setPartialMacroEvaluation(partialMacroEvaluation);
return temporaryValueClosable;
}

public boolean isUnwrapRawOverride() {
return unwrapRawOverride;
return contextConfiguration.isUnwrapRawOverride();
}

public void setUnwrapRawOverride(boolean unwrapRawOverride) {
this.unwrapRawOverride = unwrapRawOverride;
contextConfiguration = contextConfiguration.withUnwrapRawOverride(unwrapRawOverride);
}

public TemporaryValueClosable<Boolean> withUnwrapRawOverride() {
TemporaryValueClosable<Boolean> temporaryValueClosable = new TemporaryValueClosable<>(
this.unwrapRawOverride,
isUnwrapRawOverride(),
this::setUnwrapRawOverride
);
this.unwrapRawOverride = true;
setUnwrapRawOverride(true);
return temporaryValueClosable;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.hubspot.jinjava.interpret;

import com.hubspot.immutables.style.HubSpotImmutableStyle;
import com.hubspot.jinjava.lib.expression.DefaultExpressionStrategy;
import com.hubspot.jinjava.lib.expression.ExpressionStrategy;
import javax.annotation.Nullable;
import org.immutables.value.Value.Default;
import org.immutables.value.Value.Immutable;

@Immutable(singleton = true)
@HubSpotImmutableStyle
public interface ContextConfigurationIF {
@Default
default ExpressionStrategy getExpressionStrategy() {
return new DefaultExpressionStrategy();
}

@Nullable
DynamicVariableResolver getDynamicVariableResolver();

@Default
default boolean isValidationMode() {
return false;
}

@Default
default boolean isDeferredExecutionMode() {
return false;
}

@Default
default boolean isDeferLargeObjects() {
return false;
}

@Default
default boolean isThrowInterpreterErrors() {
return false;
}

@Default
default boolean isPartialMacroEvaluation() {
return false;
}

@Default
default boolean isUnwrapRawOverride() {
return false;
}
}
Loading