refine rpc generator
This commit is contained in:
@@ -9,17 +9,14 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// struct匹配
|
||||||
// struct匹配
|
const typeRegex = `(?m)(?m)(^ *type\s+[a-zA-Z][a-zA-Z0-9_-]+\s+(((struct)\s*?\{[\w\W]*?[^\{]\})|([a-zA-Z][a-zA-Z0-9_-]+)))|(^ *type\s*?\([\w\W]+\}\s*\))`
|
||||||
typeRegex = `(?m)(?m)(^ *type\s+[a-zA-Z][a-zA-Z0-9_-]+\s+(((struct)\s*?\{[\w\W]*?[^\{]\})|([a-zA-Z][a-zA-Z0-9_-]+)))|(^ *type\s*?\([\w\W]+\}\s*\))`
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
emptyStrcut = errors.New("struct body not found")
|
emptyStrcut = errors.New("struct body not found")
|
||||||
|
emptyType spec.Type
|
||||||
)
|
)
|
||||||
|
|
||||||
var emptyType spec.Type
|
|
||||||
|
|
||||||
func GetType(api *spec.ApiSpec, t string) spec.Type {
|
func GetType(api *spec.ApiSpec, t string) spec.Type {
|
||||||
for _, tp := range api.Types {
|
for _, tp := range api.Types {
|
||||||
if tp.Name == t {
|
if tp.Name == t {
|
||||||
|
|||||||
@@ -171,7 +171,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
|||||||
"types": typesCode,
|
"types": typesCode,
|
||||||
"new": newCode,
|
"new": newCode,
|
||||||
"insert": insertCode,
|
"insert": insertCode,
|
||||||
"find": strings.Join(findCode, "\r\n"),
|
"find": strings.Join(findCode, "\n"),
|
||||||
"update": updateCode,
|
"update": updateCode,
|
||||||
"delete": deleteCode,
|
"delete": deleteCode,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func genVars(table Table, withCache bool) (string, error) {
|
|||||||
Execute(map[string]interface{}{
|
Execute(map[string]interface{}{
|
||||||
"lowerStartCamelObject": stringx.From(camel).UnTitle(),
|
"lowerStartCamelObject": stringx.From(camel).UnTitle(),
|
||||||
"upperStartCamelObject": camel,
|
"upperStartCamelObject": camel,
|
||||||
"cacheKeys": strings.Join(keys, "\r\n"),
|
"cacheKeys": strings.Join(keys, "\n"),
|
||||||
"autoIncrement": table.PrimaryKey.AutoIncrement,
|
"autoIncrement": table.PrimaryKey.AutoIncrement,
|
||||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||||
"withCache": withCache,
|
"withCache": withCache,
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package ctx
|
package ctx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
@@ -16,8 +15,6 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
||||||
)
|
)
|
||||||
|
|
||||||
var errProtobufNotFound = errors.New("github.com/golang/protobuf is not found,please ensure you has already [go get github.com/golang/protobuf]")
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
constGo = "go"
|
constGo = "go"
|
||||||
constProtoC = "protoc"
|
constProtoC = "protoc"
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func (g *defaultRpcGenerator) Generate() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = g.genRemoteHandler()
|
err = g.genHandler()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,29 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
remoteTemplate = `{{.head}}
|
handlerTemplate = `{{.head}}
|
||||||
|
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import {{.imports}}
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
{{.imports}}
|
||||||
|
)
|
||||||
|
|
||||||
type {{.types}}
|
type {{.types}}
|
||||||
|
|
||||||
{{.newFuncs}}
|
func New{{.server}}Server(svcCtx *svc.ServiceContext) *{{.server}}Server {
|
||||||
|
return &{{.server}}Server{
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{{if .hasComment}}{{.comment}}{{end}}
|
||||||
|
func (s *{{.server}}Server) {{.method}} (ctx context.Context, in *{{.package}}.{{.request}}) (*{{.package}}.{{.response}}, error) {
|
||||||
|
l := logic.New{{.logicName}}(ctx,s.svcCtx)
|
||||||
|
return l.{{.method}}(in)
|
||||||
|
}
|
||||||
`
|
`
|
||||||
functionTemplate = `{{.head}}
|
functionTemplate = `{{.head}}
|
||||||
|
|
||||||
@@ -29,8 +43,6 @@ import (
|
|||||||
{{.imports}}
|
{{.imports}}
|
||||||
)
|
)
|
||||||
|
|
||||||
type {{.server}}Server struct{}
|
|
||||||
|
|
||||||
{{if .hasComment}}{{.comment}}{{end}}
|
{{if .hasComment}}{{.comment}}{{end}}
|
||||||
func (s *{{.server}}Server) {{.method}} (ctx context.Context, in *{{.package}}.{{.request}}) (*{{.package}}.{{.response}}, error) {
|
func (s *{{.server}}Server) {{.method}} (ctx context.Context, in *{{.package}}.{{.request}}) (*{{.package}}.{{.response}}, error) {
|
||||||
l := logic.New{{.logicName}}(ctx,s.svcCtx)
|
l := logic.New{{.logicName}}(ctx,s.svcCtx)
|
||||||
@@ -47,29 +59,35 @@ func (s *{{.server}}Server) {{.method}} (ctx context.Context, in *{{.package}}.{
|
|||||||
}`
|
}`
|
||||||
)
|
)
|
||||||
|
|
||||||
func (g *defaultRpcGenerator) genRemoteHandler() error {
|
func (g *defaultRpcGenerator) genHandler() error {
|
||||||
handlerPath := g.dirM[dirHandler]
|
handlerPath := g.dirM[dirHandler]
|
||||||
serverGo := fmt.Sprintf("%vhandler.go", g.Ctx.ServiceName.Lower())
|
filename := fmt.Sprintf("%vhandler.go", g.Ctx.ServiceName.Lower())
|
||||||
fileName := filepath.Join(handlerPath, serverGo)
|
handlerFile := filepath.Join(handlerPath, filename)
|
||||||
file := g.ast
|
file := g.ast
|
||||||
|
pkg := file.Package
|
||||||
|
pbImport := fmt.Sprintf(`%v "%v"`, pkg, g.mustGetPackage(dirPb))
|
||||||
|
logicImport := fmt.Sprintf(`"%v"`, g.mustGetPackage(dirLogic))
|
||||||
svcImport := fmt.Sprintf(`"%v"`, g.mustGetPackage(dirSvc))
|
svcImport := fmt.Sprintf(`"%v"`, g.mustGetPackage(dirSvc))
|
||||||
|
imports := []string{
|
||||||
|
pbImport,
|
||||||
|
logicImport,
|
||||||
|
svcImport,
|
||||||
|
}
|
||||||
types := make([]string, 0)
|
types := make([]string, 0)
|
||||||
newFuncs := make([]string, 0)
|
newFuncs := make([]string, 0)
|
||||||
head := util.GetHead(g.Ctx.ProtoSource)
|
head := util.GetHead(g.Ctx.ProtoSource)
|
||||||
for _, service := range file.Service {
|
for _, service := range file.Service {
|
||||||
types = append(types, fmt.Sprintf(typeFmt, service.Name.Title()))
|
types = append(types, fmt.Sprintf(typeFmt, service.Name.Title()))
|
||||||
newFuncs = append(newFuncs, fmt.Sprintf(newFuncFmt, service.Name.Title(), service.Name.Title(), service.Name.Title()))
|
newFuncs = append(newFuncs, fmt.Sprintf(newFuncFmt, service.Name.Title(),
|
||||||
|
service.Name.Title(), service.Name.Title()))
|
||||||
}
|
}
|
||||||
err := util.With("server").GoFmt(true).Parse(remoteTemplate).SaveTo(map[string]interface{}{
|
|
||||||
|
return util.With("server").GoFmt(true).Parse(handlerTemplate).SaveTo(map[string]interface{}{
|
||||||
"head": head,
|
"head": head,
|
||||||
"types": strings.Join(types, "\n"),
|
"types": strings.Join(types, "\n"),
|
||||||
"newFuncs": strings.Join(newFuncs, "\n"),
|
"newFuncs": strings.Join(newFuncs, "\n"),
|
||||||
"imports": svcImport,
|
"imports": strings.Join(imports, "\n\t"),
|
||||||
}, fileName, true)
|
}, handlerFile, true)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return g.genFunctions()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *defaultRpcGenerator) genFunctions() error {
|
func (g *defaultRpcGenerator) genFunctions() error {
|
||||||
@@ -89,19 +107,20 @@ func (g *defaultRpcGenerator) genFunctions() error {
|
|||||||
err := util.With("func").GoFmt(true).Parse(functionTemplate).SaveTo(map[string]interface{}{
|
err := util.With("func").GoFmt(true).Parse(functionTemplate).SaveTo(map[string]interface{}{
|
||||||
"head": head,
|
"head": head,
|
||||||
"server": service.Name.Title(),
|
"server": service.Name.Title(),
|
||||||
"imports": strings.Join(handlerImports, "\r\n"),
|
"imports": strings.Join(handlerImports, "\n"),
|
||||||
"logicName": fmt.Sprintf("%sLogic", method.Name.Title()),
|
"logicName": fmt.Sprintf("%sLogic", method.Name.Title()),
|
||||||
"method": method.Name.Title(),
|
"method": method.Name.Title(),
|
||||||
"package": pkg,
|
"package": pkg,
|
||||||
"request": method.InType,
|
"request": method.InType,
|
||||||
"response": method.OutType,
|
"response": method.OutType,
|
||||||
"hasComment": len(method.Document),
|
"hasComment": len(method.Document),
|
||||||
"comment": strings.Join(method.Document, "\r\n"),
|
"comment": strings.Join(method.Document, "\n"),
|
||||||
}, filename, true)
|
}, filename, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func (g *defaultRpcGenerator) genLogic() error {
|
|||||||
err = util.With("logic").GoFmt(true).Parse(logicTemplate).SaveTo(map[string]interface{}{
|
err = util.With("logic").GoFmt(true).Parse(logicTemplate).SaveTo(map[string]interface{}{
|
||||||
"logicName": fmt.Sprintf("%sLogic", method.Name.Title()),
|
"logicName": fmt.Sprintf("%sLogic", method.Name.Title()),
|
||||||
"functions": functions,
|
"functions": functions,
|
||||||
"imports": strings.Join(imports.KeysStr(), "\r\n"),
|
"imports": strings.Join(imports.KeysStr(), "\n"),
|
||||||
}, filename, false)
|
}, filename, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -83,7 +83,7 @@ func genLogicFunction(packageName string, method *parser.Func) (string, error) {
|
|||||||
"request": method.InType,
|
"request": method.InType,
|
||||||
"response": method.OutType,
|
"response": method.OutType,
|
||||||
"hasComment": len(method.Document) > 0,
|
"hasComment": len(method.Document) > 0,
|
||||||
"comment": strings.Join(method.Document, "\r\n"),
|
"comment": strings.Join(method.Document, "\n"),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ func (g *defaultRpcGenerator) genMain() error {
|
|||||||
"serviceName": g.Ctx.ServiceName.Lower(),
|
"serviceName": g.Ctx.ServiceName.Lower(),
|
||||||
"srv": srv,
|
"srv": srv,
|
||||||
"registers": registers,
|
"registers": registers,
|
||||||
"imports": strings.Join(imports, "\r\n"),
|
"imports": strings.Join(imports, "\n"),
|
||||||
}, fileName, true)
|
}, fileName, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user