Thanks for your response Feng.

That would work perfectly for our needs.  We'll try it out and report back.

Marcos

On Wed, Feb 24, 2016 at 12:59 PM, Feng Xiao <[email protected]> wrote:

>
>
> On Mon, Feb 22, 2016 at 3:07 PM, Marcos Juarez <[email protected]> wrote:
>
>> Is there a way to have a "base" protobuf with a set of common fields,
>>  and then have projects/apps extend that with their own custom fields?
>> We're trying to minimize copy/pasting of the same fields all over the
>> place, for messages that will have a lot of common fields, that then need
>> specialization within different parts of the codebase.
>>
>> Say something like:
>>
>> message BaseProtobuf {
>>     required int id = 1;
>>     required int field1 = 2;
>>     required string label = 3;
>> }
>>
>> and then multiple files like:
>>
>> message CustomProtobuf1 extend BaseProtobuf {
>>     required int customId = 11;
>>     optional string customLabel = 12;
>> }
>>
>> message CustomProtobuf2 extend BaseProtobuf {
>>     required int anotherCustomId = 11;
>>     optional string customLabel = 12;
>> }
>>
>> Of course, at compile time, the user would have to specify the location
>> of the base protobuf definitions, if any, since protoc would need all of
>> them to generate the java source code.
>>
>> I do know about the import functionality, but that's more targeted for
>> type and enum definitions, not for inheriting base fields, so it doesn't
>> really address my question above.
>>
> Protobuf doesn't support inheritance. I suggest using composition instead.
> Such as:
>
> message CustomProtobuf1 {
>   optional BaseProtobuf base = 1;
>   required int customId = 11;
>   optional string customLabel = 12;
> }
>
>
>
>>
>> Thanks,
>>
>> Marcos Juarez
>>
>> --
>> 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.
>>
>
>

-- 
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