mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-11 00:40:00 +08:00
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:
72
tools/goctl/rpc/example/04-transitive-import/README.md
Normal file
72
tools/goctl/rpc/example/04-transitive-import/README.md
Normal file
@@ -0,0 +1,72 @@
|
||||
# Example 04: Transitive Imports
|
||||
|
||||
This example demonstrates transitive proto imports, where A imports B and B imports C.
|
||||
|
||||
## Proto Definition
|
||||
|
||||
Three proto files form a transitive import chain, all sharing the same `go_package`:
|
||||
|
||||
```protobuf
|
||||
option go_package = "example.com/demo/pb";
|
||||
```
|
||||
|
||||
- `base.proto` — Layer C: defines base types (`BaseResp`).
|
||||
- `middleware.proto` — Layer B: imports `base.proto`, defines `RequestMeta`.
|
||||
- `main.proto` — Layer A: imports `middleware.proto`, defines the `PingService` (entry point).
|
||||
|
||||
Import chain: `main.proto` → `middleware.proto` → `base.proto`
|
||||
|
||||
## 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 main.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
|
||||
│ └── pingsvc.yaml
|
||||
├── go.mod
|
||||
├── internal
|
||||
│ ├── config
|
||||
│ │ └── config.go
|
||||
│ ├── logic
|
||||
│ │ └── pinglogic.go
|
||||
│ ├── server
|
||||
│ │ └── pingserviceserver.go
|
||||
│ └── svc
|
||||
│ └── servicecontext.go
|
||||
├── pb
|
||||
│ ├── base.pb.go
|
||||
│ ├── main.pb.go
|
||||
│ ├── main_grpc.pb.go
|
||||
│ └── middleware.pb.go
|
||||
├── pingservice
|
||||
│ └── pingservice.go
|
||||
└── pingsvc.go
|
||||
```
|
||||
|
||||
## Key Points
|
||||
|
||||
- Three proto files (`base.proto` → `middleware.proto` → `main.proto`) form a transitive import chain.
|
||||
- goctl recursively resolves all transitive imports automatically.
|
||||
- All three files share the same `go_package = "example.com/demo/pb"`.
|
||||
- You only need to specify the entry proto file — goctl and protoc handle the rest.
|
||||
- Circular imports are detected and will cause an error (same as protoc behavior).
|
||||
Reference in New Issue
Block a user