Fill message with oneof data

28 views
Skip to first unread message

Aleh Linkin

unread,
Dec 9, 2022, 11:40:16 AM12/9/22
to Protocol Buffers
I have protobuf:
message Msg
{
oneof type {
   Msg1 msg1 = 1;
   Msg2 msg2 = 2;
   Msg3 msg3 = 3;
....
}
};

Is it possible to fill Msg with protobuf::Message from one of msgs?
Like:

msg1 m1;
//fill m1;
f(&m1);

msg2 m2;
//fill m2;
f(&m2);
...

void f(google::protobuf::Message *m)
{
Msg mainMessage;
// fill mainMessage with data from m correctly? <---
}

I tried CopyFrom but it doesn't work in this case.

Thank you.


Deanna Garcia

unread,
Dec 27, 2022, 4:06:03 PM12/27/22
to Protocol Buffers
What language are you working in?

I don't think any copying is necessary, you can just build Msg like you would any other message and set the value of type to m1 or m2.

Reply all
Reply to author
Forward
0 new messages