MCPcopy
hub / github.com/MadAppGang/dingo / Generate

Method Generate

pkg/codegen/match.go:65–126  ·  view source on GitHub ↗

Constructor is in expr.go to avoid import cycles Generate generates Go code for the match expression. Returns CodeGenResult with generated code and source mappings.

()

Source from the content-addressed store, hash-verified

63// Generate generates Go code for the match expression.
64// Returns CodeGenResult with generated code and source mappings.
65func (g *MatchCodeGen) Generate() ast.CodeGenResult {
66 if g.Match == nil {
67 return ast.CodeGenResult{}
68 }
69
70 // CRITICAL: Check for Option/Result patterns FIRST before any other processing
71 // Option and Result are structs, not interfaces, so they need method-based code
72 if info := g.detectOptionResult(); info != nil {
73 return g.generateOptionResultMatch(info)
74 }
75
76 // Check for value enum patterns SECOND
77 // Value enums are typed constants, so they use simple switch (not type switch)
78 if info := g.detectValueEnum(); info != nil {
79 // Check exhaustiveness for expression matches
80 if g.Match.IsExpr {
81 if errResult := g.checkValueEnumExhaustiveness(info); errResult.Error != nil {
82 return errResult
83 }
84 }
85 return g.generateValueEnumMatch(info)
86 }
87
88 // Check exhaustiveness for expression matches (sum types)
89 if g.Match.IsExpr {
90 if errResult := g.checkExhaustiveness(); len(errResult.Output) > 0 {
91 // Error result has non-empty Output containing error code
92 return errResult
93 }
94 }
95
96 // Check for assignment context - can generate temp var pattern
97 if g.Match.IsExpr && g.Context != nil && g.Context.Context == ast.ContextAssignment {
98 return g.generateHumanLikeAssignment()
99 }
100
101 // Check for argument context - can hoist switch before function call
102 if g.Match.IsExpr && g.Context != nil && g.Context.Context == ast.ContextArgument {
103 return g.generateHoistedMatch()
104 }
105
106 // Check for return context - can unwrap IIFE for human-like output
107 if g.Match.IsExpr && g.Context != nil && g.Context.Context == ast.ContextReturn {
108 // Return context: unwrap IIFE, generate switch with direct returns
109 // This produces more idiomatic Go code
110 g.generateMatchSwitch()
111
112 // Move output to StatementOutput for statement-level replacement
113 result := g.Result()
114 result.StatementOutput = result.Output
115 result.Output = nil
116 return result
117 } else if g.Match.IsExpr {
118 // Other expression contexts (no context or unknown): wrap in IIFE
119 g.generateMatchIIFE()
120 } else {
121 // Statement context: no IIFE needed
122 g.generateMatchSwitch()

Calls 11

detectOptionResultMethod · 0.95
detectValueEnumMethod · 0.95
checkExhaustivenessMethod · 0.95
generateHoistedMatchMethod · 0.95
generateMatchSwitchMethod · 0.95
generateMatchIIFEMethod · 0.95
ResultMethod · 0.80