Copy protobuf message

5,663 views
Skip to first unread message

Shyam S R

unread,
Apr 3, 2018, 8:26:48 PM4/3/18
to Protocol Buffers
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!

Bo Yang

unread,
Apr 3, 2018, 8:42:26 PM4/3/18
to Shyam S R, Protocol Buffers
The second copy looks correct to me.

--
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.
For more options, visit https://groups.google.com/d/optout.

Shyam S R

unread,
Apr 3, 2018, 8:52:20 PM4/3/18
to Protocol Buffers
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 ?

Bo Yang

unread,
Apr 4, 2018, 12:37:38 PM4/4/18
to Shyam S R, Protocol Buffers
I don't quite follow your question.
What's the source values and nested messages you are referring to ?

Shyam S R

unread,
Apr 4, 2018, 12:46:11 PM4/4/18
to Protocol Buffers
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

Bo Yang

unread,
Apr 4, 2018, 12:59:36 PM4/4/18
to Shyam S R, Protocol Buffers
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.
Message has been deleted

Shyam S R

unread,
Apr 4, 2018, 3:38:46 PM4/4/18
to Protocol Buffers
Thanks @Bo for the comments-

I fixed the message for clarify and for reference:

message A {
   enum foo {
      value1 = 0;
      value2 = 1;
   }
   int32 id = 1;
   string name = 2;
   foo type = 3;
   optional NestedMsg1 msg = 4;
}

message NestedMsg1 {
   int32 id = 1;
   string name = 2;
}


message B {
   enum foo {
      value0 = 0;
      value1 = 1;
      value2 =2;
   }
   int32 id = 1;
   string name = 2;
   foo type = 3;
   optional NestedMsg2 msg = 4;
}

message NestedMsg2 {
   int32 id = 1;
   string name = 2;
}

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

Bo Yang

unread,
Apr 4, 2018, 4:45:01 PM4/4/18
to Shyam S R, Protocol Buffers
Given that the enum in B is a superset of A. I don't think you need to adjust any enum value.

Shyam S R

unread,
Apr 4, 2018, 5:49:38 PM4/4/18
to Protocol Buffers
Thanks @Bo.
Reply all
Reply to author
Forward
0 new messages