Protobuf
# Distribute protobuf files
A general idea is to distribute .proto
files via a git repo and managing
releases from this central place.
What this does is:
- Put all release related tooling at single place.
- Version control
.proto
files. - Provide a review process on contracts.
References are:
- https://medium.com/namely-labs/how-we-build-grpc-services-at-namely-52a3ae9e7c35
- https://stackoverflow.com/a/62128550/6645446
# Useful tools
- Online Protobuf Decoder - https://protobuf-decoder.netlify.app/
# Tips and tricks
## Decode protobuf bytes locally
### With unknown type
|
|
Its output will look like this:
|
|
### With known type
Save message.proto
with the following content:
|
|
|
|
Its output will look like this:
|
|
## Encode protobuf bytes locally
With the same message.proto
mentioned above, we can decode a message like
this:
|
|
Note: base64
is used just to avoid binary junk being printed on terminal.
### Caveats
How will an enum value be decoded when encoded enum number is not present in schema that is used for decoding?
When encoding, we are passing a number for enum, but when decoding, we usually get a string for enum values. With example:
|
|
This will print the following output:
|
|
Valid numbers for enum are 0-3 as per our definition from message.proto
, but
what if we pass an unknown value like 10
?
Handling of this depends on implementation. protoc
prints it as number. For
example,
|
|
This will print the following output:
|
|
Spark's from_protobuf
method converts it to UNKNOWN_State_10
.