support api templates

This commit is contained in:
kevin
2020-10-15 16:36:49 +08:00
parent 8291eabc2c
commit f904710811
32 changed files with 274 additions and 155 deletions

View File

@@ -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/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -22,7 +22,7 @@ func genDelete(table Table, withCache bool) (string, error) {
}
camel := table.Name.ToCamel()
output, err := util.With("delete").
output, err := templatex.With("delete").
Parse(template.Delete).
Execute(map[string]interface{}{
"upperStartCamelObject": camel,

View File

@@ -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/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
)
func genFields(fields []parser.Field) (string, error) {
@@ -25,7 +25,7 @@ func genField(field parser.Field) (string, error) {
if err != nil {
return "", err
}
output, err := util.With("types").
output, err := templatex.With("types").
Parse(template.Field).
Execute(map[string]interface{}{
"name": field.Name.ToCamel(),

View File

@@ -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/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
func genFindOne(table Table, withCache bool) (string, error) {
camel := table.Name.ToCamel()
output, err := util.With("findOne").
output, err := templatex.With("findOne").
Parse(template.FindOne).
Execute(map[string]interface{}{
"withCache": withCache,

View File

@@ -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/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
func genFindOneByField(table Table, withCache bool) (string, string, error) {
t := util.With("findOneByField").Parse(template.FindOneByField)
t := templatex.With("findOneByField").Parse(template.FindOneByField)
var list []string
camelTableName := table.Name.ToCamel()
for _, field := range table.Fields {
@@ -36,7 +36,7 @@ func genFindOneByField(table Table, withCache bool) (string, string, error) {
list = append(list, output.String())
}
if withCache {
out, err := util.With("findOneByFieldExtraMethod").Parse(template.FindOneByFieldExtraMethod).Execute(map[string]interface{}{
out, err := templatex.With("findOneByFieldExtraMethod").Parse(template.FindOneByFieldExtraMethod).Execute(map[string]interface{}{
"upperStartCamelObject": camelTableName,
"primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),

View File

@@ -9,6 +9,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"
"github.com/tal-tech/go-zero/tools/goctl/util/console"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
@@ -119,7 +120,7 @@ type (
)
func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, error) {
t := util.With("model").
t := templatex.With("model").
Parse(template.Model).
GoFmt(true)

View File

@@ -2,12 +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"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
)
func genImports(withCache, timeImport bool) (string, error) {
if withCache {
buffer, err := util.With("import").Parse(template.Imports).Execute(map[string]interface{}{
buffer, err := templatex.With("import").Parse(template.Imports).Execute(map[string]interface{}{
"time": timeImport,
})
if err != nil {
@@ -15,7 +15,7 @@ func genImports(withCache, timeImport bool) (string, error) {
}
return buffer.String(), nil
} else {
buffer, err := util.With("import").Parse(template.ImportsNoCache).Execute(map[string]interface{}{
buffer, err := templatex.With("import").Parse(template.ImportsNoCache).Execute(map[string]interface{}{
"time": timeImport,
})
if err != nil {

View File

@@ -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/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -34,7 +34,7 @@ func genInsert(table Table, withCache bool) (string, error) {
expressionValues = append(expressionValues, "data."+camel)
}
camel := table.Name.ToCamel()
output, err := util.With("insert").
output, err := templatex.With("insert").
Parse(template.Insert).
Execute(map[string]interface{}{
"withCache": withCache,

View File

@@ -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/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
)
func genNew(table Table, withCache bool) (string, error) {
output, err := util.With("new").
output, err := templatex.With("new").
Parse(template.New).
Execute(map[string]interface{}{
"withCache": withCache,

View File

@@ -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/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
)
func genTag(in string) (string, error) {
if in == "" {
return in, nil
}
output, err := util.With("tag").
output, err := templatex.With("tag").
Parse(template.Tag).
Execute(map[string]interface{}{
"field": in,

View File

@@ -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/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
)
func genTypes(table Table, withCache bool) (string, error) {
@@ -11,7 +11,7 @@ func genTypes(table Table, withCache bool) (string, error) {
if err != nil {
return "", err
}
output, err := util.With("types").
output, err := templatex.With("types").
Parse(template.Types).
Execute(map[string]interface{}{
"withCache": withCache,

View File

@@ -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/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -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 := util.With("update").
output, err := templatex.With("update").
Parse(template.Update).
Execute(map[string]interface{}{
"withCache": withCache,

View File

@@ -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/util"
"github.com/tal-tech/go-zero/tools/goctl/templatex"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -14,7 +14,7 @@ func genVars(table Table, withCache bool) (string, error) {
keys = append(keys, v.VarExpression)
}
camel := table.Name.ToCamel()
output, err := util.With("var").
output, err := templatex.With("var").
Parse(template.Vars).
GoFmt(true).
Execute(map[string]interface{}{