generateGroupedCase generates a single case clause for a group of arms. If the group has multiple arms, generates if/else chain for guards.
(group *armGroup, typeSwitch bool)
| 246 | // generateGroupedCase generates a single case clause for a group of arms. |
| 247 | // If the group has multiple arms, generates if/else chain for guards. |
| 248 | func (g *MatchCodeGen) generateGroupedCase(group *armGroup, typeSwitch bool) { |
| 249 | if len(group.arms) == 0 { |
| 250 | return |
| 251 | } |
| 252 | |
| 253 | firstArm := group.arms[0] |
| 254 | |
| 255 | // Generate case clause header |
| 256 | if group.isDefault { |
| 257 | g.Write("default:\n") |
| 258 | } else if lit, ok := firstArm.Pattern.(*ast.LiteralPattern); ok { |
| 259 | // Literal pattern |
| 260 | g.Write("case " + lit.Value + ":\n") |
| 261 | } else { |
| 262 | // Constructor pattern |
| 263 | g.Write("case " + group.typeName + ":\n") |
| 264 | } |
| 265 | |
| 266 | // Extract bindings from first arm (all arms in group have same pattern structure) |
| 267 | var bindings []Binding |
| 268 | if cp, ok := firstArm.Pattern.(*ast.ConstructorPattern); ok { |
| 269 | numParams := len(cp.Params) |
| 270 | isTupleVariant := g.isTupleVariant(group.typeName) |
| 271 | for i, param := range cp.Params { |
| 272 | bindings = append(bindings, g.extractBindings(param, i, numParams, isTupleVariant)...) |
| 273 | } |
| 274 | } else if vp, ok := firstArm.Pattern.(*ast.VariablePattern); ok { |
| 275 | bindings = []Binding{{Name: vp.Name, FieldPath: ""}} |
| 276 | } |
| 277 | |
| 278 | // Generate bindings once (they're the same for all arms in this group) |
| 279 | for _, binding := range bindings { |
| 280 | bindingCode := binding.Name + " := " |
| 281 | if binding.FieldPath != "" { |
| 282 | tempVar := g.scrutineeTempVar |
| 283 | if tempVar == "" { |
| 284 | tempVar = "v" |
| 285 | } |
| 286 | bindingCode += tempVar + "." + binding.FieldPath |
| 287 | } else { |
| 288 | // Use scrutinee temp var to avoid double evaluation |
| 289 | tempVar := g.scrutineeTempVar |
| 290 | if tempVar == "" { |
| 291 | // Fallback: generate expression (shouldn't happen in normal flow) |
| 292 | scrutineeResult := GenerateExpr(g.Match.Scrutinee) |
| 293 | bindingCode += string(scrutineeResult.Output) |
| 294 | } else { |
| 295 | bindingCode += tempVar |
| 296 | } |
| 297 | } |
| 298 | bindingCode += "\n" |
| 299 | g.Write(bindingCode) |
| 300 | } |
| 301 | |
| 302 | // Generate if/else chain for multiple arms (guards) |
| 303 | g.generateArmsChain(group.arms) |
| 304 | } |
| 305 |
no test coverage detected