codegen: make generated code use 'must' instead of 'flowutils'
This commit is contained in:
@@ -51,10 +51,9 @@ var generate_model = &cobra.Command{
|
|||||||
Path: &ast.BasicLit{Kind: token.STRING, Value: `"git.offline-twitter.com/offline-labs/gas-stack/pkg/db"`},
|
Path: &ast.BasicLit{Kind: token.STRING, Value: `"git.offline-twitter.com/offline-labs/gas-stack/pkg/db"`},
|
||||||
},
|
},
|
||||||
&ast.ImportSpec{
|
&ast.ImportSpec{
|
||||||
Name: ast.NewIdent("."),
|
|
||||||
Path: &ast.BasicLit{
|
Path: &ast.BasicLit{
|
||||||
Kind: token.STRING,
|
Kind: token.STRING,
|
||||||
Value: `"git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"`,
|
Value: `"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -27,10 +27,18 @@ const (
|
|||||||
// we don't have to do that.
|
// we don't have to do that.
|
||||||
var TrailingComments = map[ast.Node]string{}
|
var TrailingComments = map[ast.Node]string{}
|
||||||
|
|
||||||
// mustCall wraps a call expression in Must(...), producing AST for Must(inner).
|
// mustCall wraps a call expression in must.Get(...), producing AST for must.Get(inner).
|
||||||
func mustCall(inner ast.Expr) *ast.CallExpr {
|
func mustCall(inner ast.Expr) *ast.CallExpr {
|
||||||
return &ast.CallExpr{
|
return &ast.CallExpr{
|
||||||
Fun: ast.NewIdent("Must"),
|
Fun: &ast.SelectorExpr{X: ast.NewIdent("must"), Sel: ast.NewIdent("Get")},
|
||||||
|
Args: []ast.Expr{inner},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// doCall wraps a call expression in must.Do(...), producing AST for must.Do(inner).
|
||||||
|
func doCall(inner ast.Expr) *ast.CallExpr {
|
||||||
|
return &ast.CallExpr{
|
||||||
|
Fun: &ast.SelectorExpr{X: ast.NewIdent("must"), Sel: ast.NewIdent("Do")},
|
||||||
Args: []ast.Expr{inner},
|
Args: []ast.Expr{inner},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -333,7 +333,7 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
if !hasFks {
|
if !hasFks {
|
||||||
// No foreign key checking needed; just use `Must` for brevity
|
// No foreign key checking needed; just use `must.Get` for brevity
|
||||||
return []ast.Stmt{&ast.AssignStmt{
|
return []ast.Stmt{&ast.AssignStmt{
|
||||||
Lhs: []ast.Expr{ast.NewIdent("result")},
|
Lhs: []ast.Expr{ast.NewIdent("result")},
|
||||||
Tok: token.DEFINE,
|
Tok: token.DEFINE,
|
||||||
@@ -660,10 +660,7 @@ func GenerateGetAllItemsFunc(tbl schema.Table) *ast.FuncDecl {
|
|||||||
{Names: []*ast.Ident{ast.NewIdent("ret")}, Type: &ast.ArrayType{Elt: ast.NewIdent(tbl.GoTypeName)}},
|
{Names: []*ast.Ident{ast.NewIdent("ret")}, Type: &ast.ArrayType{Elt: ast.NewIdent(tbl.GoTypeName)}},
|
||||||
}}
|
}}
|
||||||
|
|
||||||
selectCall := &ast.CallExpr{
|
selectCall := doCall(&ast.CallExpr{
|
||||||
Fun: ast.NewIdent("PanicIf"),
|
|
||||||
Args: []ast.Expr{
|
|
||||||
&ast.CallExpr{
|
|
||||||
Fun: &ast.SelectorExpr{
|
Fun: &ast.SelectorExpr{
|
||||||
X: dbDB,
|
X: dbDB,
|
||||||
Sel: ast.NewIdent("Select"),
|
Sel: ast.NewIdent("Select"),
|
||||||
@@ -680,9 +677,7 @@ func GenerateGetAllItemsFunc(tbl schema.Table) *ast.FuncDecl {
|
|||||||
Y: &ast.BasicLit{Kind: token.STRING, Value: "` from " + tbl.TableName + "`"},
|
Y: &ast.BasicLit{Kind: token.STRING, Value: "` from " + tbl.TableName + "`"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
funcBody := &ast.BlockStmt{
|
funcBody := &ast.BlockStmt{
|
||||||
List: []ast.Stmt{
|
List: []ast.Stmt{
|
||||||
|
|||||||
@@ -288,7 +288,7 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
BlankLine(),
|
BlankLine(),
|
||||||
Comment("Load"),
|
Comment("Load"),
|
||||||
|
|
||||||
// item2 := Must(TestDB.GetItemByID(item.ID))
|
// item2 := must.Get(TestDB.GetItemByID(item.ID))
|
||||||
&ast.AssignStmt{
|
&ast.AssignStmt{
|
||||||
Lhs: []ast.Expr{testObj2},
|
Lhs: []ast.Expr{testObj2},
|
||||||
Tok: token.DEFINE,
|
Tok: token.DEFINE,
|
||||||
@@ -319,7 +319,7 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
Args: []ast.Expr{&ast.UnaryExpr{Op: token.AND, X: testObj}},
|
Args: []ast.Expr{&ast.UnaryExpr{Op: token.AND, X: testObj}},
|
||||||
}},
|
}},
|
||||||
|
|
||||||
// item2 = Must(TestDB.GetItemByID(item.ID))
|
// item2 = must.Get(TestDB.GetItemByID(item.ID))
|
||||||
&ast.AssignStmt{
|
&ast.AssignStmt{
|
||||||
Lhs: []ast.Expr{testObj2},
|
Lhs: []ast.Expr{testObj2},
|
||||||
Tok: token.ASSIGN,
|
Tok: token.ASSIGN,
|
||||||
@@ -445,8 +445,7 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
Name: ast.NewIdent("."),
|
Name: ast.NewIdent("."),
|
||||||
},
|
},
|
||||||
&ast.ImportSpec{
|
&ast.ImportSpec{
|
||||||
Path: &ast.BasicLit{Kind: token.STRING, Value: `"git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"`},
|
Path: &ast.BasicLit{Kind: token.STRING, Value: `"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"`},
|
||||||
Name: ast.NewIdent("."),
|
|
||||||
},
|
},
|
||||||
&ast.ImportSpec{
|
&ast.ImportSpec{
|
||||||
Path: &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s/pkg/%s"`, gomodName, packageName)},
|
Path: &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s/pkg/%s"`, gomodName, packageName)},
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package db_test
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
. "git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"
|
||||||
|
|
||||||
. "{{ .ModuleName }}/pkg/db"
|
. "{{ .ModuleName }}/pkg/db"
|
||||||
)
|
)
|
||||||
@@ -14,6 +14,6 @@ func init() {
|
|||||||
TestDB = MakeDB("tmp")
|
TestDB = MakeDB("tmp")
|
||||||
}
|
}
|
||||||
func MakeDB(dbName string) *DB {
|
func MakeDB(dbName string) *DB {
|
||||||
db := Must(Create(fmt.Sprintf("file:%s?mode=memory&cache=shared", dbName)))
|
db := must.Get(Create(fmt.Sprintf("file:%s?mode=memory&cache=shared", dbName)))
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user