|
|
@ -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,54 +54,47 @@ 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{}{
|
|
|
|
|
|
|
|
"head": head,
|
|
|
|
|
|
|
|
"types": fmt.Sprintf(typeFmt, service.Name.Title()),
|
|
|
|
|
|
|
|
"server": service.Name.Title(),
|
|
|
|
|
|
|
|
"imports": strings.Join(imports, "\n\t"),
|
|
|
|
|
|
|
|
"funcs": strings.Join(funcList, "\n"),
|
|
|
|
|
|
|
|
}, handlerFile, true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return util.With("server").GoFmt(true).Parse(handlerTemplate).SaveTo(map[string]interface{}{
|
|
|
|
|
|
|
|
"head": head,
|
|
|
|
|
|
|
|
"types": strings.Join(types, "\n"),
|
|
|
|
|
|
|
|
"newFuncs": strings.Join(newFuncs, "\n"),
|
|
|
|
|
|
|
|
"imports": strings.Join(imports, "\n\t"),
|
|
|
|
|
|
|
|
}, handlerFile, true)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
for _, method := range service.Funcs {
|
|
|
|
handlerImports := make([]string, 0)
|
|
|
|
buffer, err := util.With("func").Parse(functionTemplate).Execute(map[string]interface{}{
|
|
|
|
pbImport := fmt.Sprintf(`%v "%v"`, pkg, g.mustGetPackage(dirPb))
|
|
|
|
"server": service.Name.Title(),
|
|
|
|
handlerImports = append(handlerImports, pbImport, fmt.Sprintf(`"%v"`, g.mustGetPackage(dirLogic)))
|
|
|
|
"logicName": fmt.Sprintf("%sLogic", method.Name.Title()),
|
|
|
|
for _, service := range file.Service {
|
|
|
|
"method": method.Name.Title(),
|
|
|
|
for _, method := range service.Funcs {
|
|
|
|
"package": pkg,
|
|
|
|
handlerName := fmt.Sprintf("%shandler.go", method.Name.Lower())
|
|
|
|
"request": method.InType,
|
|
|
|
filename := filepath.Join(handlerPath, handlerName)
|
|
|
|
"response": method.OutType,
|
|
|
|
// override
|
|
|
|
"hasComment": len(method.Document),
|
|
|
|
err := util.With("func").GoFmt(true).Parse(functionTemplate).SaveTo(map[string]interface{}{
|
|
|
|
"comment": strings.Join(method.Document, "\n"),
|
|
|
|
"head": head,
|
|
|
|
})
|
|
|
|
"server": service.Name.Title(),
|
|
|
|
if err != nil {
|
|
|
|
"imports": strings.Join(handlerImports, "\n"),
|
|
|
|
return nil, err
|
|
|
|
"logicName": fmt.Sprintf("%sLogic", method.Name.Title()),
|
|
|
|
|
|
|
|
"method": method.Name.Title(),
|
|
|
|
|
|
|
|
"package": pkg,
|
|
|
|
|
|
|
|
"request": method.InType,
|
|
|
|
|
|
|
|
"response": method.OutType,
|
|
|
|
|
|
|
|
"hasComment": len(method.Document),
|
|
|
|
|
|
|
|
"comment": strings.Join(method.Document, "\n"),
|
|
|
|
|
|
|
|
}, filename, true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
functionList = append(functionList, buffer.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return functionList, nil
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|