Skip to content

Commit b6ef65a

Browse files
gszw90kevwan
authored andcommitted
feat(rpc): add rpc_name flag for generating rpc service code
- Add --rpc_name flag to set the RPC service name - Update configuration file and main file generation to use the specified service name - Modify README to include new flag description and usage examples - Enhance support for generating code from multiple proto files within the same service
1 parent 73d6fcf commit b6ef65a

File tree

8 files changed

+76
-7
lines changed

8 files changed

+76
-7
lines changed

tools/goctl/internal/flags/default_en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@
257257
"remote": "{{.global.remote}}",
258258
"branch": "{{.global.branch}}",
259259
"verbose": "Enable log output",
260-
"client": "Whether to generate rpc client"
260+
"client": "Whether to generate rpc client",
261+
"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"
261262
}
262263
},
263264
"template": {

tools/goctl/rpc/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Flags:
105105
--style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md] (default "gozero")
106106
-v, --verbose Enable log output
107107
--zrpc_out string The zrpc output directory
108+
--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.
108109
```
109110
110111
### 参数说明
@@ -115,6 +116,7 @@ Flags:
115116
* --style 指定文件输出格式
116117
* -v, --verbose 显示日志
117118
* --zrpc_out 指定zrpc输出目录
119+
* --rpc_name 指定rpc服务名,如果如果不设置就使用proto文件名作为rpc服务名称,同时也允许在同一个rpc服务中使用多个proto文件来生成代码
118120
119121
> ## --multiple
120122
> 是否开启多个 rpc service 生成,如果开启,则满足一下新特性
@@ -200,4 +202,49 @@ hello
200202
└── hello
201203
├── hello.pb.go
202204
└── hello_grpc.pb.go
205+
```
206+
207+
### --rpc_name
208+
分别执行以下命令
209+
```shell
210+
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
211+
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
212+
```
213+
生成目录结构如下:
214+
```text
215+
app/
216+
├── app.go
217+
├── client/
218+
│ ├── roleservice/
219+
│ │ └── role_service.go
220+
│ └── userservice/
221+
│ └── user_service.go
222+
├── etc/
223+
│ └── app.yaml
224+
├── internal/
225+
│ ├── config/
226+
│ │ └── config.go
227+
│ ├── logic/
228+
│ │ ├── roleservice/
229+
│ │ │ ├── create_role_logic.go
230+
│ │ │ └── update_role_logic.go
231+
│ │ └── userservice/
232+
│ │ ├── create_user_logic.go
233+
│ │ └── user_detail_logic.go
234+
│ ├── server/
235+
│ │ ├── roleservice/
236+
│ │ │ └── role_service_server.go
237+
│ │ └── userservice/
238+
│ │ └── user_service_server.go
239+
│ └── svc/
240+
│ └── service_context.go
241+
└── pb/
242+
├── role/
243+
│ ├── role.pb.go
244+
│ └── role_grpc.pb.go
245+
├── user/
246+
│ ├── user.pb.go
247+
│ └── user_grpc.pb.go
248+
├── user.proto
249+
└── role.proto
203250
```

tools/goctl/rpc/cli/cli.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/spf13/cobra"
10+
1011
"github.com/zeromicro/go-zero/tools/goctl/rpc/generator"
1112
"github.com/zeromicro/go-zero/tools/goctl/util"
1213
"github.com/zeromicro/go-zero/tools/goctl/util/console"
@@ -38,6 +39,8 @@ var (
3839
VarStringStyle string
3940
// VarStringZRPCOut describes the zRPC output.
4041
VarStringZRPCOut string
42+
// VarStringRpcName describe the rpc name
43+
VarStringRpcName string
4144
// VarBoolIdea describes whether idea or not
4245
VarBoolIdea bool
4346
// VarBoolVerbose describes whether verbose.
@@ -91,6 +94,7 @@ func RPCNew(_ *cobra.Command, args []string) error {
9194
ctx.Output = filepath.Dir(src)
9295
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))
9396
ctx.IsGenClient = VarBoolClient
97+
ctx.RpcName = VarStringRpcName
9498

