gozero template (#147)

* model/rpc generate code from template cache

* delete unused(deprecated) code

* support template init|update|clean|revert

* model: return the execute result for insert and update operation

* // deprecated: containsAny

* add template test

* add default buildVersion

* update build version
This commit is contained in:
Keson
2020-10-21 14:59:35 +08:00
committed by GitHub
parent fe0d0687f5
commit 41964f9d52
43 changed files with 908 additions and 119 deletions

View File

@@ -1,54 +1,69 @@
package gen
import (
"path/filepath"
"strings"
"fmt"
"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"
"github.com/urfave/cli"
)
const rpcTemplateText = `syntax = "proto3";
const (
category = "rpc"
callTemplateFile = "call.tpl"
callTypesTemplateFile = "call-types.tpl"
callInterfaceFunctionTemplateFile = "call-interface-func.tpl"
callFunctionTemplateFile = "call-func.tpl"
configTemplateFileFile = "config.tpl"
etcTemplateFileFile = "etc.tpl"
logicTemplateFileFile = "logic.tpl"
logicFuncTemplateFileFile = "logic-func.tpl"
mainTemplateFile = "main.tpl"
serverTemplateFile = "server.tpl"
serverFuncTemplateFile = "server-func.tpl"
svcTemplateFile = "svc.tpl"
rpcTemplateFile = "template.tpl"
)
package {{.package}};
message Request {
string ping = 1;
var templates = map[string]string{
callTemplateFile: callTemplateText,
callTypesTemplateFile: callTemplateTypes,
callInterfaceFunctionTemplateFile: callInterfaceFunctionTemplate,
callFunctionTemplateFile: callFunctionTemplate,
configTemplateFileFile: configTemplate,
etcTemplateFileFile: etcTemplate,
logicTemplateFileFile: logicTemplate,
logicFuncTemplateFileFile: logicFunctionTemplate,
mainTemplateFile: mainTemplate,
serverTemplateFile: serverTemplate,
serverFuncTemplateFile: functionTemplate,
svcTemplateFile: svcTemplate,
rpcTemplateFile: rpcTemplateText,
}
message Response {
string pong = 1;
func GenTemplates(_ *cli.Context) error {
return util.InitTemplates(category, templates)
}
service {{.serviceName}} {
rpc Ping(Request) returns(Response);
}
`
type rpcTemplate struct {
out string
console.Console
}
func NewRpcTemplate(out string, idea bool) *rpcTemplate {
return &rpcTemplate{
out: out,
Console: console.NewConsole(idea),
func RevertTemplate(name string) error {
content, ok := templates[name]
if !ok {
return fmt.Errorf("%s: no such file name", name)
}
return util.CreateTemplate(category, name, content)
}
func (r *rpcTemplate) MustGenerate(showState bool) {
r.Info("查看rpc生成请移步至「https://github.com/tal-tech/zero-doc/blob/main/doc/goctl-rpc.md」")
r.Info("generating template...")
protoFilename := filepath.Base(r.out)
serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
err := util.With("t").Parse(rpcTemplateText).SaveTo(map[string]string{
"package": serviceName.UnTitle(),
"serviceName": serviceName.Title(),
}, r.out, false)
r.Must(err)
if showState {
r.Success("Done.")
}
func Clean() error {
return util.Clean(category)
}
func Update(category string) error {
err := Clean()
if err != nil {
return err
}
return util.InitTemplates(category, templates)
}
func GetCategory() string {
return category
}