Good point.. guess the code generator could handle unionizing a
standard message encoding.. if multiple fields are encoded we could
go with a last one wins approach. And to make the proto files also
backward compatible, that could just be a message level option like:
message Foo {
option union true;
int32 a = 1;
Baz b = 2;
string c = 3;
}
BTW I do think this is a very common issue that comes up. Any time
you are working message formats your going to have to frame them in
something that will let you know the type your working with. Sure the
app could do the framing, but I think it would be much nicer if we did
it for them.
On Sep 3, 2:48 pm, "Kenton Varda" <
ken...@google.com> wrote:
> If we were to support a union encoding, I think it should look like this:
> message Foo {
> optional union bar {
> int32 a = 1;
> Baz b = 2;
> string c = 3;
> }
>
> }
>
> In other words, all it says is that "only one of these fields will be set",
> thus allowing code generators to share memory between them. The fields a,
> b, and c would have the exact same methods that optional fields normally
> have, like has_a(), a(), etc. Additionally, there would be a method like
> "bar()" which returns an enum identifying which of the fields is currently
> set.
>
> The advantages are:
> * No need to modify the wire format.
> * Implementations can get by ignoring unions (just treat them as separate
> fields) until they care to implement them.
>
> All that said, I'm not convinced this is worth implementing. It would add a
> bunch of complication, and we've gotten by fine without this for many years.
>