9599
grpcOptList := VarStringSliceGoGRPCOpt
96100
if len(grpcOptList) > 0 {

tools/goctl/rpc/cli/zrpc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/spf13/cobra"
10+
1011
"github.com/zeromicro/go-zero/tools/goctl/rpc/generator"
1112
"github.com/zeromicro/go-zero/tools/goctl/util"
1213
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
@@ -103,6 +104,7 @@ func ZRPC(_ *cobra.Command, args []string) error {
103104
ctx.Output = zrpcOut
104105
ctx.ProtocCmd = strings.Join(protocArgs, " ")
105106
ctx.IsGenClient = VarBoolClient
107+
ctx.RpcName = VarStringRpcName
106108
g := generator.NewGenerator(style, verbose)
107109
return g.Generate(&ctx)
108110
}

tools/goctl/rpc/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rpc
22

33
import (
44
"github.com/spf13/cobra"
5+
56
"github.com/zeromicro/go-zero/tools/goctl/config"
67
"github.com/zeromicro/go-zero/tools/goctl/internal/cobrax"
78
"github.com/zeromicro/go-zero/tools/goctl/rpc/cli"
@@ -57,6 +58,7 @@ func init() {
5758
protocCmdFlags.StringVar(&cli.VarStringHome, "home")
5859
protocCmdFlags.StringVar(&cli.VarStringRemote, "remote")
5960
protocCmdFlags.StringVar(&cli.VarStringBranch, "branch")
61+
protocCmdFlags.StringVar(&cli.VarStringRpcName, "rpc_name")
6062
protocCmdFlags.BoolVarP(&cli.VarBoolVerbose, "verbose", "v")
6163
protocCmdFlags.MarkHidden("go_out")
6264
protocCmdFlags.MarkHidden("go-grpc_out")

tools/goctl/rpc/generator/gen.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generator
22

33
import (
4+
"fmt"
45
"path/filepath"
56

67
"github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
@@ -26,6 +27,8 @@ type ZRpcContext struct {
2627
GrpcOutput string
2728
// Output is the output directory of the generated files.
2829
Output string
30+
// RpcName is the rpc service name
31+
RpcName string
2932
// Multiple is the flag to indicate whether the proto file is generated in multiple mode.
3033
Multiple bool
3134
// Whether to generate rpc client
@@ -41,6 +44,8 @@ func (g *Generator) Generate(zctx *ZRpcContext) error {
4144
return err
4245
}
4346

47+
fmt.Println("rpc name:", zctx.RpcName)
48+
4449
err = pathx.MkdirIfNotExist(abs)
4550
if err != nil {
4651
return err
@@ -67,7 +72,7 @@ func (g *Generator) Generate(zctx *ZRpcContext) error {
6772
return err
6873
}
6974

70-
err = g.GenEtc(dirCtx, proto, g.cfg)
75+
err = g.GenEtc(dirCtx, proto, g.cfg, zctx)
7176
if err != nil {
7277
return err
7378
}

tools/goctl/rpc/generator/genetc.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ var etcTemplate string
1919

2020
// GenEtc generates the yaml configuration file of the rpc service,
2121
// including host, port monitoring configuration items and etcd configuration
22-
func (g *Generator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
22+
func (g *Generator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config, c *ZRpcContext) error {
2323
dir := ctx.GetEtc()
24-
etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
24+
serviceName := c.RpcName
25+
if len(serviceName) == 0 {
26+
serviceName = ctx.GetServiceName().Source()
27+
}
28+
etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, serviceName)
2529
if err != nil {
2630
return err
2731
}
@@ -34,6 +38,6 @@ func (g *Generator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) err
3438
}
3539

3640
return util.With("etc").Parse(text).SaveTo(map[string]any{
37-
"serviceName": strings.ToLower(stringx.From(ctx.GetServiceName().Source()).ToCamel()),
41+
"serviceName": strings.ToLower(stringx.From(serviceName).ToCamel()),
3842
}, fileName, false)
3943
}

tools/goctl/rpc/generator/genmain.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ type MainServiceTemplateData struct {
2727
// GenMain generates the main file of the rpc service, which is an rpc service program call entry
2828
func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config,
2929
c *ZRpcContext) error {
30-
mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
30+
serviceName := c.RpcName
31+
if len(serviceName) == 0 {
32+
serviceName = ctx.GetServiceName().Source()
33+
}
34+
mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, serviceName)
3135
if err != nil {
3236
return err
3337
}
@@ -71,7 +75,7 @@ func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config
7175
return err
7276
}
7377

74-
etcFileName, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
78+
etcFileName, err := format.FileNamingFormat(cfg.NamingFormat, serviceName)
7579
if err != nil {
7680
return err
7781
}

0 commit comments

Comments
 (0)