Compare commits
No commits in common. "8c29d455ff661a3d47b0f2432484a3bf9edd6a4d" and "4e0836eb2e5dfc8420c4230ad76d1bd60812f1c3" have entirely different histories.
8c29d455ff
...
4e0836eb2e
@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/jinzhu/inflection"
|
"github.com/jinzhu/inflection"
|
||||||
|
|
||||||
pkgschema "git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
pkgschema "git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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.
|
||||||
@ -16,11 +15,9 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
packageName := "db"
|
packageName := "db"
|
||||||
testpackageName := packageName + "_test"
|
testpackageName := packageName + "_test"
|
||||||
|
|
||||||
makeHelperName := ast.NewIdent("Make" + tbl.GoTypeName)
|
|
||||||
|
|
||||||
// func MakeItem() Item { return Item{} }
|
// func MakeItem() Item { return Item{} }
|
||||||
makeItemFunc := &ast.FuncDecl{
|
makeItemFunc := &ast.FuncDecl{
|
||||||
Name: makeHelperName,
|
Name: ast.NewIdent("Make" + tbl.GoTypeName),
|
||||||
Type: &ast.FuncType{
|
Type: &ast.FuncType{
|
||||||
Params: &ast.FieldList{},
|
Params: &ast.FieldList{},
|
||||||
Results: &ast.FieldList{
|
Results: &ast.FieldList{
|
||||||
@ -60,8 +57,8 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
testObj := ast.NewIdent(textutils.CamelToPascal(tbl.GoTypeName))
|
testObj := ast.NewIdent("item")
|
||||||
testObj2 := ast.NewIdent(textutils.CamelToPascal(tbl.GoTypeName) + "2")
|
testObj2 := ast.NewIdent("item2")
|
||||||
fieldName := ast.NewIdent("Description") // TODO
|
fieldName := ast.NewIdent("Description") // TODO
|
||||||
description1 := `"an item"`
|
description1 := `"an item"`
|
||||||
description2 := `"a big item"`
|
description2 := `"a big item"`
|
||||||
@ -97,13 +94,13 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
&ast.AssignStmt{
|
&ast.AssignStmt{
|
||||||
Lhs: []ast.Expr{testObj},
|
Lhs: []ast.Expr{testObj},
|
||||||
Tok: token.DEFINE,
|
Tok: token.DEFINE,
|
||||||
Rhs: []ast.Expr{&ast.CallExpr{Fun: makeHelperName, Args: nil}},
|
Rhs: []ast.Expr{&ast.CallExpr{Fun: ast.NewIdent("MakeItem"), Args: nil}},
|
||||||
},
|
},
|
||||||
// item.Description = "an item"
|
// item.Description = "an item"
|
||||||
&ast.AssignStmt{
|
&ast.AssignStmt{
|
||||||
Lhs: []ast.Expr{
|
Lhs: []ast.Expr{
|
||||||
&ast.SelectorExpr{
|
&ast.SelectorExpr{
|
||||||
X: testObj,
|
X: ast.NewIdent("item"),
|
||||||
Sel: ast.NewIdent("Description"),
|
Sel: ast.NewIdent("Description"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -310,11 +307,11 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
shouldDefineErr := true
|
|
||||||
for _, col := range tbl.Columns {
|
for _, col := range tbl.Columns {
|
||||||
if col.IsForeignKey {
|
if col.IsForeignKey {
|
||||||
shouldIncludeTestFkCheck = true
|
shouldIncludeTestFkCheck = true
|
||||||
stmts = append(stmts, []ast.Stmt{
|
stmts = append(stmts, []ast.Stmt{
|
||||||
|
|
||||||
// post.QuotedPostID = 94354538969386985
|
// post.QuotedPostID = 94354538969386985
|
||||||
&ast.AssignStmt{
|
&ast.AssignStmt{
|
||||||
Lhs: []ast.Expr{
|
Lhs: []ast.Expr{
|
||||||
@ -335,7 +332,7 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
// err := db.SavePost(&post)
|
// err := db.SavePost(&post)
|
||||||
&ast.AssignStmt{
|
&ast.AssignStmt{
|
||||||
Lhs: []ast.Expr{ast.NewIdent("err")},
|
Lhs: []ast.Expr{ast.NewIdent("err")},
|
||||||
Tok: map[bool]token.Token{true: token.DEFINE, false: token.ASSIGN}[shouldDefineErr],
|
Tok: token.DEFINE,
|
||||||
Rhs: []ast.Expr{
|
Rhs: []ast.Expr{
|
||||||
&ast.CallExpr{
|
&ast.CallExpr{
|
||||||
Fun: &ast.SelectorExpr{
|
Fun: &ast.SelectorExpr{
|
||||||
@ -371,7 +368,6 @@ func GenerateModelTestAST(tbl pkgschema.Table, schema pkgschema.Schema, gomodNam
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}...)
|
}...)
|
||||||
shouldDefineErr = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return stmts
|
return stmts
|
||||||
|
|||||||
@ -58,7 +58,8 @@ func (c Column) GoVarName() string {
|
|||||||
|
|
||||||
// LongGoVarName returns a lowercased version of the field name (Pascal => Camel).
|
// LongGoVarName returns a lowercased version of the field name (Pascal => Camel).
|
||||||
func (c Column) LongGoVarName() string {
|
func (c Column) LongGoVarName() string {
|
||||||
return textutils.CamelToPascal(c.GoFieldName())
|
fieldname := c.GoFieldName()
|
||||||
|
return strings.ToLower(fieldname)[0:1] + fieldname[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Table is a single SQLite table.
|
// Table is a single SQLite table.
|
||||||
|
|||||||
@ -9,7 +9,3 @@ func SnakeToCamel(s string) string {
|
|||||||
}
|
}
|
||||||
return strings.Join(parts, "")
|
return strings.Join(parts, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func CamelToPascal(s string) string {
|
|
||||||
return strings.ToLower(s)[0:1] + s[1:]
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user