[C++]How to unpack a field of Any with reflction?

25 views
Skip to first unread message

qing zhou

unread,
Jan 21, 2022, 5:24:36 AM1/21/22
to Protocol Buffers
The proto file defined like this:
message HqMessage {
        uint32 busi_no          = 1;
        google.protobuf.Any detail      = 2;
}

message B0T4F1 {
        uint32 MarketType               = 501;
}
//---------------------------------------------------------------------
STEP1, use protobuf's reflection, I had set the message of BOT4F1 to the 'detail':
      .......
          const google::protobuf::Descriptor* _detail_des = _des_pool->FindMessageTypeByName("B0T4F1");
        const google::protobuf::Message* _detail_prototype_message = m_dynamic_factory.GetPrototype(_detail_des);
        google::protobuf::Message* _detail_msg = _detail_prototype_message->New();
        const google::protobuf::Reflection* _detail_msg_ref = _detail_prototype_message->GetReflection();
        const google::protobuf::FieldDescriptor* _detail_fd = _detail_des->FindFieldByNumber(501);
        uint32 mkt_type =100;
        _fd = _des->FindFieldByNumber(2);
          _msg_ref ->SetAllocatedMessage(_msg, _detail_msg, _fd);
.......
STEP2,serialize the message to a buffer, then parse the HqMessage from the buffer.
STEP3, firest, I get the message of Any, then serialize the any message to buffer again. Finally,get the message object of B0T4F1 through the new buffer.

How can i parse the buffer to the message of B0T4F1 without serialize ’detail' message again. Could I get the ‘B0T4F1' message from 'HqMessage ' directly? How can i do this?

Deanna Garcia

unread,
Jan 24, 2022, 6:35:37 PM1/24/22
to Protocol Buffers
Can you use an Any method, like unpack or getParserForType?
Reply all
Reply to author
Forward
0 new messages