2022-07-19 08:55:18 +00:00
|
|
|
# Migrate to `buf` from `prototool`
|
|
|
|
|
|
|
|
Why? Because `prototool` is outdated, and can not run on M1 mac.
|
|
|
|
|
|
|
|
We need 3 files:
|
|
|
|
|
2023-08-05 18:56:25 +00:00
|
|
|
- `build.go`: need to install protoc-gen-\* binaries with pin version in
|
|
|
|
`go.mod`
|
2022-07-19 08:55:18 +00:00
|
|
|
- `buf.yaml`
|
|
|
|
- `buf.gen.yaml`
|
|
|
|
|
2023-06-11 11:02:42 +00:00
|
|
|
FYI, I use:
|
2022-07-19 08:55:18 +00:00
|
|
|
|
|
|
|
- [grpc-ecosystem/grpc-gateway v1.16.0](https://github.com/grpc-ecosystem/grpc-gateway/releases/tag/v1.16.0)
|
2023-03-20 21:00:21 +00:00
|
|
|
- [bufbuild/protoc-gen-validate](github.com/bufbuild/protoc-gen-validate)
|
2022-10-09 11:03:05 +00:00
|
|
|
- [kei2100/protoc-gen-marshal-zap](github.com/kei2100/protoc-gen-marshal-zap)
|
2022-07-19 08:55:18 +00:00
|
|
|
|
|
|
|
`build.go`:
|
|
|
|
|
|
|
|
```go
|
|
|
|
//go:build tools
|
|
|
|
// +build tools
|
|
|
|
|
|
|
|
import (
|
2022-10-09 11:03:05 +00:00
|
|
|
_ "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway"
|
|
|
|
_ "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger"
|
|
|
|
_ "github.com/kei2100/protoc-gen-marshal-zap/plugin/protoc-gen-marshal-zap"
|
2022-07-19 08:55:18 +00:00
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
`buf.yaml`
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
version: v1
|
|
|
|
deps:
|
2023-03-20 21:00:21 +00:00
|
|
|
- buf.build/envoyproxy/protoc-gen-validate:6607b10f00ed4a3d98f906807131c44a
|
|
|
|
- buf.build/kei2100/protoc-gen-marshal-zap:081f499bbca4486784773e060c1c1418
|
2022-07-19 08:55:18 +00:00
|
|
|
- buf.build/haunt98/googleapis:b38d93f7ade94a698adff9576474ae7c
|
|
|
|
- buf.build/haunt98/grpc-gateway:ecf4f0f58aa8496f8a76ed303c6e06c7
|
|
|
|
breaking:
|
|
|
|
use:
|
2023-06-10 04:13:37 +00:00
|
|
|
- PACKAGE
|
2022-07-19 08:55:18 +00:00
|
|
|
lint:
|
|
|
|
use:
|
|
|
|
- DEFAULT
|
|
|
|
```
|
|
|
|
|
|
|
|
`buf.gen.yaml`:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
version: v1
|
|
|
|
plugins:
|
2023-06-11 11:02:42 +00:00
|
|
|
- plugin: buf.build/grpc/go:v1.3.0
|
2022-07-19 08:55:18 +00:00
|
|
|
out: pkg
|
2023-06-30 08:46:42 +00:00
|
|
|
- plugin: buf.build/protocolbuffers/go:v1.31.0
|
2023-06-10 04:11:44 +00:00
|
|
|
out: pkg
|
2023-06-30 08:46:42 +00:00
|
|
|
- plugin: buf.build/bufbuild/validate-go:v1.0.2
|
2023-03-20 21:00:21 +00:00
|
|
|
out: pkg
|
|
|
|
opt:
|
|
|
|
- lang=go
|
2023-06-10 04:11:44 +00:00
|
|
|
- plugin: marshal-zap
|
2023-03-20 21:00:21 +00:00
|
|
|
out: pkg
|
2023-06-10 04:11:44 +00:00
|
|
|
- plugin: grpc-gateway
|
2022-07-19 08:55:18 +00:00
|
|
|
out: pkg
|
|
|
|
opt:
|
|
|
|
- logtostderr=true
|
2023-06-10 04:11:44 +00:00
|
|
|
- plugin: swagger
|
2022-07-19 08:55:18 +00:00
|
|
|
out: .
|
|
|
|
opt:
|
|
|
|
- logtostderr=true
|
|
|
|
```
|
|
|
|
|
|
|
|
Update `Makefile`:
|
|
|
|
|
|
|
|
```Makefile
|
|
|
|
gen:
|
2023-03-20 21:00:21 +00:00
|
|
|
go install github.com/kei2100/protoc-gen-marshal-zap/plugin/protoc-gen-marshal-zap
|
2022-10-09 11:03:05 +00:00
|
|
|
go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
|
|
|
|
go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
|
|
|
|
go install github.com/bufbuild/buf/cmd/buf@latest
|
|
|
|
buf mod update
|
|
|
|
buf format -w
|
|
|
|
buf generate
|
2022-07-19 08:55:18 +00:00
|
|
|
```
|
|
|
|
|
2022-10-09 11:03:05 +00:00
|
|
|
Run `make gen` to have fun of course.
|
2022-07-19 08:55:18 +00:00
|
|
|
|
2023-08-05 18:56:25 +00:00
|
|
|
If using `bufbuild/protoc-gen-validate`, `kei2100/protoc-gen-marshal-zap`,
|
|
|
|
better make a raw copy of proto file for other services to integrate:
|
2023-03-20 21:00:21 +00:00
|
|
|
|
|
|
|
```Makefile
|
|
|
|
raw:
|
2023-06-10 04:11:44 +00:00
|
|
|
mkdir -p ./raw
|
2023-03-20 21:08:31 +00:00
|
|
|
cp ./api.proto ./raw/
|
|
|
|
sed -i "" -e "s/import \"validate\/validate\.proto\";//g" ./raw/api.proto
|
|
|
|
sed -i "" -e "s/\[(validate\.rules)\.string.min_len = 1\]//g" ./raw/api.proto
|
|
|
|
sed -i "" -e "s/import \"marshal-zap\.proto\";//g" ./raw/api.proto
|
|
|
|
sed -i "" -e "s/\[(marshal_zap\.mask) = true]//g" ./raw/api.proto
|
2023-03-20 21:00:21 +00:00
|
|
|
```
|
|
|
|
|
2022-07-19 08:55:18 +00:00
|
|
|
## FAQ
|
|
|
|
|
2023-08-05 18:56:25 +00:00
|
|
|
Remember `bufbuild/protoc-gen-validate`, `kei2100/protoc-gen-marshal-zap`,
|
|
|
|
`grpc-ecosystem/grpc-gateway` is optional, so feel free to delete if you don't
|
|
|
|
use theme.
|
2022-10-09 11:03:05 +00:00
|
|
|
|
2022-07-19 08:55:18 +00:00
|
|
|
If use `vendor`:
|
|
|
|
|
|
|
|
- Replace `buf generate` with `buf generate --exclude-path vendor`.
|
|
|
|
- Replace `buf format -w` with `buf format -w --exclude-path vendor`.
|
|
|
|
|
2022-07-30 17:16:44 +00:00
|
|
|
If you use grpc-gateway:
|
2022-07-19 09:06:55 +00:00
|
|
|
|
2023-08-05 18:56:25 +00:00
|
|
|
- Replace `import "third_party/googleapis/google/api/annotations.proto";` with
|
|
|
|
`import "google/api/annotations.proto";`
|
|
|
|
- Delete `security_definitions`, `security`, in
|
|
|
|
`option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger)`.
|
2022-07-19 09:06:55 +00:00
|
|
|
|
2022-07-19 08:55:18 +00:00
|
|
|
The last step is delete `prototool.yaml`.
|
|
|
|
|
2022-10-09 11:03:05 +00:00
|
|
|
If you are not migrate but start from scratch:
|
2022-07-30 17:16:44 +00:00
|
|
|
|
|
|
|
- Add `buf lint` to make sure your proto is good.
|
2023-08-05 18:56:25 +00:00
|
|
|
- Add `buf breaking --against "https://your-grpc-repo-goes-here.git"` to make
|
|
|
|
sure each time you update proto, you don't break backward compatibility.
|
2022-07-30 17:16:44 +00:00
|
|
|
|
2023-06-11 11:02:42 +00:00
|
|
|
# Tips
|
|
|
|
|
|
|
|
Some experience I got after writing proto files for a living:
|
|
|
|
|
2023-08-05 18:56:25 +00:00
|
|
|
- Ignore DRY (Do not Repeat Yourself) when handling proto, don't split proto
|
|
|
|
into many files. Trust me, it saves you from wasting time to debug how to
|
|
|
|
import Go after generated. Because proto import and Go import is
|
|
|
|
[2](https://github.com/golang/protobuf/issues/895) different things. If
|
|
|
|
someone already have split proto files, you should use `sed` to fix the damn
|
|
|
|
things.
|
2023-06-11 11:02:42 +00:00
|
|
|
|
2022-07-19 08:55:18 +00:00
|
|
|
## Thanks
|
|
|
|
|
|
|
|
- [uber/prototool](https://github.com/uber/prototool)
|
|
|
|
- [bufbuild/buf](https://github.com/bufbuild/buf)
|