ProtoBuf.jl
ProtoBuf.jl copied to clipboard
Extensions Support
Any thoughts on how to implement extensions?
@sbchisholm, I have encountered extensions rarely and haven't really thought of implementing it yet. Any suggestions / ideas?
For the use case when serializing objects to protobuf, extensions provide a way to mirror the objects in the protobuf message and support inheritance. Here's an example of how it can be accomplished in python:
import message_pb2
class A(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def to_protobuf(self):
proto = message_pb2.A()
proto.x = self.x
proto.y = self.y
proto.z = self.z
return proto
class B(A):
def __init__(self, x, y, z, b):
super(B, self).__init__(x, y, z)
self.b = b
def to_protobuf(self):
proto = super(B, self).to_protobuf()
ext = proto.Extensions[message_pb2.b_ext]
ext.b = b
return proto
message A{
required string x = 1;
required string y = 2;
required string z = 3;
extensions 100 to max;
}
message B {
required string b = 1;
}
extend A {
optional B b_ext = 101;
}
Extensions in python seem to be supported by the ProtoBuf object using a dictionary indexed by the extension number. I think something similar might work for Julia. We would probably want to have accessors for get_extenstion, set_extension, and has_extension.
proto3 deprecates Extensions and introduces an Any type which seems simpler. May be it's better to jump to proto3 instead? Unless there is an immediate requirement.
Moving ahead with proto3 sounds like a good plan, Any does look like a much cleaner approach than the Extensions.