Package com.hubspot.jinjava.util
Class EagerReconstructionUtils
java.lang.Object
com.hubspot.jinjava.util.EagerReconstructionUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringbuildBlockOrInlineSetTag(String name, Object value, JinjavaInterpreter interpreter) static StringbuildBlockOrInlineSetTag(String name, Object value, JinjavaInterpreter interpreter, boolean registerDeferredToken) static StringbuildBlockOrInlineSetTagAndRegisterDeferredToken(String name, Object value, JinjavaInterpreter interpreter) static StringbuildBlockSetTag(String name, String value, JinjavaInterpreter interpreter, boolean registerDeferredToken) Build the image for a blockSetTagand body to preserve the values of an object on the context for a later rendering pass.static StringbuildDoUpdateTag(String name, String updateString, JinjavaInterpreter interpreter) static StringbuildSetTag(Map<String, String> deferredValuesToSet, JinjavaInterpreter interpreter, boolean registerDeferredToken) Build the image for aSetTagwhich preserves the values of objects on the context for a later rendering pass.static voidcommitSpeculativeBindings(JinjavaInterpreter interpreter, EagerExecutionResult result) static PrefixToPreserveStatedeferWordsAndReconstructReferences(JinjavaInterpreter interpreter, Set<String> wordsToDefer) static EagerExecutionResultexecuteInChildContext(Function<JinjavaInterpreter, EagerExpressionResolver.EagerExpressionResult> function, JinjavaInterpreter interpreter, boolean takeNewValue, boolean partialMacroEvaluation, boolean checkForContextChanges) Deprecated.static EagerExecutionResultexecuteInChildContext(Function<JinjavaInterpreter, EagerExpressionResolver.EagerExpressionResult> function, JinjavaInterpreter interpreter, EagerContextWatcher.EagerChildContextConfig eagerChildContextConfig) static StringgetEndLabel(String noteIdentifier, TokenScannerSymbols symbols) static StringgetStartLabel(String noteIdentifier, TokenScannerSymbols symbols) handleDeferredTokenAndReconstructReferences(JinjavaInterpreter interpreter, DeferredToken deferredToken) static PrefixToPreserveStatehydrateBlockOrInlineSetTagRecursively(PrefixToPreserveState prefixToPreserveState, String name, Object value, JinjavaInterpreter interpreter) static PrefixToPreserveStatehydrateReconstructionFromContextBeforeDeferring(PrefixToPreserveState prefixToPreserveState, Set<String> deferredWords, JinjavaInterpreter interpreter) Reconstruct the macro functions and variables from the context before they get deferred.static Booleanstatic StringlabelWithNotes(String body, String noteIdentifier, JinjavaInterpreter interpreter) Surround the with notes to provide identifying information on what is.reconstructDeferredReferences(JinjavaInterpreter interpreter, Set<String> usedDeferredWords) static StringreconstructEnd(TagNode tagNode) static StringreconstructFromContextBeforeDeferring(Set<String> deferredWords, JinjavaInterpreter interpreter) static PrefixToPreserveStatereconstructFromContextBeforeDeferringAsMap(Set<String> deferredWords, JinjavaInterpreter interpreter) static voidreconstructPathAroundBlock(DynamicRenderedOutputNode prefix, OutputList blockValueBuilder, JinjavaInterpreter interpreter) static PrefixToPreserveStateresetAndDeferSpeculativeBindings(JinjavaInterpreter interpreter, EagerExecutionResult eagerExecutionResult) Reset variables to what they were before running the latest execution represented by .resetSpeculativeBindings(JinjavaInterpreter interpreter, EagerExecutionResult result) static StringwrapInAutoEscapeIfNeeded(String output, JinjavaInterpreter interpreter) static StringwrapInChildScope(String toWrap, JinjavaInterpreter interpreter) static StringwrapInRawIfNeeded(String output, JinjavaInterpreter interpreter) static StringwrapInTag(String body, String tagNameToWrap, JinjavaInterpreter interpreter, boolean registerDeferredToken) Wrap the string output in a specified block-type tag.static StringwrapPathAroundText(String text, String newPath, JinjavaInterpreter interpreter)
-
Constructor Details
-
EagerReconstructionUtils
public EagerReconstructionUtils()
-
-
Method Details
-
executeInChildContext
@Deprecated public static EagerExecutionResult executeInChildContext(Function<JinjavaInterpreter, EagerExpressionResolver.EagerExpressionResult> function, JinjavaInterpreter interpreter, boolean takeNewValue, boolean partialMacroEvaluation, boolean checkForContextChanges) Deprecated.UseEagerContextWatcher.executeInChildContext(Function, JinjavaInterpreter, EagerChildContextConfig)Execute the specified functions within a protected context. Additionally, if the execution causes existing values on the context to become deferred, then their previous values will wrapped in asettag that gets prepended to the returned result. Thefunctionis run in deferredExecutionMode=true, where the context needs to be protected from having values updated or set, such as when evaluating both the positive and negative nodes in an if statement.- Parameters:
function- Function to run within a "protected" child contextinterpreter- JinjavaInterpreter to create a child from.takeNewValue- If a value is updated (not replaced) either take the new value or take the previous value and put it into theEagerExecutionResult.prefixToPreserveState.partialMacroEvaluation- Allow macro functions to be partially evaluated rather than needing an explicit result during this render.checkForContextChanges- Set this to be true if executingfunctioncould cause changes to the context. Otherwise, a false value will speed up execution.- Returns:
- An
EagerExecutionResultwhere:resultis the string result offunction.prefixToPreserveStateis either blank or asettag that preserves the state within the output for a second rendering pass.
-
executeInChildContext
public static EagerExecutionResult executeInChildContext(Function<JinjavaInterpreter, EagerExpressionResolver.EagerExpressionResult> function, JinjavaInterpreter interpreter, EagerContextWatcher.EagerChildContextConfig eagerChildContextConfig) -
reconstructFromContextBeforeDeferring
@Deprecated public static String reconstructFromContextBeforeDeferring(Set<String> deferredWords, JinjavaInterpreter interpreter) Deprecated.Reconstruct the macro functions and variables from the context before they get deferred. Those macro functions and variables found withindeferredWordsare reconstructed withMacroTag(s) andSetTag(s), respectively to preserve the context within the Jinjava template itself.- Parameters:
deferredWords- set of words that will need to be deferred based on the previously performed operation.interpreter- the Jinjava interpreter.- Returns:
- a Jinjava-syntax string of 0 or more macro tags and 0 or more set tags.
-
reconstructFromContextBeforeDeferringAsMap
@Deprecated public static PrefixToPreserveState reconstructFromContextBeforeDeferringAsMap(Set<String> deferredWords, JinjavaInterpreter interpreter) Deprecated.Reconstruct the macro functions and variables from the context before they get deferred. Those macro functions and variables found withindeferredWordsare reconstructed withMacroTag(s) andSetTag(s), respectively to preserve the context within the Jinjava template itself.- Parameters:
deferredWords- set of words that will need to be deferred based on the previously performed operation.interpreter- the Jinjava interpreter.- Returns:
- a PrefixToPreserveState map of 0 or more macro tags and 0 or more set tags.
-
hydrateReconstructionFromContextBeforeDeferring
public static PrefixToPreserveState hydrateReconstructionFromContextBeforeDeferring(PrefixToPreserveState prefixToPreserveState, Set<String> deferredWords, JinjavaInterpreter interpreter) Reconstruct the macro functions and variables from the context before they get deferred. Those macro functions and variables found withindeferredWordsare reconstructed withMacroTag(s) andSetTag(s), respectively to preserve the context within the Jinjava template itself.- Parameters:
prefixToPreserveState- This PrefixToPreserveState will be hydrated with the Macro tag images and set tag imagesdeferredWords- set of words that will need to be deferred based on the previously performed operation.interpreter- the Jinjava interpreter.- Returns:
- The PrefixToPreserveState to allow method chaining
-
buildBlockOrInlineSetTag
public static String buildBlockOrInlineSetTag(String name, Object value, JinjavaInterpreter interpreter) -
buildBlockOrInlineSetTagAndRegisterDeferredToken
public static String buildBlockOrInlineSetTagAndRegisterDeferredToken(String name, Object value, JinjavaInterpreter interpreter) -
hydrateBlockOrInlineSetTagRecursively
public static PrefixToPreserveState hydrateBlockOrInlineSetTagRecursively(PrefixToPreserveState prefixToPreserveState, String name, Object value, JinjavaInterpreter interpreter) -
buildBlockOrInlineSetTag
public static String buildBlockOrInlineSetTag(String name, Object value, JinjavaInterpreter interpreter, boolean registerDeferredToken) -
buildSetTag
public static String buildSetTag(Map<String, String> deferredValuesToSet, JinjavaInterpreter interpreter, boolean registerDeferredToken) Build the image for aSetTagwhich preserves the values of objects on the context for a later rendering pass. The set tag will set the keys to the values within thedeferredValuesToSetMap.- Parameters:
deferredValuesToSet- Map that specifies what the context objects should be set to in the returned image.interpreter- The Jinjava interpreter.registerDeferredToken- Whether or not to register the returnedSetTagimage as anDeferredToken.- Returns:
- A jinjava-syntax string that is the image of a set tag that will be executed at a later time.
-
buildBlockSetTag
public static String buildBlockSetTag(String name, String value, JinjavaInterpreter interpreter, boolean registerDeferredToken) Build the image for a blockSetTagand body to preserve the values of an object on the context for a later rendering pass.- Parameters:
name- The name of the variable to set.value- The string value, potentially containing jinja code to put in the set tag block.interpreter- The Jinjava interpreter.registerDeferredToken- Whether to register the returnedSetTagtoken as anDeferredToken.- Returns:
- A jinjava-syntax string that is the image of a block set tag that will be executed at a later time.
-
buildDoUpdateTag
public static String buildDoUpdateTag(String name, String updateString, JinjavaInterpreter interpreter) -
reconstructEnd
-
wrapInRawIfNeeded
-
wrapInAutoEscapeIfNeeded
-
wrapInTag
public static String wrapInTag(String body, String tagNameToWrap, JinjavaInterpreter interpreter, boolean registerDeferredToken) Wrap the string output in a specified block-type tag.- Parameters:
body- The string body to wrap.tagNameToWrap- The name of the tag which will wrap around the .interpreter- The Jinjava interpreter.registerDeferredToken- Whether to register the returned Tag token as anDeferredToken.- Returns:
- A jinjava-syntax string that is the image of a block set tag that will be executed at a later time.
-
labelWithNotes
public static String labelWithNotes(String body, String noteIdentifier, JinjavaInterpreter interpreter) Surround the with notes to provide identifying information on what is. If isfooand is{{ bar }}, the result will be:{# foo #}{{ bar }}{# endfoo #}- Parameters:
body- The string body to wrap.noteIdentifier- The identifier for the note.interpreter- The Jinjava interpreter.- Returns:
- A block surrounded with labelled notes
-
getStartLabel
-
getEndLabel
-
wrapInChildScope
-
isDeferredExecutionMode
-
deferWordsAndReconstructReferences
public static PrefixToPreserveState deferWordsAndReconstructReferences(JinjavaInterpreter interpreter, Set<String> wordsToDefer) -
handleDeferredTokenAndReconstructReferences
public static Map<String,String> handleDeferredTokenAndReconstructReferences(JinjavaInterpreter interpreter, DeferredToken deferredToken) -
reconstructDeferredReferences
-
resetAndDeferSpeculativeBindings
public static PrefixToPreserveState resetAndDeferSpeculativeBindings(JinjavaInterpreter interpreter, EagerExecutionResult eagerExecutionResult) Reset variables to what they were before running the latest execution represented by . Then re-defer those variables and reconstruct deferred lazy references to them. This method is needed in 2 circumstances:* When doing some eager execution and then needing to repeat the same execution in deferred execution mode.
* When rendering logic which takes place in its own child scope (for tag, macro function, set block) and there are speculative bindings. These must be deferred and the execution must run again, so they don't get reconstructed within the child scope, and can instead be reconstructed in their original scopes.
- Parameters:
interpreter- The JinjavaInterpretereagerExecutionResult- The execution result which contains information about which bindings were modified during the execution.- Returns:
-
resetSpeculativeBindings
public static Set<String> resetSpeculativeBindings(JinjavaInterpreter interpreter, EagerExecutionResult result) -
commitSpeculativeBindings
public static void commitSpeculativeBindings(JinjavaInterpreter interpreter, EagerExecutionResult result) -
reconstructPathAroundBlock
public static void reconstructPathAroundBlock(DynamicRenderedOutputNode prefix, OutputList blockValueBuilder, JinjavaInterpreter interpreter) -
wrapPathAroundText
public static String wrapPathAroundText(String text, String newPath, JinjavaInterpreter interpreter)
-
EagerContextWatcher.executeInChildContext(Function, JinjavaInterpreter, EagerChildContextConfig)Execute the specified functions within a protected context.