|
|
@ -1,10 +1,11 @@
|
|
|
|
package gogen
|
|
|
|
package gen
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -27,22 +28,9 @@ func New{{.server}}Server(svcCtx *svc.ServiceContext) *{{.server}}Server {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{{if .hasComment}}{{.comment}}{{end}}
|
|
|
|
{{.funcs}}
|
|
|
|
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 = `
|
|
|
|
|
|
|
|
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{{.imports}}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{{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)
|
|
|
@ -52,17 +40,10 @@ func (s *{{.server}}Server) {{.method}} (ctx context.Context, in *{{.package}}.{
|
|
|
|
typeFmt = `%sServer struct {
|
|
|
|
typeFmt = `%sServer struct {
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}`
|
|
|
|
}`
|
|
|
|
newFuncFmt = `func New%sServer(svcCtx *svc.ServiceContext) *%sServer {
|
|
|
|
|
|
|
|
return &%sServer{
|
|
|
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (g *defaultRpcGenerator) genHandler() error {
|
|
|
|
func (g *defaultRpcGenerator) genHandler() error {
|
|
|
|
handlerPath := g.dirM[dirHandler]
|
|
|
|
handlerPath := g.dirM[dirHandler]
|
|
|
|
filename := fmt.Sprintf("%vhandler.go", g.Ctx.ServiceName.Lower())
|
|
|
|
|
|
|
|
handlerFile := filepath.Join(handlerPath, filename)
|
|
|
|
|
|
|
|
file := g.ast
|
|
|
|
file := g.ast
|
|
|
|
pkg := file.Package
|
|
|
|
pkg := file.Package
|
|
|
|
pbImport := fmt.Sprintf(`%v "%v"`, pkg, g.mustGetPackage(dirPb))
|
|
|
|
pbImport := fmt.Sprintf(`%v "%v"`, pkg, g.mustGetPackage(dirPb))
|
|
|
@ -73,41 +54,35 @@ func (g *defaultRpcGenerator) genHandler() error {
|
|
|
|
logicImport,
|
|
|
|
logicImport,
|
|
|
|
svcImport,
|
|
|
|
svcImport,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
types := 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()))
|
|
|
|
filename := fmt.Sprintf("%vhandler.go", service.Name.Lower())
|
|
|
|
newFuncs = append(newFuncs, fmt.Sprintf(newFuncFmt, service.Name.Title(),
|
|
|
|
handlerFile := filepath.Join(handlerPath, filename)
|
|
|
|
service.Name.Title(), service.Name.Title()))
|
|
|
|
funcList, err := g.genFunctions(service)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = util.With("server").GoFmt(true).Parse(handlerTemplate).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": fmt.Sprintf(typeFmt, service.Name.Title()),
|
|
|
|
"newFuncs": strings.Join(newFuncs, "\n"),
|
|
|
|
"server": service.Name.Title(),
|
|
|
|
"imports": strings.Join(imports, "\n\t"),
|
|
|
|
"imports": strings.Join(imports, "\n\t"),
|
|
|
|
|
|
|
|
"funcs": strings.Join(funcList, "\n"),
|
|
|
|
}, handlerFile, true)
|
|
|
|
}, handlerFile, true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (g *defaultRpcGenerator) genFunctions() error {
|
|
|
|
func (g *defaultRpcGenerator) genFunctions(service *parser.RpcService) ([]string, error) {
|
|
|
|
handlerPath := g.dirM[dirHandler]
|
|
|
|
|
|
|
|
file := g.ast
|
|
|
|
file := g.ast
|
|
|
|
pkg := file.Package
|
|
|
|
pkg := file.Package
|
|
|
|
|
|
|
|
var functionList []string
|
|
|
|
head := util.GetHead(g.Ctx.ProtoSource)
|
|
|
|
|
|
|
|
handlerImports := make([]string, 0)
|
|
|
|
|
|
|
|
pbImport := fmt.Sprintf(`%v "%v"`, pkg, g.mustGetPackage(dirPb))
|
|
|
|
|
|
|
|
handlerImports = append(handlerImports, pbImport, fmt.Sprintf(`"%v"`, g.mustGetPackage(dirLogic)))
|
|
|
|
|
|
|
|
for _, service := range file.Service {
|
|
|
|
|
|
|
|
for _, method := range service.Funcs {
|
|
|
|
for _, method := range service.Funcs {
|
|
|
|
handlerName := fmt.Sprintf("%shandler.go", method.Name.Lower())
|
|
|
|
buffer, err := util.With("func").Parse(functionTemplate).Execute(map[string]interface{}{
|
|
|
|
filename := filepath.Join(handlerPath, handlerName)
|
|
|
|
|
|
|
|
// override
|
|
|
|
|
|
|
|
err := util.With("func").GoFmt(true).Parse(functionTemplate).SaveTo(map[string]interface{}{
|
|
|
|
|
|
|
|
"head": head,
|
|
|
|
|
|
|
|
"server": service.Name.Title(),
|
|
|
|
"server": service.Name.Title(),
|
|
|
|
"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,
|
|
|
@ -115,12 +90,11 @@ func (g *defaultRpcGenerator) genFunctions() error {
|
|
|
|
"response": method.OutType,
|
|
|
|
"response": method.OutType,
|
|
|
|
"hasComment": len(method.Document),
|
|
|
|
"hasComment": len(method.Document),
|
|
|
|
"comment": strings.Join(method.Document, "\n"),
|
|
|
|
"comment": strings.Join(method.Document, "\n"),
|
|
|
|
}, filename, true)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
functionList = append(functionList, buffer.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return functionList, nil
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|