|
|
@ -9,15 +9,29 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
const (
|
|
|
|
remoteTemplate = `{{.head}}
|
|
|
|
handlerTemplate = `{{.head}}
|
|
|
|
|
|
|
|
|
|
|
|
package handler
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import {{.imports}}
|
|
|
|
import (
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{{.imports}}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type {{.types}}
|
|
|
|
type {{.types}}
|
|
|
|
|
|
|
|
|
|
|
|
{{.newFuncs}}
|
|
|
|
func New{{.server}}Server(svcCtx *svc.ServiceContext) *{{.server}}Server {
|
|
|
|
|
|
|
|
return &{{.server}}Server{
|
|
|
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{{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)
|
|
|
|
|
|
|
|
}
|
|
|
|
`
|
|
|
|
`
|
|
|
|
functionTemplate = `{{.head}}
|
|
|
|
functionTemplate = `{{.head}}
|
|
|
|
|
|
|
|
|
|
|
@ -29,8 +43,6 @@ import (
|
|
|
|
{{.imports}}
|
|
|
|
{{.imports}}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type {{.server}}Server struct{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{{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)
|
|
|
@ -47,29 +59,35 @@ func (s *{{.server}}Server) {{.method}} (ctx context.Context, in *{{.package}}.{
|
|
|
|
}`
|
|
|
|
}`
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (g *defaultRpcGenerator) genRemoteHandler() error {
|
|
|
|
func (g *defaultRpcGenerator) genHandler() error {
|
|
|
|
handlerPath := g.dirM[dirHandler]
|
|
|
|
handlerPath := g.dirM[dirHandler]
|
|
|
|
serverGo := fmt.Sprintf("%vhandler.go", g.Ctx.ServiceName.Lower())
|
|
|
|
filename := fmt.Sprintf("%vhandler.go", g.Ctx.ServiceName.Lower())
|
|
|
|
fileName := filepath.Join(handlerPath, serverGo)
|
|
|
|
handlerFile := filepath.Join(handlerPath, filename)
|
|
|
|
file := g.ast
|
|
|
|
file := g.ast
|
|
|
|
|
|
|
|
pkg := file.Package
|
|
|
|
|
|
|
|
pbImport := fmt.Sprintf(`%v "%v"`, pkg, g.mustGetPackage(dirPb))
|
|
|
|
|
|
|
|
logicImport := fmt.Sprintf(`"%v"`, g.mustGetPackage(dirLogic))
|
|
|
|
svcImport := fmt.Sprintf(`"%v"`, g.mustGetPackage(dirSvc))
|
|
|
|
svcImport := fmt.Sprintf(`"%v"`, g.mustGetPackage(dirSvc))
|
|
|
|
|
|
|
|
imports := []string{
|
|
|
|
|
|
|
|
pbImport,
|
|
|
|
|
|
|
|
logicImport,
|
|
|
|
|
|
|
|
svcImport,
|
|
|
|
|
|
|
|
}
|
|
|
|
types := make([]string, 0)
|
|
|
|
types := make([]string, 0)
|
|
|
|
newFuncs := 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()))
|
|
|
|
types = append(types, fmt.Sprintf(typeFmt, service.Name.Title()))
|
|
|
|
newFuncs = append(newFuncs, fmt.Sprintf(newFuncFmt, service.Name.Title(), service.Name.Title(), service.Name.Title()))
|
|
|
|
newFuncs = append(newFuncs, fmt.Sprintf(newFuncFmt, service.Name.Title(),
|
|
|
|
|
|
|
|
service.Name.Title(), service.Name.Title()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := util.With("server").GoFmt(true).Parse(remoteTemplate).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": strings.Join(types, "\n"),
|
|
|
|
"newFuncs": strings.Join(newFuncs, "\n"),
|
|
|
|
"newFuncs": strings.Join(newFuncs, "\n"),
|
|
|
|
"imports": svcImport,
|
|
|
|
"imports": strings.Join(imports, "\n\t"),
|
|
|
|
}, fileName, true)
|
|
|
|
}, handlerFile, true)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.genFunctions()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (g *defaultRpcGenerator) genFunctions() error {
|
|
|
|
func (g *defaultRpcGenerator) genFunctions() error {
|
|
|
@ -89,19 +107,20 @@ func (g *defaultRpcGenerator) genFunctions() error {
|
|
|
|
err := util.With("func").GoFmt(true).Parse(functionTemplate).SaveTo(map[string]interface{}{
|
|
|
|
err := util.With("func").GoFmt(true).Parse(functionTemplate).SaveTo(map[string]interface{}{
|
|
|
|
"head": head,
|
|
|
|
"head": head,
|
|
|
|
"server": service.Name.Title(),
|
|
|
|
"server": service.Name.Title(),
|
|
|
|
"imports": strings.Join(handlerImports, "\r\n"),
|
|
|
|
"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,
|
|
|
|
"request": method.InType,
|
|
|
|
"request": method.InType,
|
|
|
|
"response": method.OutType,
|
|
|
|
"response": method.OutType,
|
|
|
|
"hasComment": len(method.Document),
|
|
|
|
"hasComment": len(method.Document),
|
|
|
|
"comment": strings.Join(method.Document, "\r\n"),
|
|
|
|
"comment": strings.Join(method.Document, "\n"),
|
|
|
|
}, filename, true)
|
|
|
|
}, filename, true)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|