Skip to content

Commit 1cdc4ec

Browse files
committed
refactor(compiler): replace panic with error returns in toColumn
Convert toColumn function from using panic() to returning errors, improving error handling for invalid SQL type names. This allows callers to handle errors gracefully instead of crashing. Changes: - toColumn now returns (*Column, error) instead of *Column - Updated 3 call sites in resolve.go and output_columns.go
1 parent 67e865b commit 1cdc4ec

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

internal/compiler/output_columns.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
201201
name = *res.Name
202202
}
203203
// TODO Validate column names
204-
col := toColumn(tc.TypeName)
204+
col, err := toColumn(tc.TypeName)
205+
if err != nil {
206+
return nil, err
207+
}
205208
col.Name = name
206209
cols = append(cols, col)
207210
} else if aconst, ok := n.Defresult.(*ast.A_Const); ok {
@@ -358,7 +361,10 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
358361
name = *res.Name
359362
}
360363
// TODO Validate column names
361-
col := toColumn(n.TypeName)
364+
col, err := toColumn(n.TypeName)
365+
if err != nil {
366+
return nil, err
367+
}
362368
col.Name = name
363369
// TODO Add correct, real type inference
364370
if constant, ok := n.Arg.(*ast.A_Const); ok {

internal/compiler/resolve.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,10 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
511511
if n.TypeName == nil {
512512
return nil, fmt.Errorf("*ast.TypeCast has nil type name")
513513
}
514-
col := toColumn(n.TypeName)
514+
col, err := toColumn(n.TypeName)
515+
if err != nil {
516+
return nil, err
517+
}
515518
defaultP := named.NewInferredParam(col.Name, col.NotNull)
516519
p, _ := params.FetchMerge(ref.ref.Number, defaultP)
517520

internal/compiler/to_column.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package compiler
22

33
import (
4+
"fmt"
45
"strings"
56

67
"github.com/sqlc-dev/sqlc/internal/sql/ast"
@@ -14,13 +15,13 @@ func arrayDims(n *ast.TypeName) int {
1415
return len(n.ArrayBounds.Items)
1516
}
1617

17-
func toColumn(n *ast.TypeName) *Column {
18+
func toColumn(n *ast.TypeName) (*Column, error) {
1819
if n == nil {
19-
panic("can't build column for nil type name")
20+
return nil, fmt.Errorf("can't build column for nil type name")
2021
}
2122
typ, err := ParseTypeName(n)
2223
if err != nil {
23-
panic("toColumn: " + err.Error())
24+
return nil, fmt.Errorf("toColumn: %w", err)
2425
}
2526
arrayDims := arrayDims(n)
2627
return &Column{
@@ -29,5 +30,5 @@ func toColumn(n *ast.TypeName) *Column {
2930
NotNull: true, // XXX: How do we know if this should be null?
3031
IsArray: arrayDims > 0,
3132
ArrayDims: arrayDims,
32-
}
33+
}, nil
3334
}

0 commit comments

Comments
 (0)