Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Protobuf Messages

Tags:

I have many different schemas, however there are a set of fields which every schema contains. I was wondering if there was a way to have a different schema extend a parent schema and inherit its fields. For example this is what I want:

message Parent {     required string common1 = 0;     optional string common2 = 1; }  message Child1 { // can we extend the Parent?     // I want common1, common2 to be fields here     required int c1 = 2;     required string c2 = 3; }  message Child2 { // can we extend Parent?     // I want common1, common2 to be fields here     repeated int c3 = 2;     repeated string c4 = 3; } 

Such that Child1 and Child2 also contain the fields common1 and common2 (and potentially more) from Parent.

Is this possible and if so how?

like image 859
user1413793 Avatar asked Mar 25 '15 18:03

user1413793


People also ask

Is protobuf extensible?

Objects in protobuf-net are not extensible by default, since they are just regular . NET classes; this means that any unexpected fields will be silently dropped during deserialization, and will be lost during serialization. It is, however, trivial to support extensions.

How big can a protobuf message be?

Protobuf has a hard limit of 2GB, because many implementations use 32-bit signed arithmetic. For security reasons, many implementations (especially the Google-provided ones) impose a size limit of 64MB by default, although you can increase this limit manually if you need to.

What is the proto extension?

Developer file created in Google's Protocol Buffer format, a data serialization format used for exchanging data; specifies one or more "messages" as logical records, each of which contains name-value pairs (e.g., a Person message may have an ID, name, and address); designed as an alternative to XML for data exchange.

How do I add comments to a proto file?

Adding Comments To add comments to your . proto files, use C/C++-style // and /* ... */ syntax.


1 Answers

This is not the exact answer to your question but we can do something like this to share common parameters.

message Child1 {      required int c1 = 2;     required string c2 = 3; }  message Child2 {      required int c1 = 2;     required string c2 = 3; }  message Request {     required string common1 = 0;     optional string common2 = 1;     oneof msg { Child1 c1 = 2; Child2 c2 = 3; }  } 

Other option is to use extend keyword

message Parent {     required string common1 = 0;     optional string common2 = 1; }  message Child1 {      extend Parent     {                optional Child1 c1 = 100;     }      required int c1 = 2;     required string c2 = 3; } 
like image 185
Raheel Avatar answered Sep 21 '22 06:09

Raheel