Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
43dec3b
feat(rpc): add rpc_name flag for generating rpc service code
gszw90 Aug 2, 2025
c5b96d0
refactor(goctl): remove unnecessary RPC name printing
gszw90 Aug 2, 2025
2136351
Merge branch 'master' into feat-add-rpc-name
gszw90 Aug 3, 2025
65ed0c0
docs: update comment for VarStringRpcName
gszw90 Aug 3, 2025
b1db165
Merge remote-tracking branch 'origin/feat-add-rpc-name' into feat-add…
gszw90 Aug 3, 2025
07bb2f4
Merge branch 'master' into feat-add-rpc-name
gszw90 Aug 7, 2025
b6ef65a
feat(rpc): add rpc_name flag for generating rpc service code
gszw90 Aug 2, 2025
e0a203c
refactor(goctl): remove unnecessary RPC name printing
gszw90 Aug 2, 2025
312fda3
docs: update comment for VarStringRpcName
gszw90 Aug 3, 2025
eba5802
Merge branch 'master' into feat-add-rpc-name
gszw90 Aug 21, 2025
a484c26
feat(goctl): rename rpc_name to name for rpc service generating
gszw90 Aug 21, 2025
9a10291
Merge branch 'feat-add-rpc-name' of github.com:gszw90/go-zero into fe…
gszw90 Aug 21, 2025
e7c9e3b
docs(goctl): update rpc README for --name flag
gszw90 Aug 21, 2025
0e7f421
Merge branch 'master' into feat-add-rpc-name
gszw90 Aug 27, 2025
0e63294
Merge branch 'master' into feat-add-rpc-name
gszw90 Sep 5, 2025
3facbef
Merge branch 'master' into feat-add-rpc-name
gszw90 Sep 10, 2025
d303c47
Merge branch 'feat-add-rpc-name' of github.com:gszw90/go-zero into fe…
gszw90 Sep 10, 2025
7e8e3c8
Merge branch 'master' into feat-add-rpc-name
gszw90 Sep 15, 2025
5018b5b
Merge branch 'master' into feat-add-rpc-name
gszw90 Sep 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tools/goctl/internal/flags/default_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@
"remote": "{{.global.remote}}",
"branch": "{{.global.branch}}",
"verbose": "Enable log output",
"client": "Whether to generate rpc client"
"client": "Whether to generate rpc client",
"rpc_name": "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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use name instead of rpc_name?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}
},
"template": {
Expand Down
47 changes: 47 additions & 0 deletions tools/goctl/rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--rpc_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.
```

### 参数说明
Expand All @@ -115,6 +116,7 @@ Flags:
* --style 指定文件输出格式
* -v, --verbose 显示日志
* --zrpc_out 指定zrpc输出目录
* --rpc_name 指定rpc服务名,如果如果不设置就使用proto文件名作为rpc服务名称,同时也允许在同一个rpc服务中使用多个proto文件来生成代码

> ## --multiple
> 是否开启多个 rpc service 生成,如果开启,则满足一下新特性
Expand Down Expand Up @@ -200,4 +202,49 @@ hello
└── hello
├── hello.pb.go
└── hello_grpc.pb.go
```

### --rpc_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 --rpc_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 --rpc_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
```
4 changes: 4 additions & 0 deletions tools/goctl/rpc/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -38,6 +39,8 @@ var (
VarStringStyle string
// VarStringZRPCOut describes the zRPC output.
VarStringZRPCOut string
// VarStringRpcName describe the rpc name
Copy link

Copilot AI Aug 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment has a grammatical error. It should be "VarStringRpcName describes the rpc name" (missing 's' in 'describe').

Suggested change
// VarStringRpcName describe the rpc name
// VarStringRpcName describes the rpc name

Copilot uses AI. Check for mistakes.

VarStringRpcName string
// VarBoolIdea describes whether idea or not
VarBoolIdea bool
// VarBoolVerbose describes whether verbose.
Expand Down Expand Up @@ -91,6 +94,7 @@ 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.RpcName = VarStringRpcName

grpcOptList := VarStringSliceGoGRPCOpt
if len(grpcOptList) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions tools/goctl/rpc/cli/zrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -103,6 +104,7 @@ func ZRPC(_ *cobra.Command, args []string) error {
ctx.Output = zrpcOut
ctx.ProtocCmd = strings.Join(protocArgs, " ")
ctx.IsGenClient = VarBoolClient
ctx.RpcName = VarStringRpcName
g := generator.NewGenerator(style, verbose)
return g.Generate(&ctx)
}
Expand Down
2 changes: 2 additions & 0 deletions tools/goctl/rpc/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -57,6 +58,7 @@ func init() {
protocCmdFlags.StringVar(&cli.VarStringHome, "home")
protocCmdFlags.StringVar(&cli.VarStringRemote, "remote")
protocCmdFlags.StringVar(&cli.VarStringBranch, "branch")
protocCmdFlags.StringVar(&cli.VarStringRpcName, "rpc_name")
protocCmdFlags.BoolVarP(&cli.VarBoolVerbose, "verbose", "v")
protocCmdFlags.MarkHidden("go_out")
protocCmdFlags.MarkHidden("go-grpc_out")
Expand Down
4 changes: 3 additions & 1 deletion tools/goctl/rpc/generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type ZRpcContext struct {
GrpcOutput string
// Output is the output directory of the generated files.
Output string
// RpcName is the rpc service name
RpcName string
// Multiple is the flag to indicate whether the proto file is generated in multiple mode.
Multiple bool
// Whether to generate rpc client
Expand Down Expand Up @@ -67,7 +69,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
}
Expand Down
10 changes: 7 additions & 3 deletions tools/goctl/rpc/generator/genetc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.RpcName
if len(serviceName) == 0 {
serviceName = ctx.GetServiceName().Source()
}
etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, serviceName)
if err != nil {
return err
}
Expand All @@ -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)
}
8 changes: 6 additions & 2 deletions tools/goctl/rpc/generator/genmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.RpcName
if len(serviceName) == 0 {
serviceName = ctx.GetServiceName().Source()
}
mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, serviceName)
if err != nil {
return err
}
Expand Down Expand Up @@ -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
}
Expand Down
Loading