Skip to content

Reconstruct nested interpretation variables in string representation#1088

Merged
jasmith-hs merged 5 commits intomasterfrom
reconstruct-nested-in-string-representation
Jul 13, 2023
Merged

Reconstruct nested interpretation variables in string representation#1088
jasmith-hs merged 5 commits intomasterfrom
reconstruct-nested-in-string-representation

Conversation

@jasmith-hs
Copy link
Copy Markdown
Contributor

If a variable is reconstructed, but it implicitly depends on another variable due to the use of nested interpretation, we'll need to reconstruct the dependent variable if nested interpretation is enabled.
This is well demonstrated through the new test:

{% set i_am = 'I am' %}
{% set foo = '{{ i_am }} foo' %}
{% set bar = '{{ i_am }} bar' %}
{% set map_with_vals = {'foo': 'Foo is {{ foo }}.'} %}
{% if deferred %}
{% do map_with_vals.put('bar', 'Bar is {{ bar }}.') %}
{% endif %}
map.foo: {{ map_with_vals.foo }}
map.bar: {{ map_with_vals.bar }}

foo and bar are not used explicitly anywhere, but due to nested interpretation, they are implicitly referenced. We can determine these references using a recursive approach with the same depth limit as the depth limit for nested interpretation.

Essentially, when reconstructing a set tag for a variable which may have a string representation which uses nested interpretation, we can use EagerExpressionResolver#findDeferredWords to find words which are hidden inside of nested interpretation and also reconstruct them. So, with the above example, when trying to reconstruct foo, we realise that there's also a dependency on i_am, so we'll reconstruct i_am before the reconstruction of foo. It would look something like this:

{% set i_am = 'I am' %}{% set foo = '{{ i_am }}' %}

Comment on lines +393 to +416
String pyishStringRepresentation = PyishObjectMapper.getAsPyishString(value);

if (
depth < interpreter.getConfig().getMaxRenderDepth() &&
interpreter.getConfig().isNestedInterpretationEnabled()
) {
Set<String> dependentWords = EagerExpressionResolver.findDeferredWords(
pyishStringRepresentation,
interpreter
);
if (!dependentWords.isEmpty()) {
hydrateReconstructionFromContextBeforeDeferring(
prefixToPreserveState,
dependentWords,
interpreter,
depth + 1
);
}
}
prefixToPreserveState.put(
name,
buildSetTag(ImmutableMap.of(name, pyishStringRepresentation), interpreter, false)
);
return prefixToPreserveState;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Here's the recursion part

@jasmith-hs jasmith-hs force-pushed the reconstruct-nested-in-string-representation branch from 998d4b7 to 8d879ea Compare June 28, 2023 20:40
@jasmith-hs jasmith-hs merged commit 63faa33 into master Jul 13, 2023
@jasmith-hs jasmith-hs deleted the reconstruct-nested-in-string-representation branch July 13, 2023 12:19
jasmith-hs added a commit that referenced this pull request Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant