0

I have a proto which has a message that contains extensions

message MsgA
{
    extensions 10 to 50;
}

I have the other proto which has the extensions (showing only 1 of the extensions below)

extend MsgA
{
    optional MsgB msgB = 10;
}
message MsgB
{
    required int32 value = 1;
}

Through the protobuf code, I am able to get the Descriptor and FileDescriptor for MsgB and from there I am able to retrieve the extension "msgB" which is of type FieldDescriptor. I am finding the extensions through protobuf code vs using the generated code from the MsgB proto as I am trying to iterate through all of the extension messages vs hardcoding each of them into a MutableExtension. (I understand the hardcoded way of MutableExtension(MsgB::msgB) but want to use the retrieved extension)

I want to populate a mutable extension of MsgA with the msgB extension but MutableExtension takes in a type of

(const ::google::protobuf::internal::ExtensionIdentifier& id) or

(const ::google::protobuf::internal::ExtensionIdentifier& id, int index)

How can I populate/create MutableExtension with the found msgB extension or how do I create an ExtensionIdentifer of msgB to use as the input to create a MutableExtension of MsgA ?

rr0711
  • 17
  • 8

1 Answers1

1

I think I found a way to get the extention into MsgA through reflection.

On the MsgA object, retrieve the reflection object. Then on the reflection object call MutableMessage with the MsgA object and the extensions FieldDescriptor object (The FileDescriptor objects comes from the GetDescriptor()->file() that the extension is defined in which in this case is MsgB).

MsgA* msgObj;
FieldDescriptor* ext = fileDescriptor->extension(indexOfExtension);
auto reflection = msgObj->GetReflection();
reflection->MutableMessage(msgObj, ext);

This will then populate the MsgA object with the extension message and you can iterate through each of the extensions by index to populate the MsgA object with each extension message.

rr0711
  • 17
  • 8