> I don't want to send the whole enchilada on the wire - just the
> descriptor for the message being sent.
>
> As I showed above, I know how to get the descriptor on the wire, but
> the other side (getting the descriptor off the wire and parsing the
> message using it) is unclear to me.

You'll need to bundle it up into a FileDescriptorProto (on the
receiving side), which is the only unit at which the system can turn
descriptors into actual messages.  The code looks something like this:

DescriptorProto proto = whatever;
FileDescriptorProto fileProto =
FileDescriptorProto.newBuilder().addMessageType(proto).build();
FileDescriptor file = FileDescriptor.buildFrom(fileProto, new
FileDescriptor[] {});
Descriptor descriptor = file.getMessageTypes().get(0);

Note that this won't work if you use submessages, because submessage
references in DescriptorProtos include their package, and this way of
doing it doesn't specify a package, so it won't be able to find the
submessage definition (unless you're using the default package, of
course).  To fix that, you can either pass the package name along with
the DescriptorProto, or you can munge the DescriptorProto on the
sending side to include the package in its name, resulting in code
like the following.

DescriptorProto proto = whatever;
String name = proto.getName();
int dotIndex = name.lastIndexOf('.');
String protoName = name.substring(dotIndex + 1);
String packageName = (dotIndex == -1) ? "" : name.substring(0, dotIndex);
DescriptorProto modified =
DescriptorProto.newBuilder(proto).setName(protoName).build();
FileDescriptorProto fileProto =
FileDescriptorProto.newBuilder().addMessageType(modified).setPackage(packageName).build();
FileDescriptor file = FileDescriptor.buildFrom(fileProto, new
FileDescriptor[] {});
Descriptor descriptor = file.getMessageTypes().get(0);

Once you have the descriptor, you just need you use one of the
DynamicMessage.parseFrom() methods to parse the message.

- Adam

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to