optimize unit test & add document

This commit is contained in:
anqiansong
2020-08-16 23:28:01 +08:00
parent 790b29b8eb
commit dbc034c9eb
46 changed files with 735 additions and 1855 deletions

View File

@@ -17,7 +17,7 @@ import (
const (
pwd = "."
createTableFlag = `(?m)CREATE\s+TABLE`
createTableFlag = `(?m)^(?i)CREATE\s+TABLE` // ignore case
)
type (
@@ -47,11 +47,9 @@ func NewDefaultGenerator(src, dir string, opt ...Option) *defaultGenerator {
return generator
}
func WithConsoleOption(idea bool) Option {
func WithConsoleOption(c console.Console) Option {
return func(generator *defaultGenerator) {
if idea {
generator.Console = console.NewIdeaConsole()
}
generator.Console = c
}
}
@@ -88,7 +86,7 @@ func (g *defaultGenerator) Start(withCache bool) error {
name := fmt.Sprintf("%smodel.go", strings.ToLower(stringx.From(tableName).Snake2Camel()))
filename := filepath.Join(dirAbs, name)
if util.FileExists(filename) {
g.Warning("%s already exists", name)
g.Warning("%s already exists,ignored.", name)
continue
}
err = ioutil.WriteFile(filename, []byte(code), os.ModePerm)
@@ -98,9 +96,7 @@ func (g *defaultGenerator) Start(withCache bool) error {
}
// generate error file
filename := filepath.Join(dirAbs, "error.go")
if util.FileExists(filename) {
g.Warning("error.go already exists")
} else {
if !util.FileExists(filename) {
err = ioutil.WriteFile(filename, []byte(template.Error), os.ModePerm)
if err != nil {
return err
@@ -144,10 +140,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
if err != nil {
return "", err
}
importsCode, err := genImports(withCache)
if err != nil {
return "", err
}
importsCode := genImports(withCache)
var table Table
table.Table = in
table.CacheKey = m
@@ -156,15 +149,15 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
if err != nil {
return "", err
}
typesCode, err := genTypes(table)
typesCode, err := genTypes(table, withCache)
if err != nil {
return "", err
}
newCode, err := genNew(table)
newCode, err := genNew(table, withCache)
if err != nil {
return "", err
}
insertCode, err := genInsert(table)
insertCode, err := genInsert(table, withCache)
if err != nil {
return "", err
}

View File

@@ -2,17 +2,12 @@ package gen
import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genImports(withCache bool) (string, error) {
output, err := templatex.With("import").
Parse(template.Imports).
Execute(map[string]interface{}{
"withCache": withCache,
})
if err != nil {
return "", err
func genImports(withCache bool) string {
if withCache {
return template.Imports
} else {
return template.ImportsNoCache
}
return output.String(), nil
}

View File

@@ -8,7 +8,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genInsert(table Table) (string, error) {
func genInsert(table Table, withCache bool) (string, error) {
expressions := make([]string, 0)
expressionValues := make([]string, 0)
for _, filed := range table.Fields {
@@ -26,6 +26,7 @@ func genInsert(table Table) (string, error) {
output, err := templatex.With("insert").
Parse(template.Insert).
Execute(map[string]interface{}{
"withCache": withCache,
"upperStartCamelObject": camel,
"lowerStartCamelObject": stringx.From(camel).LowerStart(),
"expression": strings.Join(expressions, ", "),

View File

@@ -5,10 +5,11 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genNew(table Table) (string, error) {
func genNew(table Table, withCache bool) (string, error) {
output, err := templatex.With("new").
Parse(template.New).
Execute(map[string]interface{}{
"withCache": withCache,
"upperStartCamelObject": table.Name.Snake2Camel(),
})
if err != nil {

View File

@@ -5,7 +5,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genTypes(table Table) (string, error) {
func genTypes(table Table, withCache bool) (string, error) {
fields := table.Fields
fieldsString, err := genFields(fields)
if err != nil {
@@ -14,6 +14,7 @@ func genTypes(table Table) (string, error) {
output, err := templatex.With("types").
Parse(template.Types).
Execute(map[string]interface{}{
"withCache": withCache,
"upperStartCamelObject": table.Name.Snake2Camel(),
"fields": fieldsString,
})