codegen: add ability to insert comments and blank lines into the generated code
This commit is contained in:
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user