diff --git a/tools/goctl/internal/flags/default_en.json b/tools/goctl/internal/flags/default_en.json index 2b86cdda89e2..1373a5dd03ce 100644 --- a/tools/goctl/internal/flags/default_en.json +++ b/tools/goctl/internal/flags/default_en.json @@ -259,7 +259,8 @@ "remote": "{{.global.remote}}", "branch": "{{.global.branch}}", "verbose": "Enable log output", - "client": "Whether to generate rpc client" + "client": "Whether to generate rpc client", + "name": "Service name. Setting the rpc service name prevents it from defaulting to the proto file name. This enables the use of multiple proto files within the same service for code generation" } }, "template": { diff --git a/tools/goctl/rpc/README.md b/tools/goctl/rpc/README.md index 2394fbd1bb8b..d084bf36ca72 100644 --- a/tools/goctl/rpc/README.md +++ b/tools/goctl/rpc/README.md @@ -105,6 +105,7 @@ Flags: --style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md] (default "gozero") -v, --verbose Enable log output --zrpc_out string The zrpc output directory + --name string Rpc service name. Setting the rpc service name prevents it from defaulting to the proto file name. This enables the use of multiple proto files within the same service for code generation. ``` ### 参数说明 @@ -115,6 +116,7 @@ Flags: * --style 指定文件输出格式 * -v, --verbose 显示日志 * --zrpc_out 指定zrpc输出目录 +* --name 指定rpc服务名,如果如果不设置就使用proto文件名作为rpc服务名称,同时也允许在同一个rpc服务中使用多个proto文件来生成代码 > ## --multiple > 是否开启多个 rpc service 生成,如果开启,则满足一下新特性 @@ -200,4 +202,49 @@ hello └── hello ├── hello.pb.go └── hello_grpc.pb.go +``` + +### --name +分别执行以下命令 +```shell +goctl.exe rpc protoc app/pb/user.proto --go_out=./app/pb --go-grpc_out=./app/pb --zrpc_out=./app --style=go_zero -m --name app +goctl.exe rpc protoc app/pb/role.proto --go_out=./app/pb --go-grpc_out=./app/pb --zrpc_out=./app --style=go_zero -m --name app +``` +生成目录结构如下: +```text +app/ +├── app.go +├── client/ +│ ├── roleservice/ +│ │ └── role_service.go +│ └── userservice/ +│ └── user_service.go +├── etc/ +│ └── app.yaml +├── internal/ +│ ├── config/ +│ │ └── config.go +│ ├── logic/ +│ │ ├── roleservice/ +│ │ │ ├── create_role_logic.go +│ │ │ └── update_role_logic.go +│ │ └── userservice/ +│ │ ├── create_user_logic.go +│ │ └── user_detail_logic.go +│ ├── server/ +│ │ ├── roleservice/ +│ │ │ └── role_service_server.go +│ │ └── userservice/ +│ │ └── user_service_server.go +│ └── svc/ +│ └── service_context.go +└── pb/ + ├── role/ + │ ├── role.pb.go + │ └── role_grpc.pb.go + ├── user/ + │ ├── user.pb.go + │ └── user_grpc.pb.go + ├── user.proto + └── role.proto ``` \ No newline at end of file diff --git a/tools/goctl/rpc/cli/cli.go b/tools/goctl/rpc/cli/cli.go index 09193052a34b..12ac5827fe7e 100644 --- a/tools/goctl/rpc/cli/cli.go +++ b/tools/goctl/rpc/cli/cli.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/spf13/cobra" + "github.com/zeromicro/go-zero/tools/goctl/rpc/generator" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/console" @@ -38,6 +39,8 @@ var ( VarStringStyle string // VarStringZRPCOut describes the zRPC output. VarStringZRPCOut string + // VarStringName describes the service name + VarStringName string // VarBoolIdea describes whether idea or not VarBoolIdea bool // VarBoolVerbose describes whether verbose. @@ -93,7 +96,10 @@ func RPCNew(_ *cobra.Command, args []string) error { ctx.Output = filepath.Dir(src) ctx.ProtocCmd = fmt.Sprintf("protoc -I=%s %s --go_out=%s --go-grpc_out=%s", filepath.Dir(src), filepath.Base(src), filepath.Dir(src), filepath.Dir(src)) ctx.IsGenClient = VarBoolClient + ctx.Name = VarStringName ctx.Module = VarStringModule + ctx.Name = VarStringName + grpcOptList := VarStringSliceGoGRPCOpt if len(grpcOptList) > 0 { diff --git a/tools/goctl/rpc/cli/zrpc.go b/tools/goctl/rpc/cli/zrpc.go index 3f42c1135ac8..1ff14d3126ca 100644 --- a/tools/goctl/rpc/cli/zrpc.go +++ b/tools/goctl/rpc/cli/zrpc.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/spf13/cobra" + "github.com/zeromicro/go-zero/tools/goctl/rpc/generator" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" @@ -103,7 +104,9 @@ func ZRPC(_ *cobra.Command, args []string) error { ctx.Output = zrpcOut ctx.ProtocCmd = strings.Join(protocArgs, " ") ctx.IsGenClient = VarBoolClient + ctx.Name = VarStringName ctx.Module = VarStringModule + g := generator.NewGenerator(style, verbose) return g.Generate(&ctx) } diff --git a/tools/goctl/rpc/cmd.go b/tools/goctl/rpc/cmd.go index c474acc2c778..f77aacb69f12 100644 --- a/tools/goctl/rpc/cmd.go +++ b/tools/goctl/rpc/cmd.go @@ -2,6 +2,7 @@ package rpc import ( "github.com/spf13/cobra" + "github.com/zeromicro/go-zero/tools/goctl/config" "github.com/zeromicro/go-zero/tools/goctl/internal/cobrax" "github.com/zeromicro/go-zero/tools/goctl/rpc/cli" @@ -58,7 +59,9 @@ func init() { protocCmdFlags.StringVar(&cli.VarStringHome, "home") protocCmdFlags.StringVar(&cli.VarStringRemote, "remote") protocCmdFlags.StringVar(&cli.VarStringBranch, "branch") + protocCmdFlags.StringVar(&cli.VarStringName, "name") protocCmdFlags.StringVar(&cli.VarStringModule, "module") + protocCmdFlags.BoolVarP(&cli.VarBoolVerbose, "verbose", "v") protocCmdFlags.MarkHidden("go_out") protocCmdFlags.MarkHidden("go-grpc_out") diff --git a/tools/goctl/rpc/generator/gen.go b/tools/goctl/rpc/generator/gen.go index 9ff9aa60deaf..a8f3ad336613 100644 --- a/tools/goctl/rpc/generator/gen.go +++ b/tools/goctl/rpc/generator/gen.go @@ -26,6 +26,8 @@ type ZRpcContext struct { GrpcOutput string // Output is the output directory of the generated files. Output string + // Name is the rpc service name + Name string // Multiple is the flag to indicate whether the proto file is generated in multiple mode. Multiple bool // Whether to generate rpc client @@ -74,7 +76,7 @@ func (g *Generator) Generate(zctx *ZRpcContext) error { return err } - err = g.GenEtc(dirCtx, proto, g.cfg) + err = g.GenEtc(dirCtx, proto, g.cfg, zctx) if err != nil { return err } diff --git a/tools/goctl/rpc/generator/genetc.go b/tools/goctl/rpc/generator/genetc.go index 8729e76fa67c..3226df7709d7 100644 --- a/tools/goctl/rpc/generator/genetc.go +++ b/tools/goctl/rpc/generator/genetc.go @@ -19,9 +19,13 @@ var etcTemplate string // GenEtc generates the yaml configuration file of the rpc service, // including host, port monitoring configuration items and etcd configuration -func (g *Generator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error { +func (g *Generator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config, c *ZRpcContext) error { dir := ctx.GetEtc() - etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source()) + serviceName := c.Name + if len(serviceName) == 0 { + serviceName = ctx.GetServiceName().Source() + } + etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, serviceName) if err != nil { return err } @@ -34,6 +38,6 @@ func (g *Generator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) err } return util.With("etc").Parse(text).SaveTo(map[string]any{ - "serviceName": strings.ToLower(stringx.From(ctx.GetServiceName().Source()).ToCamel()), + "serviceName": strings.ToLower(stringx.From(serviceName).ToCamel()), }, fileName, false) } diff --git a/tools/goctl/rpc/generator/genmain.go b/tools/goctl/rpc/generator/genmain.go index e0d0849acb02..037869668a22 100644 --- a/tools/goctl/rpc/generator/genmain.go +++ b/tools/goctl/rpc/generator/genmain.go @@ -27,7 +27,11 @@ type MainServiceTemplateData struct { // GenMain generates the main file of the rpc service, which is an rpc service program call entry func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config, c *ZRpcContext) error { - mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source()) + serviceName := c.Name + if len(serviceName) == 0 { + serviceName = ctx.GetServiceName().Source() + } + mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, serviceName) if err != nil { return err } @@ -71,7 +75,7 @@ func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config return err } - etcFileName, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source()) + etcFileName, err := format.FileNamingFormat(cfg.NamingFormat, serviceName) if err != nil { return err }