Skip to content

Conversation

@gszw90
Copy link

@gszw90 gszw90 commented Nov 21, 2025

support multi-proto to generate rpc servcie and allow proto to import other proto file
here is the directory:

root/
├── app/
│   └──pb/
│      ├── subpb/
│      ├── common.proto
│      ├── role.proto
│      └── user.proto 
└── third_party/
    └── google/

run:

goctl rpc protoc \
app/pb/role.proto \
app/pb/user.proto \
app/pb/common.proto \
app/pb/subpb/sub.proto \
--go_out=./app/pb \
--go_opt=module=github/xxx/project/app/pb \
--go-grpc_out=./app/pb \
--go-grpc_opt=module=github/xxx/project/app/pb \
--zrpc_out=./app \
--style go_zero \
-I ./app/pb \
-I ./third_party \
-m --name=myapp 

rusult:
image

commom.proto

syntax = "proto3";

package common;

option go_package = "github/xxx/project/app/pb/common";

message EmptyRequest {
}

message EmptyResponse {
}

role.proto

syntax = "proto3";

package role;
option go_package = "github/xxx/project/app/pb/role";

import "common.proto";
import "google/protobuf/any.proto";
import "subpb/sub.proto";

message GetRoleRequest {
  string id = 1;
}

message GetRoleResponse {
  string id = 1;
  string name = 2;
  string description = 3;
}


service role {
//  rpc GetRole(GetRoleRequest) returns (GetRoleResponse);
  rpc Empty(common.EmptyRequest) returns (subpb.sub.SubMessage);
  rpc GetRole(google.protobuf.Any) returns (GetRoleResponse);
  rpc GetStream(stream GetRoleRequest) returns (stream subpb.sub.SubResponse);
  rpc GetStream2(GetRoleRequest) returns (stream subpb.sub.SubResponse);
  rpc GetStream3(stream GetRoleRequest) returns (subpb.sub.SubResponse);
}

user.proto

syntax = "proto3";

package user;
option go_package = "github/xxx/project/app/pb/user";

message GetUserRequest {
  string id = 1;
}

message GetUserResponse {
  string id = 1;
  string name = 2;
}
service user {
  rpc GetUser(GetUserRequest) returns (GetUserResponse);
}

sub.proto

syntax = "proto3";

package subpb.sub;

option go_package = "github/xxx/project/app/pb/subpb/sub;sub";

message SubMessage {
  string name = 1;
}

message SubResponse {
  SubMessage message = 1;
}

gszw90 and others added 26 commits August 2, 2025 19:04
- 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
- 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
…at-add-rpc-name

# Conflicts:
#	tools/goctl/internal/flags/default_en.json
#	tools/goctl/rpc/README.md
#	tools/goctl/rpc/cli/cli.go
#	tools/goctl/rpc/cli/zrpc.go
#	tools/goctl/rpc/cmd.go
#	tools/goctl/rpc/generator/gen.go
#	tools/goctl/rpc/generator/genetc.go
#	tools/goctl/rpc/generator/genmain.go
- Change --rpc_name to --name in the goctl rpc command description
# Conflicts:
#	tools/goctl/rpc/cli/cli.go
#	tools/goctl/rpc/cli/zrpc.go
#	tools/goctl/rpc/cmd.go
@kesonan
Copy link
Collaborator

kesonan commented Nov 22, 2025

What problem does this PR need to solve? From the perspective of code changes, it is necessary to support the generation of multiple service protos, but these protos should point to different services. Why do the output directories point to app variables? I don’t think this is a good solution. On the surface, it is to convert scripting into goctl one-time generation, but the complexity of use becomes extremely high. I do not recommend increasing the complexity of use to reduce the code generation workload.

@gszw90
Copy link
Author

gszw90 commented Nov 22, 2025

This pr addresses two issues: first, it resolves the problem of file bloat caused by defining multiple services in a single proto file; second, it improves extensibility by referencing another proto file within a single proto file.
Additionally, for scenarios where RPC service code is generated from a single proto file, the assignment style of go_package in the proto file and the command used to generate the RPC service code remain unchanged.
For example run:

goctl rpc protoc ./app/pb/role.proto \
--go_out=./app/pb \
--go-grpc_out=./app/pb \
--zrpc_out=./app

role.proto

syntax = "proto3";

package role;

option go_package = "./role";

message GetRoleRequest {
  string role_id = 1;
}

message GetRoleResponse {
  string role_id = 1;
  string role_name = 2;
}

service RoleService {
  rpc GetRole(GetRoleRequest) returns (GetRoleResponse);
}

result:
image

@kesonan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants