Many times I have a bunch of messages with some common fields, and I've 
wanted to pass them to methods that only use the common fields.  I mostly 
work in Java, so the simplest solution I can think of is adding an 
interface to the generated message that acts like a declaration of 
available getFoo methods.

It's related to this open 
issue: https://github.com/protocolbuffers/protobuf/issues/4481

It looks like there was an attempt to implement this in 2008, but the code 
never made it past the experimental stage.  Some of the old test code still 
exists. 
https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/test/proto/com/google/protobuf/test_extra_interfaces.proto
https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/compiler/java/java_helpers.cc#L309

I tried to use just the insertion points, but couldn't make that work 
smoothly.  The code in java_helpers only inserts a comment, and post 
processing the file was awkward.  I then tried with custom options.  Those 
work, but I was still modifying the java plugin.  In the end, I added two 
new fields to MessageOptions, one for adding to the immutable message 
interface and one for the builder interface.

Here's what I 
have: 
https://github.com/protocolbuffers/protobuf/compare/master...chasebradford:master

I think the end result is pretty clean.  A message definition can have a 
java_message_implements option, which names an interface to add to the 
MessageOrBuilder type.  Likewise, it can use java_builder_implements to add 
an interface to the Builder type.  I decided to not make the option 
repeatable, since interfaces can be combined into one interface in the 
project source.

The biggest drawback I've seen with this approach is the addition of a new 
option that older versions of protoc won't recognize and will reject.  The 
use of java_message_implements also adds the requirement that additional 
java classes be shared or stubbed in order to use the message definitions 
in an unrelated java project.

Are there better ways to accomplish my goals?  I'd rather not maintain a 
custom protoc binary if this can merge upstream.

Thanks,
Chase

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to