generateGroupedCases groups arms by pattern type and generates cases. This is critical for handling guarded patterns: - Multiple arms for the same constructor become if/else chain in one case - Avoids duplicate case clauses which are illegal in Go
(typeSwitch bool)
| 203 | // - Multiple arms for the same constructor become if/else chain in one case |
| 204 | // - Avoids duplicate case clauses which are illegal in Go |
| 205 | func (g *MatchCodeGen) generateGroupedCases(typeSwitch bool) { |
| 206 | // Group arms by their case key (type name for constructors, "default" for wildcards) |
| 207 | groups := make(map[string]*armGroup) |
| 208 | var order []string // Preserve order |
| 209 | |
| 210 | for _, arm := range g.Match.Arms { |
| 211 | key := g.getPatternCaseKey(arm.Pattern) |
| 212 | |
| 213 | if _, exists := groups[key]; !exists { |
| 214 | groups[key] = &armGroup{ |
| 215 | typeName: key, |
| 216 | isDefault: key == "default", |
| 217 | } |
| 218 | order = append(order, key) |
| 219 | } |
| 220 | groups[key].arms = append(groups[key].arms, arm) |
| 221 | } |
| 222 | |
| 223 | // Generate case for each group |
| 224 | for _, key := range order { |
| 225 | group := groups[key] |
| 226 | g.generateGroupedCase(group, typeSwitch) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // getPatternCaseKey returns the case key for a pattern. |
| 231 | // Constructor patterns return their full type name. |
no test coverage detected