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

Fix issues with AbstractCodeWriter state stacks #2142

Merged
merged 1 commit into from
Feb 19, 2024

Conversation

mtdowling
Copy link
Member

Code writer context inheritance wasn't working in the previous implementation of the stack based inheritance system. We were able to resolve context from the current state and parent state (because of eagerly copying states and flattening them per/state), but we weren't able to look at parent states that used a CodeSection. Methods of a CodeSection state can be accessed in named template labels. However, when another state is pushed after setting a CodeSection value for a state, code writer wasn't crawling the state stack to see if previous CodeSections could provide a value for a named label that wasn't explicitly set as parameter in the context map. This was because instead of looking up values lazily by iterating over the state stack, we were making eager copies of different pieces of states. This matters when using things like for loops in templates that push an implicit state to capture the current loop iteration state.

This commit changes our approach to now actually use the stack itself and iterate over states when attempting to resolve context keys, formatters, iterators, and CodeSection values.

Some tests were added to ensure the behavior of the writer is the same. For example, when using inheritance based lookups, removing a context key now means we need to set a context key to an explicit null value to emulate the previous behavior. If we didn't do this, and we just called Map#remove(), the context key may have been set by a parent state, making the remove call do nothing (whereas the previous implementation would actually take effect).

Background

  • What do these changes do?
  • Why are they important?

Testing

  • How did you test these changes?

Links

  • Links to additional context, if necessary
  • Issue #, if applicable (see here for a list of keywords to use for linking issues)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Code writer context inheritance wasn't working in the previous
implementation of the stack based inheritance system. We were able to
resolve context from the current state and parent state (because of
eagerly copying states and flattening them per/state), but we weren't
able to look at parent states that used a CodeSection. Methods of a
CodeSection state can be accessed in named template labels. However,
when another state is pushed after setting a CodeSection value for a
state, code writer wasn't crawling the state stack to see if previous
CodeSections could provide a value for a named label that wasn't
explicitly set as parameter in the context map. This was because
instead of looking up values lazily by iterating over the state stack,
we were making eager copies of different pieces of states. This
matters when using things like for loops in templates that push an
implicit state to capture the current loop iteration state.

This commit changes our approach to now actually use the stack itself
and iterate over states when attempting to resolve context keys,
formatters, iterators, and CodeSection values.

Some tests were added to ensure the behavior of the writer is the
same. For example, when using inheritance based lookups, removing a
context key now means we need to set a context key to an explicit null
value to emulate the previous behavior. If we didn't do this, and we
just called Map#remove(), the context key may have been set by a parent
state, making the remove call do nothing (whereas the previous
implementation would actually take effect).
@mtdowling mtdowling requested a review from a team as a code owner February 15, 2024 20:00
@mtdowling mtdowling requested a review from sugmanue February 15, 2024 20:00
@mtdowling mtdowling merged commit a57f585 into main Feb 19, 2024
11 checks passed
@sugmanue sugmanue deleted the fix-state-inheritance branch February 19, 2024 19:52
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.

2 participants