generator: add GetAllXyzs() function
This commit is contained in:
parent
a05cad5144
commit
5c1e1dcb6a
@ -72,6 +72,7 @@ var generate_model = &cobra.Command{
|
|||||||
modelgenerate.GenerateSaveItemFunc(table),
|
modelgenerate.GenerateSaveItemFunc(table),
|
||||||
modelgenerate.GenerateDeleteItemFunc(table),
|
modelgenerate.GenerateDeleteItemFunc(table),
|
||||||
modelgenerate.GenerateGetItemByIDFunc(table),
|
modelgenerate.GenerateGetItemByIDFunc(table),
|
||||||
|
modelgenerate.GenerateGetAllItemsFunc(table),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
||||||
|
"github.com/jinzhu/inflection"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateIDType(table schema.Table) *ast.GenDecl {
|
func GenerateIDType(table schema.Table) *ast.GenDecl {
|
||||||
@ -233,6 +234,59 @@ func GenerateGetItemByIDFunc(tbl schema.Table) *ast.FuncDecl {
|
|||||||
return funcDecl
|
return funcDecl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateGetAllItemsFunc produces an AST for the `GetAllXyzs()` function.
|
||||||
|
// E.g., a table with `table.TypeName = "foods"` will produce a "GetAllFoods()" function.
|
||||||
|
func GenerateGetAllItemsFunc(tbl schema.Table) *ast.FuncDecl {
|
||||||
|
funcName := "GetAll" + inflection.Plural(tbl.TypeName)
|
||||||
|
recv := &ast.FieldList{List: []*ast.Field{
|
||||||
|
{Names: []*ast.Ident{ast.NewIdent("db")}, Type: ast.NewIdent("DB")},
|
||||||
|
}}
|
||||||
|
result := &ast.FieldList{List: []*ast.Field{
|
||||||
|
{Names: []*ast.Ident{ast.NewIdent("ret")}, Type: &ast.ArrayType{Elt: ast.NewIdent(tbl.TypeName)}},
|
||||||
|
}}
|
||||||
|
|
||||||
|
selectCall := &ast.CallExpr{
|
||||||
|
Fun: ast.NewIdent("PanicIf"),
|
||||||
|
Args: []ast.Expr{
|
||||||
|
&ast.CallExpr{
|
||||||
|
Fun: &ast.SelectorExpr{
|
||||||
|
X: ast.NewIdent("db.DB"),
|
||||||
|
Sel: ast.NewIdent("Select"),
|
||||||
|
},
|
||||||
|
Args: []ast.Expr{
|
||||||
|
&ast.UnaryExpr{Op: token.AND, X: ast.NewIdent("ret")},
|
||||||
|
&ast.BinaryExpr{
|
||||||
|
X: &ast.BinaryExpr{
|
||||||
|
X: &ast.BasicLit{Kind: token.STRING, Value: "`SELECT `"},
|
||||||
|
Op: token.ADD,
|
||||||
|
Y: SQLFieldsConstIdent(tbl),
|
||||||
|
},
|
||||||
|
Op: token.ADD,
|
||||||
|
Y: &ast.BasicLit{Kind: token.STRING, Value: "` FROM " + tbl.TableName + "`"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
funcBody := &ast.BlockStmt{
|
||||||
|
List: []ast.Stmt{
|
||||||
|
&ast.ExprStmt{X: selectCall},
|
||||||
|
&ast.ReturnStmt{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ast.FuncDecl{
|
||||||
|
Recv: recv,
|
||||||
|
Name: ast.NewIdent(funcName),
|
||||||
|
Type: &ast.FuncType{
|
||||||
|
Params: &ast.FieldList{},
|
||||||
|
Results: result,
|
||||||
|
},
|
||||||
|
Body: funcBody,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GenerateDeleteItemFunc produces an AST for the `DeleteXyz()` function.
|
// GenerateDeleteItemFunc produces an AST for the `DeleteXyz()` function.
|
||||||
// E.g., a table with `table.TypeName = "foods"` will produce a "DeleteFood()" function.
|
// E.g., a table with `table.TypeName = "foods"` will produce a "DeleteFood()" function.
|
||||||
func GenerateDeleteItemFunc(tbl schema.Table) *ast.FuncDecl {
|
func GenerateDeleteItemFunc(tbl schema.Table) *ast.FuncDecl {
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import (
|
|||||||
"go/token"
|
"go/token"
|
||||||
|
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
||||||
|
"github.com/jinzhu/inflection"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GenerateModelTestAST produces an AST for a starter test file for a given model.
|
// GenerateModelTestAST produces an AST for a starter test file for a given model.
|
||||||
@ -87,40 +88,7 @@ func GenerateModelTestAST(tbl schema.Table, gomodName string) *ast.File {
|
|||||||
description1 := `"an item"`
|
description1 := `"an item"`
|
||||||
description2 := `"a big item"`
|
description2 := `"a big item"`
|
||||||
|
|
||||||
return &ast.File{
|
testCreateUpdateDelete := &ast.FuncDecl{
|
||||||
Name: ast.NewIdent(testpackageName),
|
|
||||||
Decls: []ast.Decl{
|
|
||||||
&ast.GenDecl{
|
|
||||||
Tok: token.IMPORT,
|
|
||||||
Specs: []ast.Spec{
|
|
||||||
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"fmt"`}},
|
|
||||||
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"testing"`}},
|
|
||||||
&ast.ImportSpec{
|
|
||||||
Path: &ast.BasicLit{Kind: token.STRING, Value: `"git.offline-twitter.com/offline-labs/gas-stack/pkg/db"`},
|
|
||||||
Name: ast.NewIdent("db"),
|
|
||||||
},
|
|
||||||
&ast.ImportSpec{
|
|
||||||
Path: &ast.BasicLit{Kind: token.STRING, Value: `"git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"`},
|
|
||||||
Name: ast.NewIdent("."),
|
|
||||||
},
|
|
||||||
&ast.ImportSpec{
|
|
||||||
Path: &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s/pkg/%s"`, gomodName, packageName)},
|
|
||||||
Name: ast.NewIdent("."),
|
|
||||||
},
|
|
||||||
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"github.com/stretchr/testify/assert"`}},
|
|
||||||
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"github.com/stretchr/testify/require"`}},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
// var TestDB *DB
|
|
||||||
testDBDecl,
|
|
||||||
|
|
||||||
// func init() { TestDB = MakeDB("tmp") }
|
|
||||||
initFuncDecl,
|
|
||||||
|
|
||||||
// func MakeDB(dbName string) *DB { db := Must(Create(fmt.Sprintf("file:%s?mode=memory&cache=shared", dbName))); return db }
|
|
||||||
makeDBHelperDecl,
|
|
||||||
|
|
||||||
&ast.FuncDecl{
|
|
||||||
Name: ast.NewIdent("TestCreateUpdateDelete" + tbl.TypeName),
|
Name: ast.NewIdent("TestCreateUpdateDelete" + tbl.TypeName),
|
||||||
Type: &ast.FuncType{
|
Type: &ast.FuncType{
|
||||||
Params: &ast.FieldList{
|
Params: &ast.FieldList{
|
||||||
@ -245,7 +213,64 @@ func GenerateModelTestAST(tbl schema.Table, gomodName string) *ast.File {
|
|||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
testGetAll := &ast.FuncDecl{
|
||||||
|
Name: ast.NewIdent("TestGetAll" + inflection.Plural(tbl.TypeName)),
|
||||||
|
Type: &ast.FuncType{Params: &ast.FieldList{List: []*ast.Field{
|
||||||
|
{Names: []*ast.Ident{ast.NewIdent("t")}, Type: &ast.StarExpr{X: ast.NewIdent("testing.T")}},
|
||||||
|
}}, Results: nil},
|
||||||
|
Body: &ast.BlockStmt{
|
||||||
|
List: []ast.Stmt{
|
||||||
|
&ast.AssignStmt{
|
||||||
|
Lhs: []ast.Expr{ast.NewIdent("_")},
|
||||||
|
Tok: token.ASSIGN,
|
||||||
|
Rhs: []ast.Expr{&ast.CallExpr{
|
||||||
|
Fun: &ast.SelectorExpr{
|
||||||
|
X: ast.NewIdent("TestDB"),
|
||||||
|
Sel: ast.NewIdent("GetAll" + inflection.Plural(tbl.TypeName)),
|
||||||
|
},
|
||||||
|
}},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return &ast.File{
|
||||||
|
Name: ast.NewIdent(testpackageName),
|
||||||
|
Decls: []ast.Decl{
|
||||||
|
&ast.GenDecl{
|
||||||
|
Tok: token.IMPORT,
|
||||||
|
Specs: []ast.Spec{
|
||||||
|
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"fmt"`}},
|
||||||
|
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"testing"`}},
|
||||||
|
&ast.ImportSpec{
|
||||||
|
Path: &ast.BasicLit{Kind: token.STRING, Value: `"git.offline-twitter.com/offline-labs/gas-stack/pkg/db"`},
|
||||||
|
Name: ast.NewIdent("db"),
|
||||||
|
},
|
||||||
|
&ast.ImportSpec{
|
||||||
|
Path: &ast.BasicLit{Kind: token.STRING, Value: `"git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"`},
|
||||||
|
Name: ast.NewIdent("."),
|
||||||
|
},
|
||||||
|
&ast.ImportSpec{
|
||||||
|
Path: &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s/pkg/%s"`, gomodName, packageName)},
|
||||||
|
Name: ast.NewIdent("."),
|
||||||
|
},
|
||||||
|
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"github.com/stretchr/testify/assert"`}},
|
||||||
|
&ast.ImportSpec{Path: &ast.BasicLit{Kind: token.STRING, Value: `"github.com/stretchr/testify/require"`}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// var TestDB *DB
|
||||||
|
testDBDecl,
|
||||||
|
|
||||||
|
// func init() { TestDB = MakeDB("tmp") }
|
||||||
|
initFuncDecl,
|
||||||
|
|
||||||
|
// func MakeDB(dbName string) *DB { db := Must(Create(fmt.Sprintf("file:%s?mode=memory&cache=shared", dbName))); return db }
|
||||||
|
makeDBHelperDecl,
|
||||||
|
|
||||||
|
testCreateUpdateDelete,
|
||||||
|
testGetAll,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user