codegen: use must.Be helper in Save function

This commit is contained in:
2026-09-08 18:18:31 -07:00
parent 49d2a7748f
commit a1b06a3ef8

View File

@@ -53,20 +53,48 @@ func GoTypeForColumn(c schema.Column) ast.Expr {
} }
} }
func PanicIfRowsAffected(tbl schema.Table) *ast.IfStmt { // MustBeRowsAffected produces an AST for a `must.Be(...)` call asserting that exactly one row
return &ast.IfStmt{ // was affected by the preceding statement, e.g.:
Cond: &ast.BinaryExpr{ //
// must.Be(must.Get(result.RowsAffected()) == 1, "%w: Food ID=%d", ErrNotInDB, f.ID)
//
// For "without rowid" tables, the message includes the table's primary key column(s) instead of ID.
func MustBeRowsAffected(tbl schema.Table) *ast.ExprStmt {
pkParts := []string{}
pkArgs := []ast.Expr{}
if tbl.IsWithoutRowid {
for _, col := range tbl.PrimaryKeyColumns() {
verb := "%v"
if col.Type == "integer" || col.Type == "int" || col.IsNonCodeTableForeignKey() {
verb = "%d"
}
pkParts = append(pkParts, fmt.Sprintf("%s=%s", col.Name, verb))
pkArgs = append(pkArgs, &ast.SelectorExpr{X: ast.NewIdent(tbl.VarName), Sel: ast.NewIdent(col.GoFieldName())})
}
} else {
pkParts = append(pkParts, "ID=%d")
pkArgs = append(pkArgs, &ast.SelectorExpr{X: ast.NewIdent(tbl.VarName), Sel: ast.NewIdent("ID")})
}
msg := fmt.Sprintf("%%w: %s %s", tbl.GoTypeName, strings.Join(pkParts, ", "))
args := append([]ast.Expr{
&ast.BinaryExpr{
X: mustCall(&ast.CallExpr{ X: mustCall(&ast.CallExpr{
Fun: &ast.SelectorExpr{X: ast.NewIdent("result"), Sel: ast.NewIdent("RowsAffected")}, Fun: &ast.SelectorExpr{X: ast.NewIdent("result"), Sel: ast.NewIdent("RowsAffected")},
Args: []ast.Expr{}, Args: []ast.Expr{},
}), }),
Op: token.NEQ, Op: token.EQL,
Y: &ast.BasicLit{Kind: token.INT, Value: "1"}, Y: &ast.BasicLit{Kind: token.INT, Value: "1"},
}, },
Body: &ast.BlockStmt{List: []ast.Stmt{ &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf("%q", msg)},
&ast.ExprStmt{X: &ast.CallExpr{Fun: ast.NewIdent("panic"), Args: []ast.Expr{ast.NewIdent(tbl.VarName)}}}, ast.NewIdent("ErrNotInDB"),
}}, }, pkArgs...)
}
return &ast.ExprStmt{X: &ast.CallExpr{
Fun: &ast.SelectorExpr{X: ast.NewIdent("must"), Sel: ast.NewIdent("Be")},
Args: args,
}}
} }
// --------------- // ---------------
@@ -418,7 +446,7 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
}) })
} }
ret = append(ret, namedExecStmt(upsertStmt)...) ret = append(ret, namedExecStmt(upsertStmt)...)
ret = append(ret, PanicIfRowsAffected(tbl)) ret = append(ret, MustBeRowsAffected(tbl))
} else { } else {
// if item.ID == 0 {...} else {...} // if item.ID == 0 {...} else {...}
ret = append(ret, &ast.IfStmt{ ret = append(ret, &ast.IfStmt{
@@ -472,7 +500,7 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
[]ast.Stmt{Comment("Do update")}, []ast.Stmt{Comment("Do update")},
append( append(
namedExecStmt(updateStmt), namedExecStmt(updateStmt),
PanicIfRowsAffected(tbl), MustBeRowsAffected(tbl),
)..., )...,
), ),
}, },
@@ -720,7 +748,7 @@ func GenerateDeleteItemFunc(tbl schema.Table) *ast.FuncDecl {
}, },
})}, })},
}, },
PanicIfRowsAffected(tbl), MustBeRowsAffected(tbl),
}, },
} }