codegen: add ability to insert comments and blank lines into the generated code
Some checks failed
CI / build-docker (push) Successful in 4s
CI / build-docker-bootstrap (push) Has been skipped
CI / release-test (push) Failing after 12s

This commit is contained in:
2026-02-15 14:54:22 -08:00
parent 8ab21edae9
commit d6426bba14
3 changed files with 197 additions and 24 deletions

View File

@@ -240,7 +240,7 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
List: func() []ast.Stmt {
ret := []ast.Stmt{}
if hasFks {
ret = append(ret, checkForeignKeyFailuresAssignment)
ret = append(ret, checkForeignKeyFailuresAssignment, BlankLine())
}
if hasUpdatedAt {
// Auto-timestamps: updated_at
@@ -261,7 +261,7 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
// Do create
List: append(
func() []ast.Stmt {
ret1 := []ast.Stmt{}
ret1 := []ast.Stmt{Comment("Do create")}
if hasCreatedAt {
// Auto-timestamps: created_at
ret1 = append(ret1, &ast.AssignStmt{
@@ -321,23 +321,25 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
},
},
},
Else: &ast.IfStmt{
Cond: &ast.BinaryExpr{
X: ast.NewIdent("err"),
Op: token.NEQ,
Y: ast.NewIdent("nil"),
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ExprStmt{
X: &ast.CallExpr{
Fun: ast.NewIdent("panic"),
Args: []ast.Expr{ast.NewIdent("err")},
},
},
Else: func() *ast.IfStmt {
panicStmt := &ast.ExprStmt{
X: &ast.CallExpr{
Fun: ast.NewIdent("panic"),
Args: []ast.Expr{ast.NewIdent("err")},
},
},
},
}
TrailingComments[panicStmt] = "not a foreign key error"
return &ast.IfStmt{
Cond: &ast.BinaryExpr{
X: ast.NewIdent("err"),
Op: token.NEQ,
Y: ast.NewIdent("nil"),
},
Body: &ast.BlockStmt{
List: []ast.Stmt{panicStmt},
},
}
}(),
},
)
}(),
@@ -357,6 +359,7 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
Else: &ast.BlockStmt{
// Do update
List: []ast.Stmt{
Comment("Do update"),
&ast.AssignStmt{
Lhs: []ast.Expr{ast.NewIdent("result")},
Tok: token.DEFINE,
@@ -389,6 +392,10 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
}
funcDecl := &ast.FuncDecl{
Doc: &ast.CommentGroup{List: []*ast.Comment{
{Text: fmt.Sprintf("// Save%s creates or updates a %s in the database.", tbl.GoTypeName, tbl.GoTypeName)},
{Text: "// If the item doesn't exist (has no ID set), it will create it; otherwise it will do an update."},
}},
Recv: dbRecv,
Name: ast.NewIdent("Save" + tbl.GoTypeName),
Type: &ast.FuncType{