ProtoBuf.jl icon indicating copy to clipboard operation
ProtoBuf.jl copied to clipboard

Extensions Support

Open sbchisholm opened this issue 10 years ago • 4 comments
trafficstars

Any thoughts on how to implement extensions?

sbchisholm avatar May 05 '15 19:05 sbchisholm

@sbchisholm, I have encountered extensions rarely and haven't really thought of implementing it yet. Any suggestions / ideas?

tanmaykm avatar May 07 '15 14:05 tanmaykm

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.

sbchisholm avatar May 11 '15 13:05 sbchisholm

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.

tanmaykm avatar May 21 '15 03:05 tanmaykm

Moving ahead with proto3 sounds like a good plan, Any does look like a much cleaner approach than the Extensions.

sbchisholm avatar May 21 '15 12:05 sbchisholm