creating a message from another but changing only one field

4,596 views
Skip to first unread message

marco gaddoni

unread,
Oct 10, 2011, 11:44:10 AM10/10/11
to prot...@googlegroups.com

Hello,

i am using protocol buffers to comunicate between two programs,
one in c# and the other in c++.

it is working well.

i have a generic question with the c# (and i think the java) api:
how do you create a different message from an existing one
but changin only one field?

say that i have

message foo {
 ..message baz {
....int32 f1 = 1;
..}

..required baz c1;
..required int32 f2;
}

and i have a message in java Foo msg;
how do i create a new message identical to
msg but with f1 different ?

in my code i have to write a lot of code
and that code is fragile because i have to
.Merge mostly field by field everything and if i
change the structure of the message adding one
field i have to change my code.

there is another way to do it ?

...



Ron Reynolds

unread,
Oct 10, 2011, 1:05:45 PM10/10/11
to prot...@googlegroups.com
i believe this is where Foo.newBuilder(Foo oldFoo) comes into play.  PB messages once built are immutable so instead you create a builder around an immutable Foo, change what you want through the builder's methods, then build a new (immutable) message using Builder.build().
there's also the method foo.toBuilder() which will do the same thing.


From: marco gaddoni <marco....@gmail.com>
To: prot...@googlegroups.com
Sent: Monday, October 10, 2011 8:44 AM
Subject: [protobuf] creating a message from another but changing only one field

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/DPAr9H49nL8J.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


marco gaddoni

unread,
Oct 11, 2011, 10:53:51 AM10/11/11
to prot...@googlegroups.com, Ron Reynolds
thank you

You suggest that this is the best way?

..Foo oldfoo = get_from_elsewhere();

..Baz newbaz = oldfoo.c1.toBuilder().set_f1(42).Build();
..Foo newfoo = oldfoo.toBuilder().set_c1(newbaz).Build();

..send_somewhere(newfoo);

i see this problematic if i have messages nested in messages



Il giorno lunedì 10 ottobre 2011 19:05:45 UTC+2, Ron ha scritto:
i believe this is where Foo.newBuilder(Foo oldFoo) comes into play.  PB messages once built are immutable so instead you create a builder around an immutable Foo, change what you want through the builder's methods, then build a new (immutable) message using Builder.build().
there's also the method foo.toBuilder() which will do the same thing.


From: marco gaddoni <marco...@gmail.com>

Ron Reynolds

unread,
Oct 11, 2011, 11:01:35 AM10/11/11
to prot...@googlegroups.com
yes, it can be.  also i seem to remember when modifying a list of elements it's easier to clear the target list, iterate the source list and selectively copy over to the target list rather than try to modify the target list after it has been copied.

Cc: Ron Reynolds <tequi...@ymail.com>
Sent: Tuesday, October 11, 2011 7:53 AM
Subject: Re: [protobuf] creating a message from another but changing only one field
Reply all
Reply to author
Forward
0 new messages