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