Skip to content

Commit

Permalink
use goctl template to generate all kinds of templates
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Oct 19, 2020
1 parent 85a815b commit dfe6e88
Show file tree
Hide file tree
Showing 35 changed files with 152 additions and 104 deletions.
11 changes: 11 additions & 0 deletions core/errorx/callchain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package errorx

func Chain(fns ...func() error) error {
for _, fn := range fns {
if err := fn(); err != nil {
return err
}
}

return nil
}
27 changes: 27 additions & 0 deletions core/errorx/callchain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package errorx

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestChain(t *testing.T) {
var errDummy = errors.New("dummy")
assert.Nil(t, Chain(func() error {
return nil
}, func() error {
return nil
}))
assert.Equal(t, errDummy, Chain(func() error {
return errDummy
}, func() error {
return nil
}))
assert.Equal(t, errDummy, Chain(func() error {
return nil
}, func() error {
return errDummy
}))
}
4 changes: 2 additions & 2 deletions tools/goctl/api/gogen/genconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/vars"
)

Expand Down Expand Up @@ -48,7 +48,7 @@ func genConfig(dir string, api *spec.ApiSpec) error {
}

var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)
text, err := templatex.LoadTemplate(category, configTemplateFile, configTemplate)
text, err := ctlutil.LoadTemplate(category, configTemplateFile, configTemplate)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/api/gogen/genetc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
)

const (
Expand Down Expand Up @@ -40,7 +40,7 @@ func genEtc(dir string, api *spec.ApiSpec) error {
port = strconv.Itoa(defaultPort)
}

text, err := templatex.LoadTemplate(category, etcTemplateFile, etcTemplate)
text, err := ctlutil.LoadTemplate(category, etcTemplateFile, etcTemplate)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions tools/goctl/api/gogen/genhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/tal-tech/go-zero/tools/goctl/api/spec"
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/vars"
)
Expand Down Expand Up @@ -94,7 +93,7 @@ func doGenToFile(dir, handler string, group spec.Group, route spec.Route, handle
}
defer fp.Close()

text, err := templatex.LoadTemplate(category, handlerTemplateFile, handlerTemplate)
text, err := util.LoadTemplate(category, handlerTemplateFile, handlerTemplate)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions tools/goctl/api/gogen/genlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/vars"
)
Expand Down Expand Up @@ -94,7 +93,7 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
requestString = "req " + "types." + strings.Title(route.RequestType.Name)
}

text, err := templatex.LoadTemplate(category, logicTemplateFile, logicTemplate)
text, err := ctlutil.LoadTemplate(category, logicTemplateFile, logicTemplate)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions tools/goctl/api/gogen/genmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/vars"
)
Expand Down Expand Up @@ -61,7 +60,7 @@ func genMain(dir string, api *spec.ApiSpec) error {
return err
}

text, err := templatex.LoadTemplate(category, mainTemplateFile, mainTemplate)
text, err := ctlutil.LoadTemplate(category, mainTemplateFile, mainTemplate)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions tools/goctl/api/gogen/gensvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/vars"
)
Expand Down Expand Up @@ -52,7 +51,7 @@ func genServiceContext(dir string, api *spec.ApiSpec) error {
return err
}

text, err := templatex.LoadTemplate(category, contextTemplateFile, contextTemplate)
text, err := ctlutil.LoadTemplate(category, contextTemplateFile, contextTemplate)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/api/gogen/template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gogen

import (
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli"
)

Expand All @@ -25,5 +25,5 @@ var templates = map[string]string{
}

func GenTemplates(_ *cli.Context) error {
return templatex.InitTemplates(category, templates)
return util.InitTemplates(category, templates)
}
12 changes: 5 additions & 7 deletions tools/goctl/goctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,6 @@ var (
},
},
Action: gogen.GoCommand,
Subcommands: []cli.Command{
{
Name: "template",
Usage: "initialize the api templates",
Action: gogen.GenTemplates,
},
},
},
{
Name: "java",
Expand Down Expand Up @@ -339,6 +332,11 @@ var (
Usage: "the features of the latest version",
Action: feature.Feature,
},
{
Name: "template",
Usage: "initialize the api templates",
Action: gogen.GenTemplates,
},
}
)

Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/model/sql/gen/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)

Expand All @@ -22,7 +22,7 @@ func genDelete(table Table, withCache bool) (string, error) {
}

