Skip to content

Commit 07ba397

Browse files
committed
Migrate DeferredRandomNumberGenerator to use DeferredParsingException
1 parent 3debc4e commit 07ba397

6 files changed

Lines changed: 45 additions & 13 deletions

File tree

src/main/java/com/hubspot/jinjava/el/ext/DeferredParsingException.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ public class DeferredParsingException extends DeferredValueException {
66
private final String deferredEvalResult;
77
private final Object sourceNode;
88

9+
public DeferredParsingException(String message) {
10+
super(message);
11+
this.deferredEvalResult = message;
12+
this.sourceNode = null;
13+
}
14+
915
public DeferredParsingException(Object sourceNode, String deferredEvalResult) {
1016
super(
1117
String.format(

src/main/java/com/hubspot/jinjava/el/ext/eager/EagerAstMethod.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import de.odysseus.el.tree.impl.ast.AstParameters;
1010
import de.odysseus.el.tree.impl.ast.AstProperty;
1111
import javax.el.ELContext;
12+
import javax.el.ELException;
1213

1314
public class EagerAstMethod extends AstMethod implements EvalResultHolder {
1415
protected Object evalResult;
@@ -37,10 +38,20 @@ public Object eval(Bindings bindings, ELContext context) {
3738
evalResult = super.eval(bindings, context);
3839
hasEvalResult = true;
3940
return evalResult;
40-
} catch (DeferredParsingException e) {
41+
} catch (DeferredParsingException | ELException e) {
42+
DeferredParsingException e1;
43+
if (!(e instanceof DeferredParsingException)) {
44+
if (e.getCause() instanceof DeferredParsingException) {
45+
e1 = (DeferredParsingException) e.getCause();
46+
} else {
47+
throw e;
48+
}
49+
} else {
50+
e1 = (DeferredParsingException) e;
51+
}
4152
throw new DeferredParsingException(
4253
this,
43-
getPartiallyResolved(bindings, context, e)
54+
getPartiallyResolved(bindings, context, e1)
4455
);
4556
} finally {
4657
property.getAndClearEvalResult();

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
import com.hubspot.jinjava.JinjavaConfig;
2929
import com.hubspot.jinjava.el.ExpressionResolver;
3030
import com.hubspot.jinjava.el.ext.DeferredParsingException;
31+
import com.hubspot.jinjava.el.ext.ExtendedParser;
3132
import com.hubspot.jinjava.interpret.TemplateError.ErrorItem;
3233
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
3334
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
3435
import com.hubspot.jinjava.interpret.errorcategory.BasicTemplateErrorCategory;
3536
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
37+
import com.hubspot.jinjava.objects.serialization.PyishSerializable;
3638
import com.hubspot.jinjava.random.ConstantZeroRandomNumberGenerator;
3739
import com.hubspot.jinjava.random.DeferredRandomNumberGenerator;
3840
import com.hubspot.jinjava.tree.Node;
@@ -60,7 +62,7 @@
6062
import org.apache.commons.lang3.StringUtils;
6163
import org.apache.commons.lang3.exception.ExceptionUtils;
6264

63-
public class JinjavaInterpreter {
65+
public class JinjavaInterpreter implements PyishSerializable {
6466
private final Multimap<String, BlockInfo> blocks = ArrayListMultimap.create();
6567
private final LinkedList<Node> extendParentRoots = new LinkedList<>();
6668

@@ -745,4 +747,9 @@ private String getWrappedErrorMessage(
745747
);
746748
}
747749
}
750+
751+
@Override
752+
public String toPyishString() {
753+
return ExtendedParser.INTERPRETER;
754+
}
748755
}

src/main/java/com/hubspot/jinjava/objects/serialization/PyishSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void serialize(
2222
jsonGenerator.setCharacterEscapes(PyishCharacterEscapes.INSTANCE);
2323
String string;
2424
if (object instanceof PyishSerializable) {
25-
jsonGenerator.writeRaw(((PyishSerializable) object).toPyishString());
25+
jsonGenerator.writeRawValue(((PyishSerializable) object).toPyishString());
2626
} else {
2727
string = Objects.toString(object, "");
2828
try {

src/main/java/com/hubspot/jinjava/random/DeferredRandomNumberGenerator.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.hubspot.jinjava.random;
22

3-
import com.hubspot.jinjava.interpret.DeferredValueException;
3+
import com.hubspot.jinjava.el.ext.DeferredParsingException;
44
import java.util.Random;
55
import java.util.stream.DoubleStream;
66
import java.util.stream.IntStream;
@@ -14,42 +14,42 @@ public class DeferredRandomNumberGenerator extends Random {
1414

1515
@Override
1616
protected int next(int bits) {
17-
throw new DeferredValueException(EXCEPTION_MESSAGE);
17+
throw new DeferredParsingException(EXCEPTION_MESSAGE);
1818
}
1919

2020
@Override
2121
public int nextInt() {
22-
throw new DeferredValueException(EXCEPTION_MESSAGE);
22+
throw new DeferredParsingException(EXCEPTION_MESSAGE);
2323
}
2424

2525
@Override
2626
public int nextInt(int bound) {
27-
throw new DeferredValueException(EXCEPTION_MESSAGE);
27+
throw new DeferredParsingException(EXCEPTION_MESSAGE);
2828
}
2929

3030
@Override
3131
public long nextLong() {
32-
throw new DeferredValueException(EXCEPTION_MESSAGE);
32+
throw new DeferredParsingException(EXCEPTION_MESSAGE);
3333
}
3434

3535
@Override
3636
public boolean nextBoolean() {
37-
throw new DeferredValueException(EXCEPTION_MESSAGE);
37+
throw new DeferredParsingException(EXCEPTION_MESSAGE);
3838
}
3939

4040
@Override
4141
public float nextFloat() {
42-
throw new DeferredValueException(EXCEPTION_MESSAGE);
42+
throw new DeferredParsingException(EXCEPTION_MESSAGE);
4343
}
4444

4545
@Override
4646
public double nextDouble() {
47-
throw new DeferredValueException(EXCEPTION_MESSAGE);
47+
throw new DeferredParsingException(EXCEPTION_MESSAGE);
4848
}
4949

5050
@Override
5151
public synchronized double nextGaussian() {
52-
throw new DeferredValueException(EXCEPTION_MESSAGE);
52+
throw new DeferredParsingException(EXCEPTION_MESSAGE);
5353
}
5454

5555
@Override

src/test/java/com/hubspot/jinjava/util/EagerExpressionResolverTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.hubspot.jinjava.objects.date.PyishDate;
1919
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
2020
import com.hubspot.jinjava.objects.serialization.PyishSerializable;
21+
import com.hubspot.jinjava.random.RandomNumberGeneratorStrategy;
2122
import com.hubspot.jinjava.tree.parse.DefaultTokenScannerSymbols;
2223
import com.hubspot.jinjava.tree.parse.TagToken;
2324
import com.hubspot.jinjava.tree.parse.TokenScannerSymbols;
@@ -52,6 +53,7 @@ private JinjavaInterpreter getInterpreter(boolean evaluateMapKeys) throws Except
5253
JinjavaConfig
5354
.newBuilder()
5455
.withExecutionMode(EagerExecutionMode.instance())
56+
.withRandomNumberGeneratorStrategy(RandomNumberGeneratorStrategy.DEFERRED)
5557
.withLegacyOverrides(
5658
LegacyOverrides.newBuilder().withEvaluateMapKeys(evaluateMapKeys).build()
5759
)
@@ -679,6 +681,12 @@ public void itHandlesToday() {
679681
.isEqualTo("'bar' ~ today()");
680682
}
681683

684+
@Test
685+
public void itHandlesRandom() {
686+
assertThat(eagerResolveExpression("range(1)|random").toString())
687+
.isEqualTo("filter:random.filter([0], ____int3rpr3t3r____)");
688+
}
689+
682690
public static void voidFunction(int nothing) {}
683691

684692
public static boolean isNull(Object foo, Object bar) {

0 commit comments

Comments
 (0)