Make small changes to the Java protocol buffer object
•
Java
I want to make a small change to go deep into the tree of Java protocol buffer objects
I can use it Getbuilder () method to create a new object, which is an old object with some changes
When doing so at a deeper level, the code becomes ugly:
Quux.Builder quuxBuilder = foo.getBar().getBaz().getQuux().toBuilder() Baz.Builder bazBuilder = foo.getBar().getBaz().toBuilder() Bar.Builder barBuilder = foo.getBar().toBuilder() Foo.Builder fooBuilder = foo.toBuilder() quuxBuilder.setNewThing(newThing); bazBuilder.setQuux(quuxBuilder); barBuilder.setBaz(bazBuilder); fooBuilder.setBar(barBuilder); Foo newFoo = fooBuilder.build();
(this is only level 4. I often deal with levels 5-8)
Is there a better way?
Solution
Another option is (I think it's been a while)
Foo.Builder fooBuilder = foo.toBuilder(); fooBuilder.getBarBuilder().getBazBuilder().getQuuxBuilder() .setNewThing(newThing); newFoo = fooBuilder.build();
Please note that this is not more efficient; You're still copying foo, bar, Baz and quux
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码