This commit is contained in:
kesonan 2024-04-01 23:22:29 +08:00 committed by GitHub
parent 8c0bb27136
commit 2d3ebb9b62
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 9 deletions

View File

@ -11,15 +11,17 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/format" "github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/zeromicro/go-zero/tools/goctl/util/stringx"
) )
//go:embed main.tpl //go:embed main.tpl
var mainTemplate string var mainTemplate string
type MainServiceTemplateData struct { type MainServiceTemplateData struct {
Service string GRPCService string
ServerPkg string Service string
Pkg string ServerPkg string
Pkg string
} }
// GenMain generates the main file of the rpc service, which is an rpc service program call entry // GenMain generates the main file of the rpc service, which is an rpc service program call entry
@ -57,9 +59,10 @@ func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config
} }
imports = append(imports, remoteImport) imports = append(imports, remoteImport)
serviceNames = append(serviceNames, MainServiceTemplateData{ serviceNames = append(serviceNames, MainServiceTemplateData{
Service: parser.CamelCase(e.Name), GRPCService: parser.CamelCase(e.Name),
ServerPkg: serverPkg, Service: stringx.From(e.Name).ToCamel(),
Pkg: proto.PbPackage, ServerPkg: serverPkg,
Pkg: proto.PbPackage,
}) })
} }

View File

@ -92,7 +92,7 @@ func (g *Generator) genServerGroup(ctx DirContext, proto parser.Proto, cfg *conf
if err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]any{ if err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]any{
"head": head, "head": head,
"unimplementedServer": fmt.Sprintf("%s.Unimplemented%sServer", proto.PbPackage, "unimplementedServer": fmt.Sprintf("%s.Unimplemented%sServer", proto.PbPackage,
stringx.From(service.Name).ToCamel()), parser.CamelCase(service.Name)),
"server": stringx.From(service.Name).ToCamel(), "server": stringx.From(service.Name).ToCamel(),
"imports": strings.Join(imports.KeysStr(), pathx.NL), "imports": strings.Join(imports.KeysStr(), pathx.NL),
"funcs": strings.Join(funcList, pathx.NL), "funcs": strings.Join(funcList, pathx.NL),
@ -143,7 +143,7 @@ func (g *Generator) genServerInCompatibility(ctx DirContext, proto parser.Proto,
return util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]any{ return util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]any{
"head": head, "head": head,
"unimplementedServer": fmt.Sprintf("%s.Unimplemented%sServer", proto.PbPackage, "unimplementedServer": fmt.Sprintf("%s.Unimplemented%sServer", proto.PbPackage,
stringx.From(service.Name).ToCamel()), parser.CamelCase(service.Name)),
"server": stringx.From(service.Name).ToCamel(), "server": stringx.From(service.Name).ToCamel(),
"imports": strings.Join(imports.KeysStr(), pathx.NL), "imports": strings.Join(imports.KeysStr(), pathx.NL),
"funcs": strings.Join(funcList, pathx.NL), "funcs": strings.Join(funcList, pathx.NL),

View File

@ -23,7 +23,7 @@ func main() {
ctx := svc.NewServiceContext(c) ctx := svc.NewServiceContext(c)
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
{{range .serviceNames}} {{.Pkg}}.Register{{.Service}}Server(grpcServer, {{.ServerPkg}}.New{{.Service}}Server(ctx)) {{range .serviceNames}} {{.Pkg}}.Register{{.GRPCService}}Server(grpcServer, {{.ServerPkg}}.New{{.Service}}Server(ctx))
{{end}} {{end}}
if c.Mode == service.DevMode || c.Mode == service.TestMode { if c.Mode == service.DevMode || c.Mode == service.TestMode {
reflection.Register(grpcServer) reflection.Register(grpcServer)