Protobuf 3 optional values in Java bindings

37 views
Skip to first unread message

marko

unread,
Oct 13, 2020, 8:05:49 AM10/13/20
to Protocol Buffers
I'm generating Java bindings from Protobuf 3 schema definitions that include optional values represented with protobuf wrapper types. 

Decoding the fields as Java Optional<T> would work nicely if the generated accessors would be able to return null when the optional value is missing:

Optional.ofNullable(myObject.getMyField())

Unfortunately, the generated accessor returns the value or a default value, if missing.
So, one needs to resort to something like this:

myObject.hasMyField() ? Optional.of(myObject.getMyField()) : Optional.empty()

What's the best practice for handling this with Protobuf?

Is there a way to get protoc to generate code that would allow getting the actual underlying real value directly?

Reply all
Reply to author
Forward
0 new messages