You need to do it like this:
message TypeC { optional TypeA a = 1; optional TypeB b = 2; }
If there are many options, you can also add a tag field so that you do not need to check has_*() for each of them.
This is described in the Protobuf docs: https://developers.google.com/protocol-buffers/docs/techniques#union
PS. This missing Protobufs feature has been fixed in Cap'n Proto , a new serialization system of the same author (me): Cap'n Proto implements "union" for this purpose. I also implemented unions at Protobufs before leaving Google, but before I left, I was not able to merge my changes into the backbone. Sorry .: (
EDIT: Looks like the Protobuf team eventually merged my change and released version 2.6.0 with it. :) See the oneof ad .
Kenton Varda
source share