Message.Builder and sub-builders

61 views
Skip to first unread message

Alex

unread,
Dec 1, 2011, 12:00:06 AM12/1/11
to prot...@googlegroups.com

Let's assume we have the following structure:

message Alpha {

  message Beta {
    
    optional int32 mercury = 1;
    
    optional int32 venus = 2;
  }

  optional int32 earth = 1;

  optional Beta mars = 2;
}

The Java generated code allows to get a sub-builder for "mars" field from a builder for an Alpha message by calling Alpha.newBuilder().getMarsBuilder(). This is nice because I can keep builders for structured messages and update them as needed and build the Alpha message as needed, but still hold on to the big builder which I plan to update further.

But I do not think there is a way to do this in a generic way, using Message.Builder interface. Is there any plan for something like this?

Jason Hsueh

unread,
Dec 6, 2011, 2:53:37 AM12/6/11
to prot...@googlegroups.com
Do you mean getting the sub-builders by passing the Descriptor.FieldDescriptor for the field you want, like what you would do for Message.Builder.setField()? This sounds like a reasonable feature request, though to be honest I'm not familiar enough with the Java code to know how difficult this would be.

--
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/-/Y2ff3aqv72wJ.
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.

Alex

unread,
Dec 6, 2011, 3:21:50 AM12/6/11
to prot...@googlegroups.com
Yes. For example by calling messageBuilder.getBuilder(marsFieldDescriptor) I would get a builder for the mars field which stays attached to the messageBuilder.

At the moment one can call Message.Builder.newBuilder(Descriptors.FieldDescriptor)  in a generic way to create a new sub-builder, but this new builder is detached from the parent builder. So one cannot have a compare complex structure of  builders to handle in a generic way. But the same thing works when handing messages in non-generic way.
Reply all
Reply to author
Forward
0 new messages