Thanks @Bo for the comments-
I fixed the message for clarify and for reference:
message A {
enum foo {
value1 = 0;
value2 = 1;
}
int32 id;
string name;
foo type;
optional NestedMsg1 msg;
}
message NestedMsg1 {
int32 id;
string name;
}
message B {
enum foo {
value0 = 0;
value1 = 1;
value2 =2;
}
int32 id;
string name;
foo type;
optional NestedMsg2 msg;
}
message NestedMsg2 {
int32 id;
string name;
}
Given such a schema, i understand that it should be feasible to copy from
type A to B through:
void copy(A &in, B &out) {
out.ParseFromString(in.SerializeToString());
return;
}
The enum value in out may have to be adjusted though i believe-
Thanks,
Shyam
On Wednesday, April 4, 2018 at 9:59:36 AM UTC-7, Bo Yang wrote:
>
> 1) You misunderstood the enum definition in proto. field1, field2 and
> field3 don't define fields on message A and B. Instead, they define the
> enum value of enum foo.
> 2) If the nested message field have the same structure and same field
> number, it will still work.
> BTW, I just find your proto definition is not valid. Please take a look of
> the example, see how a valid proto looks like.
>
> On Wed, Apr 4, 2018 at 9:46 AM Shyam S R <[email protected] <javascript:>>
> wrote:
>
>> By source values i meant the values of enums in "in" message: void copy(A
>> &in, B &out). The enum values in A and B differ by one element.
>>
>> By nested messages,
>> message A {
>> enum foo {
>> field1 = 0;
>> field2 = 1;
>> }
>> int32 id;
>> string name;
>> foo type;
>> NestedMsg1 msg1;
>> }
>>
>> message NestedMsg1 {
>> int32 id;
>> string name;
>> }
>>
>> If message B were to also have a similar structure, will
>> out.ParseFromString(in.SerializeToString()) take care of nested messages as
>> well ?
>>
>> I also explore CopyFrom(Message&) but not sure if it will work-
>>
>> Thanks,
>> Shyam
>>
>> On Wednesday, April 4, 2018 at 9:37:38 AM UTC-7, Bo Yang wrote:
>>
>>> I don't quite follow your question.
>>> What's the source values and nested messages you are referring to ?
>>>
>>> On Tue, Apr 3, 2018 at 5:52 PM Shyam S R <[email protected]> wrote:
>>>
>>>> thanks for the reply @Bo. Follow up: What happens to the enums, the
>>>> source values are maintained i suppose ?
>>>>
>>>> Does it work with nested messages also, in the sense, does
>>>> ParseFromString look for any message type information within the stream or
>>>> as long as the scalar types are good it parses ok ?
>>>>
>>>> On Tuesday, April 3, 2018 at 5:42:26 PM UTC-7, Bo Yang wrote:
>>>>
>>>>> The second copy looks correct to me.
>>>>>
>>>>> On Tue, Apr 3, 2018 at 5:26 PM Shyam S R <[email protected]> wrote:
>>>>>
>>>> Hi all,
>>>>>>
>>>>>> Is there a favored way to copy a protobuf message from one type to
>>>>>> another ? A little background:
>>>>>>
>>>>>> I have two messages for example:
>>>>>> message A {
>>>>>> enum foo {
>>>>>> field1 = 0;
>>>>>> field2 = 1;
>>>>>> }
>>>>>> int32 id;
>>>>>> string name;
>>>>>> foo type;
>>>>>> }
>>>>>>
>>>>>> message B {
>>>>>> enum foo {
>>>>>> field1 = 0;
>>>>>> field2 = 1;
>>>>>> field3 =2;
>>>>>> }
>>>>>> int32 id;
>>>>>> string name;
>>>>>> foo type;
>>>>>> }
>>>>>>
>>>>>> I have references to these messages and want to implement this
>>>>>> method:
>>>>>> void copy(A &in, B &out) {
>>>>>> // in's contents need to be copied to out
>>>>>> }
>>>>>>
>>>>>> the only difference between A and B is the enum field and they are
>>>>>> named different. There could be more messages within the message types A
>>>>>> and B with the same fields, but only enum's having an extra field in B.
>>>>>> How
>>>>>> should i go about this ?
>>>>>>
>>>>>> Do the following options look feasible :
>>>>>> void copy(A &in, B &out) {
>>>>>> // in's contents need to be copied to out
>>>>>> cast in to type B
>>>>>> out = in;
>>>>>> }
>>>>>>
>>>>>> void copy(A &in, B &out) {
>>>>>> // in's contents need to be copied to out
>>>>>> Use in.SerializeAsString() and pipe it to out.ParseFromString()
>>>>>> }
>>>>>>
>>>>>> Appreciate the community's help!
>>>>>>
>>>>>> Thanks!
>>>>>>
>>>>>> --
>>>>>> 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 [email protected].
>>>>>> To post to this group, send email to [email protected].
>>>>>
>>>>>
>>>>>> Visit this group at https://groups.google.com/group/protobuf.
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>> --
>>>> 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 [email protected].
>>>> To post to this group, send email to [email protected].
>>>> Visit this group at https://groups.google.com/group/protobuf.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>> --
>> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/protobuf.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.