feat(goctl/rpc): support external proto imports with cross-package ty… (#5472)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
kesonan
2026-03-22 12:01:20 +08:00
committed by GitHub
parent c12c82b2f6
commit 004995f06a
93 changed files with 4871 additions and 270 deletions

View File

@@ -0,0 +1,66 @@
# 示例 10流式 RPC
本示例演示 gRPC 的三种流式通信模式:服务端流、客户端流和双向流。
## Proto 定义
`stream.proto` 定义了三个 RPC 方法,演示每种流式模式。
`go_package` 使用完整的模块路径:
```protobuf
option go_package = "example.com/demo/pb";
```
## 生成命令
首先,在输出目录中初始化 `go.mod`
```bash
mkdir -p output && cd output && go mod init example.com/demo && cd ..
```
然后生成代码:
```bash
goctl rpc protoc stream.proto \
--go_out=output \
--go-grpc_out=output \
--zrpc_out=output \
--go_opt=module=example.com/demo \
--go-grpc_opt=module=example.com/demo \
--module=example.com/demo \
-I .
```
生成的目录结构:
```
output/
├── etc
│ └── streamsvc.yaml
├── go.mod
├── internal
│ ├── config
│ │ └── config.go
│ ├── logic
│ │ ├── bidistreamlogic.go
│ │ ├── clientstreamlogic.go
│ │ └── serverstreamlogic.go
│ ├── server
│ │ └── streamserviceserver.go
│ └── svc
│ └── servicecontext.go
├── pb
│ ├── stream.pb.go
│ └── stream_grpc.pb.go
├── streamservice
│ └── streamservice.go
└── streamsvc.go
```
## 要点说明
- 支持三种流式模式:服务端流(响应带 `stream`)、客户端流(请求带 `stream`)和双向流(两端都带 `stream`)。
- goctl 为每个流式 RPC 方法生成独立的逻辑文件。
- 流式客户端代码不会自动生成,需直接使用 gRPC 客户端。

View File

@@ -0,0 +1,66 @@
# Example 10: Streaming RPC
This example demonstrates all three gRPC streaming patterns: server streaming, client streaming, and bidirectional streaming.
## Proto Definition
`stream.proto` defines three RPC methods demonstrating each streaming pattern.
The `go_package` uses a full module path:
```protobuf
option go_package = "example.com/demo/pb";
```
## Generation Commands
First, initialize the output directory with a `go.mod`:
```bash
mkdir -p output && cd output && go mod init example.com/demo && cd ..
```
Then generate the code:
```bash
goctl rpc protoc stream.proto \
--go_out=output \
--go-grpc_out=output \
--zrpc_out=output \
--go_opt=module=example.com/demo \
--go-grpc_opt=module=example.com/demo \
--module=example.com/demo \
-I .
```
Generated directory structure:
```
output/
├── etc
│ └── streamsvc.yaml
├── go.mod
├── internal
│ ├── config
│ │ └── config.go
│ ├── logic
│ │ ├── bidistreamlogic.go
│ │ ├── clientstreamlogic.go
│ │ └── serverstreamlogic.go
│ ├── server
│ │ └── streamserviceserver.go
│ └── svc
│ └── servicecontext.go
├── pb
│ ├── stream.pb.go
│ └── stream_grpc.pb.go
├── streamservice
│ └── streamservice.go
└── streamsvc.go
```
## Key Points
- Supports three streaming patterns: server streaming (`stream` on response), client streaming (`stream` on request), and bidirectional streaming (`stream` on both).
- goctl generates separate logic files for each streaming RPC method.
- Streaming client code is not auto-generated; use the gRPC client directly.

View File

@@ -0,0 +1,24 @@
syntax = "proto3";
package streamsvc;
option go_package = "example.com/demo/pb";
message StreamReq {
string input = 1;
}
message StreamReply {
string output = 1;
}
service StreamService {
// ServerStream: client sends one request, server returns a stream of responses.
rpc ServerStream(StreamReq) returns (stream StreamReply);
// ClientStream: client sends a stream of requests, server returns one response.
rpc ClientStream(stream StreamReq) returns (StreamReply);
// BidiStream: both client and server send streams of messages.
rpc BidiStream(stream StreamReq) returns (stream StreamReply);
}