-
Notifications
You must be signed in to change notification settings - Fork 170
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
Defer variables used in deferred nodes #438
Conversation
…les-used-in-deferred
…les-used-in-deferred
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.
The tests look thorough, so I trust that this works as expected. I've got a few othr comments you might want to consider.
private static final Pattern TEMPLATE_TAG_PATTERN = Pattern.compile(TEMPLATE_TAG_REGEX); | ||
|
||
private static final Pattern SET_TAG_PATTERN = Pattern.compile( | ||
"set " + TEMPLATE_TAG_REGEX |
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.
Could you use the name defined here?
public static final String TAG_NAME = "set"; |
import java.util.stream.Collectors; | ||
|
||
public class DeferredValueUtils { | ||
private static final String TEMPLATE_TAG_REGEX = "(\\w+(?:\\.\\w+)*)"; |
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 don't think this will work with multivariable assignments like set myvar1, myvar2, myvar3, myvar4 = ...
I'm not sure you'd want to though as that might make things very complex.
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.
Nice catch. I added this regex to be more specific for looking for sets as we don't have the existing context to compare matches to filter out noise. I think I will change this (and the other usages) to filter out any reserved tokens instead.
|
||
@Test | ||
public void itPutsDeferredVariablesOnParentScopes() { | ||
String template = ""; |
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.
Might be nicer to put these in fixture files like some of the other tests.
|
||
@Test | ||
public void itMarksVariablesUsedAsMapKeysAsDeferred() { | ||
/* {% set week_number = contact.MOweekno %} |
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.
comment needed?
As a follow on to the discussion in the PR here: #421
This PR proposes a solution for two problems: Handling variables used within a deferred block and handling variables used in deferred blocks that occur at lower scopes.
Handling variables used within a deferred block
As discussed in #421 we want a way to mark variables as deferred while we are processing so that any subsequent uses of the variable are not processed. This is achieved here by scanning the deferred node each time and finding variables used within it. These variables are then put back into the context as DeferredValues with their original value. Any subsequent uses of these variables will result in a DeferredValueException and be handled like any other deferred variable. Before adding DeferredValuesUtils this kind of logic only existed in internal HS code -I believe because it is a small bit hacky. Before now, anyone else making use of the deferred functionality would have had to have their own way to rebuild a Deferred Render Context for their 2nd stage render.
I have also added support for finding yet undefined variables used within deferred blocks and marking them as deferred for example
Previously this would have output newVariable as an empty undefined var. Now it will be put in the context as deferred and so will be re-output as {{ newVariable }}
handling variables used in deferred blocks that occur at lower scopes
I have observed issues in using the previous implementation of deferred variables in a HS setup where things are rendered in lower contexts (eg modules) and any deferred values are lost. These changes copy any deferred values to the parent scope, again with their original value.
I can think of some cases where this might go wrong. If you had a template, that imported the same module multiple times, the first instance of the module would muddle the global scope with its values. I don't think this more global scope should cause issues as any code making use of deferred values should be preserved and the order of execution should be consistent. I added handling for deferred values to SetTag to handle this case.