|
|
@ -13,9 +13,9 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
const (
|
|
|
|
sharedTemplateText = `{{.head}}
|
|
|
|
callTemplateText = `{{.head}}
|
|
|
|
|
|
|
|
|
|
|
|
//go:generate mockgen -destination ./{{.name}}model_mock.go -package {{.filePackage}} -source $GOFILE
|
|
|
|
//go:generate mockgen -destination ./{{.name}}_mock.go -package {{.filePackage}} -source $GOFILE
|
|
|
|
|
|
|
|
|
|
|
|
package {{.filePackage}}
|
|
|
|
package {{.filePackage}}
|
|
|
|
|
|
|
|
|
|
|
@ -29,24 +29,24 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
type (
|
|
|
|
{{.serviceName}}Model interface {
|
|
|
|
{{.serviceName}} interface {
|
|
|
|
{{.interface}}
|
|
|
|
{{.interface}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
default{{.serviceName}}Model struct {
|
|
|
|
default{{.serviceName}} struct {
|
|
|
|
cli rpcx.Client
|
|
|
|
cli rpcx.Client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func New{{.serviceName}}Model(cli rpcx.Client) {{.serviceName}}Model {
|
|
|
|
func New{{.serviceName}}(cli rpcx.Client) {{.serviceName}} {
|
|
|
|
return &default{{.serviceName}}Model{
|
|
|
|
return &default{{.serviceName}}{
|
|
|
|
cli: cli,
|
|
|
|
cli: cli,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{{.functions}}
|
|
|
|
{{.functions}}
|
|
|
|
`
|
|
|
|
`
|
|
|
|
sharedTemplateTypes = `{{.head}}
|
|
|
|
callTemplateTypes = `{{.head}}
|
|
|
|
|
|
|
|
|
|
|
|
package {{.filePackage}}
|
|
|
|
package {{.filePackage}}
|
|
|
|
|
|
|
|
|
|
|
@ -56,11 +56,11 @@ var errJsonConvert = errors.New("json convert error")
|
|
|
|
|
|
|
|
|
|
|
|
{{.types}}
|
|
|
|
{{.types}}
|
|
|
|
`
|
|
|
|
`
|
|
|
|
sharedInterfaceFunctionTemplate = `{{if .hasComment}}{{.comment}}
|
|
|
|
callInterfaceFunctionTemplate = `{{if .hasComment}}{{.comment}}
|
|
|
|
{{end}}{{.method}}(ctx context.Context,in *{{.pbRequest}}) {{if .hasResponse}}(*{{.pbResponse}},{{end}} error{{if .hasResponse}}){{end}}`
|
|
|
|
{{end}}{{.method}}(ctx context.Context,in *{{.pbRequest}}) {{if .hasResponse}}(*{{.pbResponse}},{{end}} error{{if .hasResponse}}){{end}}`
|
|
|
|
sharedFunctionTemplate = `
|
|
|
|
callFunctionTemplate = `
|
|
|
|
{{if .hasComment}}{{.comment}}{{end}}
|
|
|
|
{{if .hasComment}}{{.comment}}{{end}}
|
|
|
|
func (m *default{{.rpcServiceName}}Model) {{.method}}(ctx context.Context,in *{{.pbRequest}}) {{if .hasResponse}}(*{{.pbResponse}},{{end}} error{{if .hasResponse}}){{end}} {
|
|
|
|
func (m *default{{.rpcServiceName}}) {{.method}}(ctx context.Context,in *{{.pbRequest}}) {{if .hasResponse}}(*{{.pbResponse}},{{end}} error{{if .hasResponse}}){{end}} {
|
|
|
|
var request {{.package}}.{{.pbRequest}}
|
|
|
|
var request {{.package}}.{{.pbRequest}}
|
|
|
|
bts, err := jsonx.Marshal(in)
|
|
|
|
bts, err := jsonx.Marshal(in)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -98,59 +98,78 @@ func (m *default{{.rpcServiceName}}Model) {{.method}}(ctx context.Context,in *{{
|
|
|
|
`
|
|
|
|
`
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (g *defaultRpcGenerator) genShared() error {
|
|
|
|
func (g *defaultRpcGenerator) genCall() error {
|
|
|
|
sharePackage := filepath.Base(g.Ctx.SharedDir)
|
|
|
|
|
|
|
|
file := g.ast
|
|
|
|
file := g.ast
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typeCode, err := file.GenTypesCode()
|
|
|
|
typeCode, err := file.GenTypesCode()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pbPkg := file.Package
|
|
|
|
pbPkg := file.Package
|
|
|
|
remotePackage := fmt.Sprintf(`%v "%v"`, pbPkg, g.mustGetPackage(dirPb))
|
|
|
|
remotePackage := fmt.Sprintf(`%v "%v"`, pbPkg, g.mustGetPackage(dirPb))
|
|
|
|
filename := filepath.Join(g.Ctx.SharedDir, "types.go")
|
|
|
|
filename := filepath.Join(callPath, "types.go")
|
|
|
|
head := util.GetHead(g.Ctx.ProtoSource)
|
|
|
|
head := util.GetHead(g.Ctx.ProtoSource)
|
|
|
|
err = util.With("types").GoFmt(true).Parse(sharedTemplateTypes).SaveTo(map[string]interface{}{
|
|
|
|
err = util.With("types").GoFmt(true).Parse(callTemplateTypes).SaveTo(map[string]interface{}{
|
|
|
|
"head": head,
|
|
|
|
"head": head,
|
|
|
|
"filePackage": sharePackage,
|
|
|
|
"filePackage": service.Name.Lower(),
|
|
|
|
"pbPkg": pbPkg,
|
|
|
|
"pbPkg": pbPkg,
|
|
|
|
"serviceName": g.Ctx.ServiceName.Title(),
|
|
|
|
"serviceName": g.Ctx.ServiceName.Title(),
|
|
|
|
"lowerStartServiceName": g.Ctx.ServiceName.UnTitle(),
|
|
|
|
"lowerStartServiceName": g.Ctx.ServiceName.UnTitle(),
|
|
|
|
"types": typeCode,
|
|
|
|
"types": typeCode,
|
|
|
|
}, filename, true)
|
|
|
|
}, filename, true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, err = exec.LookPath("mockgen")
|
|
|
|
_, err = exec.LookPath("mockgen")
|
|
|
|
mockGenInstalled := err == nil
|
|
|
|
mockGenInstalled := err == nil
|
|
|
|
for _, service := range file.Service {
|
|
|
|
filename = filepath.Join(callPath, fmt.Sprintf("%s.go", service.Name.Lower()))
|
|
|
|
filename := filepath.Join(g.Ctx.SharedDir, fmt.Sprintf("%smodel.go", service.Name.Lower()))
|
|
|
|
functions, err := g.getFuncs(service)
|
|
|
|
functions, err := g.getFuncs(service)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iFunctions, err := g.getInterfaceFuncs(service)
|
|
|
|
iFunctions, err := g.getInterfaceFuncs(service)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mockFile := filepath.Join(g.Ctx.SharedDir, fmt.Sprintf("%smodel_mock.go", service.Name.Lower()))
|
|
|
|
|
|
|
|
os.Remove(mockFile)
|
|
|
|
mockFile := filepath.Join(callPath, fmt.Sprintf("%s_mock.go", service.Name.Lower()))
|
|
|
|
err = util.With("shared").GoFmt(true).Parse(sharedTemplateText).SaveTo(map[string]interface{}{
|
|
|
|
os.Remove(mockFile)
|
|
|
|
"name": service.Name.Lower(),
|
|
|
|
err = util.With("shared").GoFmt(true).Parse(callTemplateText).SaveTo(map[string]interface{}{
|
|
|
|
"head": head,
|
|
|
|
"name": service.Name.Lower(),
|
|
|
|
"filePackage": sharePackage,
|
|
|
|
"head": head,
|
|
|
|
"pbPkg": pbPkg,
|
|
|
|
"filePackage": service.Name.Lower(),
|
|
|
|
"package": remotePackage,
|
|
|
|
"pbPkg": pbPkg,
|
|
|
|
"serviceName": service.Name.Title(),
|
|
|
|
"package": remotePackage,
|
|
|
|
"functions": strings.Join(functions, "\n"),
|
|
|
|
"serviceName": service.Name.Title(),
|
|
|
|
"interface": strings.Join(iFunctions, "\n"),
|
|
|
|
"functions": strings.Join(functions, "\n"),
|
|
|
|
}, filename, true)
|
|
|
|
"interface": strings.Join(iFunctions, "\n"),
|
|
|
|
if err != nil {
|
|
|
|
}, filename, true)
|
|
|
|
return err
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
// if mockgen is already installed, it will generate code of gomock for shared files
|
|
|
|
}
|
|
|
|
_, err = exec.LookPath("mockgen")
|
|
|
|
// if mockgen is already installed, it will generate code of gomock for shared files
|
|
|
|
if mockGenInstalled {
|
|
|
|
_, err = exec.LookPath("mockgen")
|
|
|
|
execx.Run(fmt.Sprintf("go generate %s", filename))
|
|
|
|
if mockGenInstalled {
|
|
|
|
}
|
|
|
|
execx.Run(fmt.Sprintf("go generate %s", filename))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
@ -169,7 +188,7 @@ func (g *defaultRpcGenerator) getFuncs(service *parser.RpcService) ([]string, er
|
|
|
|
if len(method.Document) > 0 {
|
|
|
|
if len(method.Document) > 0 {
|
|
|
|
comment = method.Document[0]
|
|
|
|
comment = method.Document[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer, err := util.With("sharedFn").Parse(sharedFunctionTemplate).Execute(map[string]interface{}{
|
|
|
|
buffer, err := util.With("sharedFn").Parse(callFunctionTemplate).Execute(map[string]interface{}{
|
|
|
|
"rpcServiceName": service.Name.Title(),
|
|
|
|
"rpcServiceName": service.Name.Title(),
|
|
|
|
"method": method.Name.Title(),
|
|
|
|
"method": method.Name.Title(),
|
|
|
|
"package": pkgName,
|
|
|
|
"package": pkgName,
|
|
|
@ -191,6 +210,7 @@ func (g *defaultRpcGenerator) getFuncs(service *parser.RpcService) ([]string, er
|
|
|
|
func (g *defaultRpcGenerator) getInterfaceFuncs(service *parser.RpcService) ([]string, error) {
|
|
|
|
func (g *defaultRpcGenerator) getInterfaceFuncs(service *parser.RpcService) ([]string, error) {
|
|
|
|
file := g.ast
|
|
|
|
file := g.ast
|
|
|
|
functions := make([]string, 0)
|
|
|
|
functions := make([]string, 0)
|
|
|
|
|
|
|
|
|
|
|
|
for _, method := range service.Funcs {
|
|
|
|
for _, method := range service.Funcs {
|
|
|
|
data, found := file.Strcuts[strings.ToLower(method.OutType)]
|
|
|
|
data, found := file.Strcuts[strings.ToLower(method.OutType)]
|
|
|
|
if found {
|
|
|
|
if found {
|
|
|
@ -200,19 +220,21 @@ func (g *defaultRpcGenerator) getInterfaceFuncs(service *parser.RpcService) ([]s
|
|
|
|
if len(method.Document) > 0 {
|
|
|
|
if len(method.Document) > 0 {
|
|
|
|
comment = method.Document[0]
|
|
|
|
comment = method.Document[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer, err := util.With("interfaceFn").Parse(sharedInterfaceFunctionTemplate).Execute(map[string]interface{}{
|
|
|
|
buffer, err := util.With("interfaceFn").Parse(callInterfaceFunctionTemplate).Execute(
|
|
|
|
"hasComment": len(method.Document) > 0,
|
|
|
|
map[string]interface{}{
|
|
|
|
"comment": comment,
|
|
|
|
"hasComment": len(method.Document) > 0,
|
|
|
|
"method": method.Name.Title(),
|
|
|
|
"comment": comment,
|
|
|
|
"pbRequest": method.InType,
|
|
|
|
"method": method.Name.Title(),
|
|
|
|
"pbResponse": method.OutType,
|
|
|
|
"pbRequest": method.InType,
|
|
|
|
"hasResponse": found,
|
|
|
|
"pbResponse": method.OutType,
|
|
|
|
})
|
|
|
|
"hasResponse": found,
|
|
|
|
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
functions = append(functions, buffer.String())
|
|
|
|
functions = append(functions, buffer.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return functions, nil
|
|
|
|
return functions, nil
|
|
|
|
}
|
|
|
|
}
|