Skip to content

Commit

Permalink
Add interpreter factory to be able to use extended interpreters
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Ruban committed Jul 11, 2019
1 parent 766c29d commit 1f68b2c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/hubspot/jinjava/Jinjava.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public RenderResult renderForResult(String template, Map<String, ?> bindings, Ji
renderConfig = parentInterpreter.getConfig();
}

JinjavaInterpreter interpreter = new JinjavaInterpreter(this, context, renderConfig);
JinjavaInterpreter interpreter = globalConfig.getInterpreterFactory().newInstance(this, context, renderConfig);
JinjavaInterpreter.pushCurrent(interpreter);

try {
Expand Down Expand Up @@ -224,7 +224,7 @@ public RenderResult renderForResult(String template, Map<String, ?> bindings, Ji
* @return a new interpreter instance
*/
public JinjavaInterpreter newInterpreter() {
return new JinjavaInterpreter(this, this.getGlobalContext(), this.getGlobalConfig());
return globalConfig.getInterpreterFactory().newInstance(this, this.getGlobalContext(), this.getGlobalConfig());
}

}
28 changes: 24 additions & 4 deletions src/main/java/com/hubspot/jinjava/JinjavaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.Context.Library;
import com.hubspot.jinjava.interpret.InterpreterFactory;
import com.hubspot.jinjava.interpret.JinjavaInterpreterFactory;
import com.hubspot.jinjava.random.RandomNumberGeneratorStrategy;

public class JinjavaConfig {
Expand All @@ -49,17 +51,22 @@ public class JinjavaConfig {
private final RandomNumberGeneratorStrategy randomNumberGenerator;
private final boolean validationMode;
private final long maxStringLength;
private InterpreterFactory interpreterFactory;

public static Builder newBuilder() {
return new Builder();
}

public JinjavaConfig() {
this(StandardCharsets.UTF_8, Locale.ENGLISH, ZoneOffset.UTC, 10, new HashMap<>(), false, false, true, false, 0, false, 0, true, RandomNumberGeneratorStrategy.THREAD_LOCAL, false, 0);
this(new JinjavaInterpreterFactory());
}

public JinjavaConfig(InterpreterFactory interpreterFactory) {
this(StandardCharsets.UTF_8, Locale.ENGLISH, ZoneOffset.UTC, 10, new HashMap<>(), false, false, true, false, 0, false, 0, true, RandomNumberGeneratorStrategy.THREAD_LOCAL, false, 0, interpreterFactory);
}

public JinjavaConfig(Charset charset, Locale locale, ZoneId timeZone, int maxRenderDepth) {
this(charset, locale, timeZone, maxRenderDepth, new HashMap<>(), false, false, true, false, 0, false, 0, true, RandomNumberGeneratorStrategy.THREAD_LOCAL, false, 0);
this(charset, locale, timeZone, maxRenderDepth, new HashMap<>(), false, false, true, false, 0, false, 0, true, RandomNumberGeneratorStrategy.THREAD_LOCAL, false, 0, new JinjavaInterpreterFactory());
}

private JinjavaConfig(Charset charset,
Expand All @@ -77,7 +84,8 @@ private JinjavaConfig(Charset charset,
boolean nestedInterpretationEnabled,
RandomNumberGeneratorStrategy randomNumberGenerator,
boolean validationMode,
long maxStringLength) {
long maxStringLength,
InterpreterFactory interpreterFactory) {
this.charset = charset;
this.locale = locale;
this.timeZone = timeZone;
Expand All @@ -94,6 +102,7 @@ private JinjavaConfig(Charset charset,
this.randomNumberGenerator = randomNumberGenerator;
this.validationMode = validationMode;
this.maxStringLength = maxStringLength;
this.interpreterFactory = interpreterFactory;
}

public Charset getCharset() {
Expand Down Expand Up @@ -160,6 +169,10 @@ public long getMaxStringLength() {
return maxStringLength;
}

public InterpreterFactory getInterpreterFactory() {
return interpreterFactory;
}

public static class Builder {
private Charset charset = StandardCharsets.UTF_8;
private Locale locale = Locale.ENGLISH;
Expand All @@ -179,6 +192,7 @@ public static class Builder {
private RandomNumberGeneratorStrategy randomNumberGeneratorStrategy = RandomNumberGeneratorStrategy.THREAD_LOCAL;
private boolean validationMode = false;
private long maxStringLength = 0;
private InterpreterFactory interpreterFactory = new JinjavaInterpreterFactory();

private Builder() {}

Expand Down Expand Up @@ -263,6 +277,11 @@ public Builder withMaxStringLength(long maxStringLength) {
return this;
}

public Builder withInterperterFactory(InterpreterFactory interperterFactory) {
this.interpreterFactory = interperterFactory;
return this;
}

public JinjavaConfig build() {
return new JinjavaConfig(charset,
locale,
Expand All @@ -279,7 +298,8 @@ public JinjavaConfig build() {
nestedInterpretationEnabled,
randomNumberGeneratorStrategy,
validationMode,
maxStringLength);
maxStringLength,
interpreterFactory);
}

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

import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;

public interface InterpreterFactory {
JinjavaInterpreter newInstance(JinjavaInterpreter orig);
JinjavaInterpreter newInstance(Jinjava application, Context context, JinjavaConfig renderConfig);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hubspot.jinjava.interpret;

import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;

public class JinjavaInterpreterFactory implements InterpreterFactory {
@Override
public JinjavaInterpreter newInstance(JinjavaInterpreter orig) {
return new JinjavaInterpreter(orig);
}

@Override
public JinjavaInterpreter newInstance(Jinjava application, Context context, JinjavaConfig renderConfig) {
return new JinjavaInterpreter(application, context, renderConfig);
}
}

2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
String template = interpreter.getResource(templateFile);
Node node = interpreter.parse(template);

JinjavaInterpreter child = new JinjavaInterpreter(interpreter);
JinjavaInterpreter child = interpreter.getConfig().getInterpreterFactory().newInstance(interpreter);
child.getContext().put(Context.IMPORT_RESOURCE_PATH_KEY, templateFile);
JinjavaInterpreter.pushCurrent(child);
try {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
String template = interpreter.getResource(templateFile);
Node node = interpreter.parse(template);

JinjavaInterpreter child = new JinjavaInterpreter(interpreter);
JinjavaInterpreter child = interpreter.getConfig().getInterpreterFactory().newInstance(interpreter);
child.getContext().put(Context.IMPORT_RESOURCE_PATH_KEY, templateFile);
JinjavaInterpreter.pushCurrent(child);

Expand Down

0 comments on commit 1f68b2c

Please sign in to comment.