what’s the right way to do polymorphism with protocol buffers?

In proto3 the extend keyword has been replaced. From the docs: If you are already familiar with proto2 syntax, the Any type replaces extensions. syntax = “proto3”; import “google/protobuf/any.proto”; message Foo { google.protobuf.Any bar = 1; } But beware: Any is essentially a bytes blob. Most of the times it is better to use Oneof: … Read more

How do I represent a UUID in a protobuf message?

You should probably use string or bytes to represent a UUID. Use string if it is most convenient to keep the UUID in human-readable format (e.g. “de305d54-75b4-431b-adb2-eb6b9e546014”) or use bytes if you are storing the 128-bit value raw. (If you aren’t sure, you probably want string.) Wrapping the value in a message type called UUID … Read more

Why are there no custom default values in proto3?

My understanding is that proto3 no longer allows you to detect field presence and no longer supports non-zero default values because this makes it easier to implement protobufs in terms of “plain old structs” in various languages, without the need to generate accessor methods. This is perceived as making Protobuf easier to use in those … Read more

Date and time type for use with Protobuf

There is Timestamp message type since protobuf 3.0, that’s how to create it in model: syntax = “proto3”; import “google/protobuf/timestamp.proto”; message MyMessage { google.protobuf.Timestamp my_field = 1; } timestamp.proto file contains examples of Timestamp using, including related to Linux and Windows programs. Example 1: Compute Timestamp from POSIX time(). Timestamp timestamp; timestamp.set_seconds(time(NULL)); timestamp.set_nanos(0); Example 2: … Read more