Skip to content

Support mixed when/else blocks in case statements - #875

Merged
sebastienros merged 6 commits into
mainfrom
copilot/fix-case-when-evaluation
Nov 20, 2025
Merged

sebastienros merged 6 commits into
mainfrom
copilot/fix-case-when-evaluation

Conversation

Copilot AI commented Nov 20, 2025 •

Copy link
Copy Markdown
Contributor

Case/When Evaluation Issues Fix

This PR fixes the case/when evaluation to support multiple when and else blocks in any order, as described in the issue.

Summary of Changes:

1. New Data Model (CaseBlock.cs)

  • Created CaseBlock abstract base class
  • Implemented WhenBlock for when clauses with options
  • Implemented ElseBlock for else clauses

2. Updated CaseStatement.cs

  • Removed old constructor accepting ElseStatement and WhenStatement[]
  • Simplified to single constructor accepting CaseBlock[]
  • Updated evaluation logic:
    • All matching when blocks are evaluated (not just first match)
    • Else blocks execute only if no when has matched before them
    • Multiple matching options in a when block trigger multiple executions

3. Updated FluidParser.cs

  • Modified parser to accept when/else blocks in any order
  • Uses new WhenBlock and ElseBlock parsers that can be mixed

4. Updated AstVisitor.cs and AstRewriter.cs

  • Simplified to only handle new block structure
  • Removed backward compatibility code

5. Updated Tests (CaseStatementTests.cs)

  • Converted all tests to use new WhenBlock and ElseBlock types
  • Tests for mixed when/else blocks
  • Tests for multiple else blocks
  • Tests for when blocks before and after else
  • Tests from golden-liquid reference implementation
  • All scenarios from the issue validated

Verification:

✅ Issue Example 1 (no match): Returns all else blocks
✅ Issue Example 2 (first whens match): Returns only matched when blocks
✅ All 1356 tests pass
✅ No backward compatibility needed - cleaner implementation

Original prompt

This section details on the original issue you should resolve

<issue_title>case/when evaluation issues</issue_title>
<issue_description>The expected order or case/when evaluation is cryptic.
The only thing obvious is that when/else can be mixed and the parser and model need to be updated.

{%  case 'x' %}
  {% when 'y' %}match1
  {% when 'y' %}match2
  {% else %} else1
  {% else %} else2
  {% when 'y' %}match3
  {% when 'y' %}match4
  {% else %} else3
  {% else %} else4
{% endcase %}

should return

else1
   else2
   else3
   else4

and

{%  case 'x' %}
  {% when 'x' %}match1
  {% when 'x' %}match2
  {% else %} else1
  {% else %} else2
  {% when 'y' %}match3
  {% when 'y' %}match4
  {% else %} else3
  {% else %} else4
{% endcase %}

return

match1
  match2

</issue_description>

<agent_instructions>Update the parser and statement for case such that multiple when and else statements can be provided in order. Change the evaluation logic such that the provided examples are valid. You can check the file https://raw.githubusercontent.com/jg-rp/golden-liquid/refs/heads/main/tests/tags/case.json for more tests.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits November 20, 2025 16:39
Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix case/when evaluation issues in the parser Support mixed when/else blocks in case statements Nov 20, 2025
Copilot AI requested a review from sebastienros November 20, 2025 16:50
@sebastienros

Copy link
Copy Markdown
Owner

@copilot there is no need to keep the previous CaseStatement constructor for backward compatibility.

Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
@sebastienros
sebastienros marked this pull request as ready for review November 20, 2025 17:06
@sebastienros
sebastienros enabled auto-merge (squash) November 20, 2025 17:07
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.

case/when evaluation issues

2 participants