Tag Archives: protobuf
Protobuf: safer usage for buf
A drop-in replacement for buf issued by Ozon. Repository: https://github.com/easyp-tech/server Presentation (for subscribers only, unfortunately): https://conf.ontico.ru/online/hl2023/details/5206585
Protobuf: add header parameters
They finally added it to the grpc-gateway: https://github.com/grpc-ecosystem/grpc-gateway/pull/3010/files#diff-c255ac405628aada46c25a2c9765605e9f823bc523d3739fd3fa71d4bcbf5c99. So, to use it you can just update to the version 2.14.0 or above — https://github.com/grpc-ecosystem/grpc-gateway/releases/tag/v2.14.0, and add something like this to your protofile:
1 2 3 4 5 6 7 8 9 10 |
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = { summary: "Create a new foo."; parameters: { headers: { name: "My-Custom-Header"; description: "Some custom header description"; required: true; }; }; }; |
Golang. Adding a json body to POST request in protofile
It could be a bit tricky, and I failed to find a good example for it.
Useful links for proto generator development
https://rotemtam.com/2021/03/22/creating-a-protoc-plugin-to-gen-go-code/ https://medium.com/@tim.r.coulson/writing-a-protoc-plugin-with-google-golang-org-protobuf-cd5aa75f5777 https://github.com/nametake/protoc-gen-gohttp https://github.com/drekle/protoc-gen-goexample/blob/c60883c9711f3cfbd5191fd697651dbd067e6fdd/main.go#L135 https://github.com/grpc-ecosystem/grpc-gateway/blob/24434e22fb9734f1a62c81c4ea246125d7844645/examples/internal/proto/examplepb/echo_service_grpc.pb.go https://pkg.go.dev/google.golang.org/protobuf/compiler/protogen#Options.Run https://github.com/lyft/protoc-gen-star https://go.dev/blog/protobuf-apiv2 https://docs.buf.build/build/usage/#configuration https://github.com/grpc-ecosystem/grpc-gateway https://github.com/googleapis/googleapis/tree/master/google/api https://github.com/bufbuild/buf https://stackoverflow.com/questions/36540427/cannot-find-package-google-protobuf https://pkg.go.dev/google.golang.org/protobuf https://developers.google.com/protocol-buffers/docs/reference/go-generated#package https://stackoverflow.com/questions/61666805/correct-format-of-protoc-go-package https://grpc.io/docs/languages/go/quickstart/ https://github.com/protocolbuffers/protobuf/tree/master/src/google/protobuf https://github.com/gogo/protobuf/blob/b03c65ea87cdc3521ede29f62fe3ce239267c1bc/protobuf/google/protobuf/descriptor.proto
Nullable fields in json generated from protobuf file
Native support With grpc v2 for go, protofile’s optional option is supported, so you don’t need to import proto extensions anymore and use things like google.protobuf.StringValue. my.proto
1 2 3 |
message FeeView { optional string name = 1 [json_name = "name"]; } |
go mapper
1 2 3 4 5 6 7 8 |
var name *string if fee.Name != nil { name = &fee.Name } fee := &desc.FeeView{ Name: name, } |
Legacy way with extensions Null for nil value my.proto
1 2 3 4 5 |
import "google/protobuf/wrappers.proto"; message FeeView { google.protobuf.StringValue name = 1 [json_name = "name"]; } |
go mapper
1 2 3 4 5 6 7 8 9 10 11 12 |
import "github.com/gogo/protobuf/types" var name *types.StringValue if fee.Name != nil { name = &types.StringValue{ Value: *fee.Name, } } fee := &desc.FeeView{ Name: name, } |
Which will lead to this json response
1 |
{"translation_key":"a fee","name":null,"value":5,"style":"body","text_color":"grey","tag":null} |
Omit a field …
Complete list of swagger options to protobuf file
Here is an example with many options that help generate proper swagger out of protofile. Original URL — https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/examples/internal/proto/examplepb/a_bit_of_everything.proto.
Installing protobuf tools on MacOS
Installing protoc
1 |
brew install protobuf |
Or follow different instructions. Installing grpc_cli Option 1. Easy way.
1 2 |
brew tap grpc/grpc brew install --with-plugins grpc |
It is described here — https://github.com/grpc/homebrew-grpc. Option 2. Hard way — using cmake and make. NOT RECOMMENDED.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
brew install autoconf automake libtool shtool cmake git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc cd grpc git submodule update --init mkdir -p cmake/build cd cmake/build cmake ../.. make gRPC_INSTALL=ON make install # and continue accordgin to https://github.com/grpc/grpc/blob/master/BUILDING.md |
Or follow these instructions