update handler generation (#27)
* add execute files * add protoc-osx * add rpc generation * add rpc generation * add: rpc template generation * update usage * fixed env prepare for project in go path * optimize gomod cache * add README.md * format error * reactor templatex.go * remove waste code * update project.go & README.md * update project.go & README.md
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/ctx"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||
"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}}
|
||||
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)
|
||||
}
|
||||
{{.funcs}}
|
||||
`
|
||||
functionTemplate = `{{.head}}
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
{{.imports}}
|
||||
)
|
||||
|
||||
functionTemplate = `
|
||||
{{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)
|
||||
@@ -52,17 +40,10 @@ func (s *{{.server}}Server) {{.method}} (ctx context.Context, in *{{.package}}.{
|
||||
typeFmt = `%sServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}`
|
||||
newFuncFmt = `func New%sServer(svcCtx *svc.ServiceContext) *%sServer {
|
||||
return &%sServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}`
|
||||
)
|
||||
|
||||
func (g *defaultRpcGenerator) genHandler() error {
|
||||
handlerPath := g.dirM[dirHandler]
|
||||
filename := fmt.Sprintf("%vhandler.go", g.Ctx.ServiceName.Lower())
|
||||
handlerFile := filepath.Join(handlerPath, filename)
|
||||
file := g.ast
|
||||
pkg := file.Package
|
||||
pbImport := fmt.Sprintf(`%v "%v"`, pkg, g.mustGetPackage(dirPb))
|
||||
@@ -73,54 +54,47 @@ func (g *defaultRpcGenerator) genHandler() error {
|
||||
logicImport,
|
||||
svcImport,
|
||||
}
|
||||
types := make([]string, 0)
|
||||
newFuncs := make([]string, 0)
|
||||
head := util.GetHead(g.Ctx.ProtoSource)
|
||||
for _, service := range file.Service {
|
||||
types = append(types, fmt.Sprintf(typeFmt, service.Name.Title()))
|
||||
newFuncs = append(newFuncs, fmt.Sprintf(newFuncFmt, service.Name.Title(),
|
||||
service.Name.Title(), service.Name.Title()))
|
||||
}
|
||||
|
||||
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 {
|
||||
handlerPath := g.dirM[dirHandler]
|
||||
file := g.ast
|
||||
pkg := file.Package
|
||||
|
||||
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 {
|
||||
handlerName := fmt.Sprintf("%shandler.go", method.Name.Lower())
|
||||
filename := filepath.Join(handlerPath, handlerName)
|
||||
// override
|
||||
err := util.With("func").GoFmt(true).Parse(functionTemplate).SaveTo(map[string]interface{}{
|
||||
"head": head,
|
||||
"server": service.Name.Title(),
|
||||
"imports": strings.Join(handlerImports, "\n"),
|
||||
"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
|
||||
}
|
||||
filename := fmt.Sprintf("%vhandler.go", service.Name.Lower())
|
||||
handlerFile := filepath.Join(handlerPath, filename)
|
||||
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
|
||||
}
|
||||
|
||||
func (g *defaultRpcGenerator) genFunctions(service *parser.RpcService) ([]string, error) {
|
||||
file := g.ast
|
||||
pkg := file.Package
|
||||
var functionList []string
|
||||
for _, method := range service.Funcs {
|
||||
buffer, err := util.With("func").Parse(functionTemplate).Execute(map[string]interface{}{
|
||||
"server": service.Name.Title(),
|
||||
"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"),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
functionList = append(functionList, buffer.String())
|
||||
}
|
||||
return functionList, nil
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/dsymonds/gotoc/parser"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/lang"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
||||
astParser "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||
@@ -72,8 +73,7 @@ func (g *defaultRpcGenerator) genPb() error {
|
||||
|
||||
func (g *defaultRpcGenerator) protocGenGo(target string) error {
|
||||
src := filepath.Dir(g.Ctx.ProtoFileSrc)
|
||||
sh := fmt.Sprintf(`export PATH=%s:$PATH
|
||||
protoc -I=%s --go_out=plugins=grpc:%s %s`, filepath.Join(g.Ctx.GoPath, "bin"), src, target, g.Ctx.ProtoFileSrc)
|
||||
sh := fmt.Sprintf(`protoc -I=%s --go_out=plugins=grpc:%s %s`, src, target, g.Ctx.ProtoFileSrc)
|
||||
stdout, err := execx.Run(sh)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gogen
|
||||
package gen
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
|
||||
Reference in New Issue
Block a user