Skip to content
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

Merged
merged 16 commits into from
May 14, 2020
Merged

Conversation

Joeoh
Copy link
Contributor

@Joeoh Joeoh commented May 5, 2020

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

{% if deferred = 'foo' %} 
{% set newVariable = 'deferred was foo' %}
{% else %}
{% set newVariable = 'deferred was not foo' %}
{% endif %}
{{ newVariable }}

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.

Copy link
Contributor

@boulter boulter left a 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
Copy link
Contributor

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+)*)";
Copy link
Contributor

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.

Copy link
Contributor Author

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 = "";
Copy link
Contributor

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 %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment needed?

@Joeoh Joeoh merged commit 424d245 into master May 14, 2020
@Joeoh Joeoh deleted the defer-variables-used-in-deferred branch May 14, 2020 15:35
Joeoh added a commit that referenced this pull request May 15, 2020
This was referenced May 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants