You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
241 lines
5.9 KiB
Go
241 lines
5.9 KiB
Go
4 years ago
|
package gen
|
||
4 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"path/filepath"
|
||
|
"strings"
|
||
|
|
||
4 years ago
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
||
4 years ago
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||
|
)
|
||
|
|
||
4 years ago
|
const (
|
||
4 years ago
|
callTemplateText = `{{.head}}
|
||
4 years ago
|
|
||
4 years ago
|
//go:generate mockgen -destination ./{{.name}}_mock.go -package {{.filePackage}} -source $GOFILE
|
||
4 years ago
|
|
||
|
package {{.filePackage}}
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
{{.package}}
|
||
4 years ago
|
|
||
4 years ago
|
"github.com/tal-tech/go-zero/core/jsonx"
|
||
|
"github.com/tal-tech/go-zero/rpcx"
|
||
|
)
|
||
|
|
||
|
type (
|
||
4 years ago
|
{{.serviceName}} interface {
|
||
4 years ago
|
{{.interface}}
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
default{{.serviceName}} struct {
|
||
4 years ago
|
cli rpcx.Client
|
||
|
}
|
||
|
)
|
||
|
|
||
4 years ago
|
func New{{.serviceName}}(cli rpcx.Client) {{.serviceName}} {
|
||
|
return &default{{.serviceName}}{
|
||
4 years ago
|
cli: cli,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
{{.functions}}
|
||
|
`
|
||
4 years ago
|
callTemplateTypes = `{{.head}}
|
||
4 years ago
|
|
||
|
package {{.filePackage}}
|
||
|
|
||
4 years ago
|
import "errors"
|
||
4 years ago
|
|
||
4 years ago
|
var errJsonConvert = errors.New("json convert error")
|
||
4 years ago
|
|
||
|
{{.types}}
|
||
|
`
|
||
4 years ago
|
callInterfaceFunctionTemplate = `{{if .hasComment}}{{.comment}}
|
||
4 years ago
|
{{end}}{{.method}}(ctx context.Context,in *{{.pbRequest}}) {{if .hasResponse}}(*{{.pbResponse}},{{end}} error{{if .hasResponse}}){{end}}`
|
||
4 years ago
|
callFunctionTemplate = `
|
||
4 years ago
|
{{if .hasComment}}{{.comment}}{{end}}
|
||
4 years ago
|
func (m *default{{.rpcServiceName}}) {{.method}}(ctx context.Context,in *{{.pbRequest}}) {{if .hasResponse}}(*{{.pbResponse}},{{end}} error{{if .hasResponse}}){{end}} {
|
||
4 years ago
|
var request {{.package}}.{{.pbRequest}}
|
||
|
bts, err := jsonx.Marshal(in)
|
||
|
if err != nil {
|
||
4 years ago
|
return {{if .hasResponse}}nil, {{end}}errJsonConvert
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
err = jsonx.Unmarshal(bts, &request)
|
||
|
if err != nil {
|
||
4 years ago
|
return {{if .hasResponse}}nil, {{end}}errJsonConvert
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
client := {{.package}}.New{{.rpcServiceName}}Client(m.cli.Conn())
|
||
4 years ago
|
{{if .hasResponse}}resp, err := {{else}}_, err = {{end}}client.{{.method}}(ctx, &request)
|
||
|
{{if .hasResponse}}if err != nil{
|
||
|
return nil, err
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
var ret {{.pbResponse}}
|
||
4 years ago
|
bts, err = jsonx.Marshal(resp)
|
||
|
if err != nil{
|
||
|
return nil, errJsonConvert
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
err = jsonx.Unmarshal(bts, &ret)
|
||
|
if err != nil{
|
||
|
return nil, errJsonConvert
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
return &ret, nil{{else}}if err != nil {
|
||
4 years ago
|
return err
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
return nil{{end}}
|
||
4 years ago
|
}
|
||
|
`
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
func (g *defaultRpcGenerator) genCall() error {
|
||
4 years ago
|
file := g.ast
|
||
4 years ago
|
if len(file.Service) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
if len(file.Service) > 1 {
|
||
|
return fmt.Errorf("we recommend only one service in a proto, currently %d", len(file.Service))
|
||
|
}
|
||
|
|
||
4 years ago
|
typeCode, err := file.GenTypesCode()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
4 years ago
|
service := file.Service[0]
|
||
|
callPath, err := filepath.Abs(service.Name.Lower())
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err = util.MkdirIfNotExist(callPath); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
4 years ago
|
pbPkg := file.Package
|
||
|
remotePackage := fmt.Sprintf(`%v "%v"`, pbPkg, g.mustGetPackage(dirPb))
|
||
4 years ago
|
filename := filepath.Join(callPath, "types.go")
|
||
4 years ago
|
head := util.GetHead(g.Ctx.ProtoSource)
|
||
4 years ago
|
err = util.With("types").GoFmt(true).Parse(callTemplateTypes).SaveTo(map[string]interface{}{
|
||
4 years ago
|
"head": head,
|
||
4 years ago
|
"filePackage": service.Name.Lower(),
|
||
4 years ago
|
"pbPkg": pbPkg,
|
||
|
"serviceName": g.Ctx.ServiceName.Title(),
|
||
|
"lowerStartServiceName": g.Ctx.ServiceName.UnTitle(),
|
||
|
"types": typeCode,
|
||
|
}, filename, true)
|
||
4 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
_, err = exec.LookPath("mockgen")
|
||
|
mockGenInstalled := err == nil
|
||
4 years ago
|
filename = filepath.Join(callPath, fmt.Sprintf("%s.go", service.Name.Lower()))
|
||
|
functions, err := g.getFuncs(service)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
iFunctions, err := g.getInterfaceFuncs(service)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
mockFile := filepath.Join(callPath, fmt.Sprintf("%s_mock.go", service.Name.Lower()))
|
||
|
os.Remove(mockFile)
|
||
|
err = util.With("shared").GoFmt(true).Parse(callTemplateText).SaveTo(map[string]interface{}{
|
||
|
"name": service.Name.Lower(),
|
||
|
"head": head,
|
||
|
"filePackage": service.Name.Lower(),
|
||
|
"pbPkg": pbPkg,
|
||
|
"package": remotePackage,
|
||
|
"serviceName": service.Name.Title(),
|
||
|
"functions": strings.Join(functions, "\n"),
|
||
|
"interface": strings.Join(iFunctions, "\n"),
|
||
|
}, filename, true)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
// if mockgen is already installed, it will generate code of gomock for shared files
|
||
|
_, err = exec.LookPath("mockgen")
|
||
|
if mockGenInstalled {
|
||
|
execx.Run(fmt.Sprintf("go generate %s", filename))
|
||
4 years ago
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (g *defaultRpcGenerator) getFuncs(service *parser.RpcService) ([]string, error) {
|
||
|
file := g.ast
|
||
|
pkgName := file.Package
|
||
|
functions := make([]string, 0)
|
||
|
for _, method := range service.Funcs {
|
||
|
data, found := file.Strcuts[strings.ToLower(method.OutType)]
|
||
|
if found {
|
||
|
found = len(data.Field) > 0
|
||
|
}
|
||
|
var comment string
|
||
|
if len(method.Document) > 0 {
|
||
|
comment = method.Document[0]
|
||
|
}
|
||
4 years ago
|
buffer, err := util.With("sharedFn").Parse(callFunctionTemplate).Execute(map[string]interface{}{
|
||
4 years ago
|
"rpcServiceName": service.Name.Title(),
|
||
|
"method": method.Name.Title(),
|
||
|
"package": pkgName,
|
||
|
"pbRequest": method.InType,
|
||
|
"pbResponse": method.OutType,
|
||
|
"hasResponse": found,
|
||
|
"hasComment": len(method.Document) > 0,
|
||
|
"comment": comment,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
functions = append(functions, buffer.String())
|
||
|
}
|
||
|
return functions, nil
|
||
|
}
|
||
|
|
||
|
func (g *defaultRpcGenerator) getInterfaceFuncs(service *parser.RpcService) ([]string, error) {
|
||
|
file := g.ast
|
||
|
functions := make([]string, 0)
|
||
4 years ago
|
|
||
4 years ago
|
for _, method := range service.Funcs {
|
||
|
data, found := file.Strcuts[strings.ToLower(method.OutType)]
|
||
|
if found {
|
||
|
found = len(data.Field) > 0
|
||
|
}
|
||
|
var comment string
|
||
|
if len(method.Document) > 0 {
|
||
|
comment = method.Document[0]
|
||
|
}
|
||
4 years ago
|
buffer, err := util.With("interfaceFn").Parse(callInterfaceFunctionTemplate).Execute(
|
||
|
map[string]interface{}{
|
||
|
"hasComment": len(method.Document) > 0,
|
||
|
"comment": comment,
|
||
|
"method": method.Name.Title(),
|
||
|
"pbRequest": method.InType,
|
||
|
"pbResponse": method.OutType,
|
||
|
"hasResponse": found,
|
||
|
})
|
||
4 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
functions = append(functions, buffer.String())
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
return functions, nil
|
||
|
}
|