Modify the payload of a dynamic generated message

57 views
Skip to first unread message

Mihai Muraru

unread,
Jul 9, 2019, 2:55:28 AM7/9/19
to Protocol Buffers
Hi,

I have a plugin which has to modify at runtime the value of  field from a dynamic protobuf message. First in the constructor of my class I create a copy of the original message
  auto desc_set=gproto::FileDescriptorSet();
  if (!desc_set.ParseFromString(channel_info.description))
  {
    throw ExporterException("\"" + channel_info.description + "\" is no valid protobuf descriptor.");
  }
  std::string error;

  auto proto_msg = _proto_decoder.GetProtoMessageFromDescriptorSet(desc_set, StripProtoTopicInfo(channel_info.type), error);
  if (proto_msg != nullptr)
  {
    _proto_msg.reset(proto_msg);
  }
  else
  {
    throw ExporterException(error);
  }
  //create a copy of original message because later we have to parse the original message with const parameter
  auto new_desc_set = gproto::FileDescriptorSet();
  new_desc_set.ParseFromString(channel_info.description);

  _modified_msg = _proto_decoder.GetProtoMessageFromDescriptorSet(new_desc_set, StripProtoTopicInfo(channel_info.type), error);


 , then I populate the original message fields in a using
void ExporterChannel::SetData(Timestamp timestamp, const MetaData& meta_data, const std::string& payload)
....
_proto_msg
->ParseFromString(payload)
....

 Then I parse the original message using a recursive function 
ParseProtobufMessage(_proto_msg.get(), _modified_msg, _proto_msg->GetTypeName());
by iterate in each field using   
auto refl = msg->GetReflection();
  std
::vector<const gproto::FieldDescriptor*> field_descs;
  refl
->ListFields(*msg, &field_descs);

 All is good until I get to repeated field.
refl->SetRepeatedFloat(new_msg, field_desc, index, refl->GetRepeatedFloat(*msg, field_desc, index)*signal.FactorValue.toFloat() + signal.OffsetValue.toFloat());

When I come back from the function and try to :
 _modified_msg->SerializeToString(&new_payload_);
  I get this Exception error:

Capture.JPG


in the header:

Capture.JPG

Can anyone tell me what i'm doing wrong?

Thanks in advance!

Adam Cozzette

unread,
Jul 9, 2019, 4:27:02 PM7/9/19
to Mihai Muraru, Protocol Buffers
My guess would be that there's a lifetime issue involving DynamicMessageFactory. The factory has to outlive any messages created from it via factory.GetPrototype(...)->New(). I would have to see more of the code to know for sure, though.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/4225aaeb-a1d1-4b76-906f-0249762dea7e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mihai Muraru

unread,
Jul 10, 2019, 1:43:36 AM7/10/19
to Protocol Buffers
Hi Cozzette,

I've attached all files. Maybe it will help you to help me . Thank you! Remember that the problems is only with Repeated fields.
To unsubscribe from this group and stop receiving emails from it, send an email to prot...@googlegroups.com.
exporter_channel.cpp
exporter_channel.h

Mihai Muraru

unread,
Jul 11, 2019, 9:12:20 AM7/11/19
to Protocol Buffers
Hi all.

Problem solved. I have used Reflection->GetMutableMessage(_modified_msg, field_descriptor, index) to get a not const message which then I've passed it to the recursive function 
ParseProtobufMessage(_proto_msg.get(), _modified_msg, _proto_msg->GetTypeName());

Thanks!
Reply all
Reply to author
Forward
0 new messages