As it's a repeated field, you should use "getRepeatedFieldBuilder".

On Mon Jan 26 2015 at 5:38:23 PM Jon Emerson <panac...@gmail.com> wrote:

> Hi There,
>
> I'm writing a utility to convert data stored in MongoDB back to protocol
> buffer format.  My strategy is to look for each field in the protocol
> buffer definition, and if there's a matching field from my MongoDB object,
> then pull that data and set it on my protocol buffer builder.  This works
> great, except for embedded messages.  I can't figure out how to create a
> Builder for an embedded message.
>
> Here's my first try:
>
>           switch (fieldDescriptor.getJavaType()) {
>             ...
>             case MESSAGE:
>               Class<T> type = (Class<T>)
> fieldDescriptor.getDefaultValue().getClass(); // getDefaultValue throws.
>               messageBuilder.addRepeatedField(fieldDescriptor,
>                   fromDBObject((BasicBSONObject) object.get(fieldName),
> type));
>               break;
>
> But the noted line throws with the error,
> "java.lang.UnsupportedOperationException: FieldDescriptor.getDefaultValue()
> called on an embedded message field."
>
> Here's my second try:
>           switch (fieldDescriptor.getJavaType()) {
>             ...
>             case MESSAGE:
>               Message.Builder embeddedMessageBuilder =
> messageBuilder.getFieldBuilder(fieldDescriptor); // getFieldBuilder throws.
>               Class<T> type = (Class<T>)
> embeddedMessageBuilder.getDefaultInstanceForType().getClass();
>               messageBuilder.addRepeatedField(fieldDescriptor,
>                   fromDBObject((BasicBSONObject) object.get(fieldName),
> type));
>               break;
>
> Here, the noted line throws with the error,
> "java.lang.UnsupportedOperationException: getFieldBuilder() called on a
> non-Message type."
>
> I'm not sure what else to try.  My question boils down to a very simple
> one: Given a FieldDescriptor that describes an embedded repeated MESSAGE
> field, how can you create a Builder for it?
>
> Thanks!
> jon
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at http://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 protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to