-
Notifications
You must be signed in to change notification settings - Fork 169
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
Allow partial evaluation of templates #282
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
73c2965
POC of deferred evaluation of parts of templates
799d3d2
Handle deferring a whole TagNode
6953416
Add test for preserving function invocations
18e45e3
Move deferred value classes to correct package
4b66b25
Add comments for DeferredValue and DeferredValueException
1032c62
Make tests more clear
95471b6
Also defer evaluation when encountering a random value
af693cb
Remove unused
a9fb902
Update javadocs to use @link
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
import com.hubspot.jinjava.interpret.InterpretException; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import com.hubspot.jinjava.interpret.DeferredValueException; | ||
import com.hubspot.jinjava.lib.tag.Tag; | ||
import com.hubspot.jinjava.tree.output.OutputNode; | ||
import com.hubspot.jinjava.tree.output.RenderedOutputNode; | ||
|
@@ -48,13 +49,14 @@ private TagNode(TagNode n) { | |
|
||
@Override | ||
public OutputNode render(JinjavaInterpreter interpreter) { | ||
|
||
if (interpreter.getContext().isValidationMode() && !tag.isRenderedInValidationMode()) { | ||
return new RenderedOutputNode(""); | ||
} | ||
|
||
try { | ||
return tag.interpretOutput(this, interpreter); | ||
} catch (DeferredValueException e) { | ||
return new RenderedOutputNode(reconstructImage()); | ||
} catch (InterpretException e) { | ||
throw e; | ||
} catch (Exception e) { | ||
|
@@ -84,4 +86,17 @@ public Tag getTag() { | |
return tag; | ||
} | ||
|
||
|
||
private String reconstructImage() { | ||
StringBuilder builder = new StringBuilder().append(master.getImage()); | ||
|
||
for (Node n : getChildren()) { | ||
builder.append(n.getMaster().getImage()); | ||
} | ||
|
||
builder.append("{% ").append(getEndName()). append(" %}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use/create constants for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
|
||
return builder.toString(); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we could store a reference to the original template string and an index value of the start and end n the node object. I think this would solve the raw case and preserve all the whitespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to get that working in a separate PR. If I can get it to work, I'll loop back and update this PR to use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it looks like it might require a larger version bump because I will need to mess around with Tag / Node etc constructors. I'll go ahead with this for now and come back to that as I refine this.