Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance in protocol buffers

How to handle inheritance in Google Protocol Buffers 3.0?

Java equivalent code:

public class Bar {
    String name;
}
public class Foo extends Bar {
    String id;
}

What would be Proto equivalent code?

message Bar {
    string name = 1;
}
message Foo {
    string id = 2;
}
like image 202
Vivek Sinha Avatar asked Dec 20 '16 13:12

Vivek Sinha


People also ask

Does Protobuf have inheritance?

Inheritance is not supported in protocol buffers.

What is protocol buffer in C#?

Protobuf is used to serialize data structure efficiently. It is faster, smaller & simpler than XML. Protobuf is useful in developing programs to communicate with each other over a wire and it is created by Google. It helps us in creating gRPC services.

What is oneof in Protobuf?

Protocol Buffer (Protobuf) provides two simpler options for dealing with values that might be of more than one type. The Any type can represent any known Protobuf message type. And you can use the oneof keyword to specify that only one of a range of fields can be set in any message.

What are protocol buffers?

Protocol buffers are a combination of the definition language (created in . proto files), the code that the proto compiler generates to interface with data, language-specific runtime libraries, and the serialization format for data that is written to a file (or sent across a network connection).


2 Answers

Protocol Buffers does not support inheritance. Instead, consider using composition:

message Foo {
  Bar bar = 1;
  string id = 2;
}

However, that said, there is a trick you can use which is like inheritance -- but which is an ugly hack, so you should only use it with care. If you define your message types like:

message Bar {
  string name = 1;
}
message Foo {
  string name = 1;
  string id = 2;
}

These two types are compatible, because Foo contains a superset of the fields of Bar. This means if you have an encoded message of one type, you can decode it as the other type. If you try to decode a Bar as type Foo, the field id will not be set (and will get its default value). If you decode a Foo as type Bar, the field id will be ignored. (Notice that these are the same rules that apply when adding new fields to a type over time.)

You can possibly use this to implement something like inheritance, by having several types all of which contain a copy of the fields of the "superclass". However, there are a couple big problems with this approach:

  • To convert a message object of type Foo to type Bar, you have to serialize and re-parse; you can't just cast. This can be inefficient.
  • It's very hard to add new fields to the superclass, because you have to make sure to add the field to every subclass and have to make sure that this doesn't create any field number conflicts.
like image 142
Kenton Varda Avatar answered Oct 16 '22 10:10

Kenton Varda


See the Protocol Buffer Basics tutorial:

Don't go looking for facilities similar to class inheritance, though – protocol buffers don't do that.

like image 30
Andy Turner Avatar answered Oct 16 '22 11:10

Andy Turner