Skip to content

Commit 8926302

Browse files
committed
feat(poet): add Defer, Assign, and CallStmt statement types
Add new statement AST nodes: - Defer: defer statements (defer expr) - Assign: assignment statements (left op right) - CallStmt: function call statements Update generator.go to use structured statements: - Convert RawStmt assignments to poet.Assign - Eliminates manual tab management for these statements The rendering handles indentation automatically, making the code more maintainable and less error-prone.
1 parent 17bedc0 commit 8926302

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

internal/codegen/golang/generator.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,19 @@ func (g *CodeGenerator) addModelsCode(f *poet.File) {
421421
{
422422
Values: []string{"[]byte"},
423423
Body: []poet.Stmt{
424-
poet.RawStmt{Code: fmt.Sprintf("\t\t*e = %s(s)\n", enum.Name)},
424+
poet.Assign{
425+
Left: []string{"*e"}, Op: "=",
426+
Right: []string{fmt.Sprintf("%s(s)", enum.Name)},
427+
},
425428
},
426429
},
427430
{
428431
Values: []string{"string"},
429432
Body: []poet.Stmt{
430-
poet.RawStmt{Code: fmt.Sprintf("\t\t*e = %s(s)\n", enum.Name)},
433+
poet.Assign{
434+
Left: []string{"*e"}, Op: "=",
435+
Right: []string{fmt.Sprintf("%s(s)", enum.Name)},
436+
},
431437
},
432438
},
433439
{
@@ -479,11 +485,15 @@ func (g *CodeGenerator) addModelsCode(f *poet.File) {
479485
poet.If{
480486
Cond: "value == nil",
481487
Body: []poet.Stmt{
482-
poet.RawStmt{Code: fmt.Sprintf("\t\tns.%s, ns.Valid = \"\", false\n", enum.Name)},
488+
poet.Assign{
489+
Left: []string{"ns." + enum.Name, "ns.Valid"},
490+
Op: "=",
491+
Right: []string{`""`, "false"},
492+
},
483493
poet.Return{Values: []string{"nil"}},
484494
},
485495
},
486-
poet.RawStmt{Code: "\tns.Valid = true\n"},
496+
poet.Assign{Left: []string{"ns.Valid"}, Op: "=", Right: []string{"true"}},
487497
poet.Return{Values: []string{fmt.Sprintf("ns.%s.Scan(value)", enum.Name)}},
488498
},
489499
})
@@ -1352,11 +1362,14 @@ func (g *CodeGenerator) addCopyFromCodePGX(f *poet.File) {
13521362
poet.If{
13531363
Cond: "!r.skippedFirstNextCall",
13541364
Body: []poet.Stmt{
1355-
poet.RawStmt{Code: "\t\tr.skippedFirstNextCall = true\n"},
1365+
poet.Assign{
1366+
Left: []string{"r.skippedFirstNextCall"}, Op: "=",
1367+
Right: []string{"true"},
1368+
},
13561369
poet.Return{Values: []string{"true"}},
13571370
},
13581371
},
1359-
poet.RawStmt{Code: "\tr.rows = r.rows[1:]\n"},
1372+
poet.Assign{Left: []string{"r.rows"}, Op: "=", Right: []string{"r.rows[1:]"}},
13601373
poet.Return{Values: []string{"len(r.rows) > 0"}},
13611374
},
13621375
})
@@ -1697,7 +1710,7 @@ func (g *CodeGenerator) addBatchCodePGX(f *poet.File) {
16971710
Name: "Close",
16981711
Results: []poet.Param{{Type: "error"}},
16991712
Stmts: []poet.Stmt{
1700-
poet.RawStmt{Code: "\tb.closed = true\n"},
1713+
poet.Assign{Left: []string{"b.closed"}, Op: "=", Right: []string{"true"}},
17011714
poet.Return{Values: []string{"b.br.Close()"}},
17021715
},
17031716
})

internal/poet/ast.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,26 @@ type Case struct {
188188
Values []string // Case values (empty for default case)
189189
Body []Stmt
190190
}
191+
192+
// Defer represents a defer statement.
193+
type Defer struct {
194+
Call string // The function call to defer
195+
}
196+
197+
func (Defer) isStmt() {}
198+
199+
// Assign represents an assignment statement.
200+
type Assign struct {
201+
Left []string // Left-hand side (variable names)
202+
Op string // Assignment operator: "=", ":=", "+=", etc.
203+
Right []string // Right-hand side (expressions)
204+
}
205+
206+
func (Assign) isStmt() {}
207+
208+
// CallStmt represents a function call as a statement.
209+
type CallStmt struct {
210+
Call string // The function call expression
211+
}
212+
213+
func (CallStmt) isStmt() {}

internal/poet/render.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ func renderStmt(b *strings.Builder, s Stmt, indent string) {
307307
renderIf(b, s, indent)
308308
case Switch:
309309
renderSwitch(b, s, indent)
310+
case Defer:
311+
renderDefer(b, s, indent)
312+
case Assign:
313+
renderAssign(b, s, indent)
314+
case CallStmt:
315+
renderCallStmt(b, s, indent)
310316
}
311317
}
312318

@@ -403,3 +409,36 @@ func renderSwitch(b *strings.Builder, s Switch, indent string) {
403409
b.WriteString(indent)
404410
b.WriteString("}\n")
405411
}
412+
413+
func renderDefer(b *strings.Builder, d Defer, indent string) {
414+
b.WriteString(indent)
415+
b.WriteString("defer ")
416+
b.WriteString(d.Call)
417+
b.WriteString("\n")
418+
}
419+
420+
func renderAssign(b *strings.Builder, a Assign, indent string) {
421+
b.WriteString(indent)
422+
for i, l := range a.Left {
423+
if i > 0 {
424+
b.WriteString(", ")
425+
}
426+
b.WriteString(l)
427+
}
428+
b.WriteString(" ")
429+
b.WriteString(a.Op)
430+
b.WriteString(" ")
431+
for i, r := range a.Right {
432+
if i > 0 {
433+
b.WriteString(", ")
434+
}
435+
b.WriteString(r)
436+
}
437+
b.WriteString("\n")
438+
}
439+
440+
func renderCallStmt(b *strings.Builder, c CallStmt, indent string) {
441+
b.WriteString(indent)
442+
b.WriteString(c.Call)
443+
b.WriteString("\n")
444+
}

0 commit comments

Comments
 (0)