-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow partial evaluation of templates (#282)
* Defer evaluation of parts of templates * Handle deferring a whole TagNode * Add test for preserving function invocations * Also defer evaluation when encountering a random value * Update javadocs to use @link
- Loading branch information
Showing
8 changed files
with
310 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/hubspot/jinjava/interpret/DeferredValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.hubspot.jinjava.interpret; | ||
|
||
/** | ||
* Marker object which indicates that the template engine should skip over evaluating | ||
* this part of the template, if the object is resolved from the context. | ||
* | ||
*/ | ||
public class DeferredValue { | ||
private static final DeferredValue INSTANCE = new DeferredValue(); | ||
|
||
private DeferredValue() { | ||
} | ||
|
||
public static DeferredValue instance() { | ||
return INSTANCE; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/hubspot/jinjava/interpret/DeferredValueException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.hubspot.jinjava.interpret; | ||
|
||
/** | ||
* Exception thrown when attempting to render a {@link com.hubspot.jinjava.interpret.DeferredValue}. | ||
* The exception is effectively used for flow control, to unwind evaluating a template Node | ||
* and instead echo its contents to the output. | ||
*/ | ||
public class DeferredValueException extends InterpretException { | ||
public DeferredValueException(String message) { | ||
super("Encountered a deferred value: " + message); | ||
} | ||
|
||
public DeferredValueException(String variable, int lineNumber, int startPosition) { | ||
super("Encountered a deferred value: \"" + variable + "\"", lineNumber, startPosition); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/java/com/hubspot/jinjava/random/DeferredRandomNumberGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.hubspot.jinjava.random; | ||
|
||
import java.util.Random; | ||
import java.util.stream.DoubleStream; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.LongStream; | ||
|
||
import com.hubspot.jinjava.interpret.DeferredValueException; | ||
|
||
/** | ||
* A random number generator that throws {@link com.hubspot.jinjava.interpret.DeferredValueException} for all supported methods. | ||
*/ | ||
public class DeferredRandomNumberGenerator extends Random { | ||
|
||
private static final String EXCEPTION_MESSAGE = "Generating random number"; | ||
|
||
@Override | ||
protected int next(int bits) { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public int nextInt() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public int nextInt(int bound) { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public long nextLong() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public boolean nextBoolean() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public float nextFloat() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public double nextDouble() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public synchronized double nextGaussian() { | ||
throw new DeferredValueException(EXCEPTION_MESSAGE); | ||
} | ||
|
||
@Override | ||
public void nextBytes(byte[] bytes) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public IntStream ints(long streamSize) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public IntStream ints() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public IntStream ints(int randomNumberOrigin, int randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public LongStream longs(long streamSize) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public LongStream longs() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public LongStream longs(long randomNumberOrigin, long randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public DoubleStream doubles(long streamSize) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public DoubleStream doubles() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
|
||
public enum RandomNumberGeneratorStrategy { | ||
THREAD_LOCAL, | ||
CONSTANT_ZERO | ||
CONSTANT_ZERO, | ||
DEFERRED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.