camel := table.Name.ToCamel()
output, err := templatex.With("delete").
output, err := util.With("delete").
Parse(template.Delete).
Execute(map[string]interface{}{
"upperStartCamelObject": camel,
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/model/sql/gen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
)

func genFields(fields []parser.Field) (string, error) {
Expand All @@ -25,7 +25,7 @@ func genField(field parser.Field) (string, error) {
if err != nil {
return "", err
}
output, err := templatex.With("types").
output, err := util.With("types").
Parse(template.Field).
Execute(map[string]interface{}{
"name": field.Name.ToCamel(),
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/model/sql/gen/findone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package gen

import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)

func genFindOne(table Table, withCache bool) (string, error) {
camel := table.Name.ToCamel()
output, err := templatex.With("findOne").
output, err := util.With("findOne").
Parse(template.FindOne).
Execute(map[string]interface{}{
"withCache": withCache,
Expand Down
6 changes: 3 additions & 3 deletions tools/goctl/model/sql/gen/findonebyfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"strings"

"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)

func genFindOneByField(table Table, withCache bool) (string, string, error) {
t := templatex.With("findOneByField").Parse(template.FindOneByField)
t := util.With("findOneByField").Parse(template.FindOneByField)
var list []string
camelTableName := table.Name.ToCamel()
for _, field := range table.Fields {
Expand All @@ -36,7 +36,7 @@ func genFindOneByField(table Table, withCache bool) (string, string, error) {
list = append(list, output.String())
}
if withCache {
out, err := templatex.With("findOneByFieldExtraMethod").Parse(template.FindOneByFieldExtraMethod).Execute(map[string]interface{}{
out, err := util.With("findOneByFieldExtraMethod").Parse(template.FindOneByFieldExtraMethod).Execute(map[string]interface{}{
"upperStartCamelObject": camelTableName,
"primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
Expand Down
3 changes: 1 addition & 2 deletions tools/goctl/model/sql/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/console"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
Expand Down Expand Up @@ -120,7 +119,7 @@ type (
)

func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, error) {
t := templatex.With("model").
t := util.With("model").
Parse(template.Model).
GoFmt(true)

Expand Down
6 changes: 3 additions & 3 deletions tools/goctl/model/sql/gen/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package gen

import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
)

func genImports(withCache, timeImport bool) (string, error) {
if withCache {
buffer, err := templatex.With("import").Parse(template.Imports).Execute(map[string]interface{}{
buffer, err := util.With("import").Parse(template.Imports).Execute(map[string]interface{}{
"time": timeImport,
})
if err != nil {
return "", err
}
return buffer.String(), nil
} else {
buffer, err := templatex.With("import").Parse(template.ImportsNoCache).Execute(map[string]interface{}{
buffer, err := util.With("import").Parse(template.ImportsNoCache).Execute(map[string]interface{}{
"time": timeImport,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/model/sql/gen/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)

Expand Down Expand Up @@ -34,7 +34,7 @@ func genInsert(table Table, withCache bool) (string, error) {
expressionValues = append(expressionValues, "data."+camel)
}
camel := table.Name.ToCamel()
output, err := templatex.With("insert").
output, err := util.With("insert").
Parse(template.Insert).
Execute(map[string]interface{}{
"withCache": withCache,
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/model/sql/gen/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package gen

import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
)

func genNew(table Table, withCache bool) (string, error) {
output, err := templatex.With("new").
output, err := util.With("new").
Parse(template.New).
Execute(map[string]interface{}{
"withCache": withCache,
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/model/sql/gen/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package gen

import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
)

func genTag(in string) (string, error) {
if in == "" {
return in, nil
}
output, err := templatex.With("tag").
output, err := util.With("tag").
Parse(template.Tag).
Execute(map[string]interface{}{
"field": in,
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/model/sql/gen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package gen

import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
)

func genTypes(table Table, withCache bool) (string, error) {
Expand All @@ -11,7 +11,7 @@ func genTypes(table Table, withCache bool) (string, error) {
if err != nil {
return "", err
}
output, err := templatex.With("types").
output, err := util.With("types").
Parse(template.Types).
Execute(map[string]interface{}{
"withCache": withCache,
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/model/sql/gen/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"

"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)

Expand All @@ -22,7 +22,7 @@ func genUpdate(table Table, withCache bool) (string, error) {
}
expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel())
camelTableName := table.Name.ToCamel()
output, err := templatex.With("update").
output, err := util.With("update").
Parse(template.Update).
Execute(map[string]interface{}{
"withCache": withCache,
Expand Down
Loading

0 comments on commit dfe6e88

Please sign in to comment.