This is because EnumValueDescriptor doesn't override toString(). You can use
getName() and getValue() in the EnumValueDescriptor to read the value.
EnumValueDescriptor is defined in com.google.protobuf.Descriptors

On Sun, Jun 19, 2011 at 10:34 AM, Nik Hodgkinson <11x...@gmail.com> wrote:

> I'm working on a system to serialize descriptors and send them over the
> wire. All has gone fairly well with the exception of enums. I have no
> problem serializing the descriptor or deserializing it on the receiving end,
> but when I try to read from one I get an object like
> com.google.protobuf.Descriptors$EnumValueDescriptor@2993a66f and I can't
> seem to figure out how to get the value or name out of the object. So
> here's what I'm working with:
>
> First two functions that I've made from a snippet on someone else's post:
>
> private Descriptors.Descriptor getDescriptorForBytes(ByteString wireType)
>       throws InvalidProtocolBufferException,
> Descriptors.DescriptorValidationException
>   {
>     DescriptorPackage.WireDescriptor wireItem =
> DescriptorPackage.WireDescriptor.parseFrom(wireType);
>
>     DescriptorProtos.DescriptorProto protoOut =
> DescriptorProtos.DescriptorProto
>         .parseFrom(wireItem.getDescriptorProto());
>     DescriptorProtos.DescriptorProto modified =
>
> DescriptorProtos.DescriptorProto.newBuilder(protoOut).setName(wireItem.getProtoName()).build();
>     DescriptorProtos.FileDescriptorProto fileProto =
>         DescriptorProtos.FileDescriptorProto
>             .newBuilder()
>             .addMessageType(modified)
>             .setPackage(wireItem.getPackageName())
>             .build();
>     Descriptors.FileDescriptor file = Descriptors.FileDescriptor.buildFrom(
>         fileProto, new
>         Descriptors.FileDescriptor[]{}
>     );
>
>     return file.findMessageTypeByName(wireItem.getProtoName());
>   }
>
>   private ByteString getBytesForDescriptor(Descriptors.Descriptor desc)
>   {
>     DescriptorProtos.DescriptorProto proto = desc.toProto();
>     String name = Testing.TestItem.getDescriptor().getFullName();
>     int dotIndex = name.lastIndexOf('.');
>     String protoName = name.substring(dotIndex + 1);
>     String packageName = (dotIndex == -1) ? "" : name.substring(0,
> dotIndex);
>
>     ByteString bytes = proto.toByteString();
>
>     return DescriptorPackage.WireDescriptor
>         .newBuilder()
>         .setProtoName(protoName)
>         .setPackageName(packageName)
>         .setDescriptorProto(bytes)
>         .build()
>         .toByteString();
>   }
>
> Then I have the following proto:
>
> message TestItem {
>   required int32 id = 1;
>   required string name = 2;
>
>   enum Type {
>     FOO = 0;
>     BAR = 1;
>     BAZ = 2;
>   }
>   optional Type type = 3;
>
>   message SubItem {
>     required string name = 1;
>     optional int32 id = 2;
>   }
>
>   optional SubItem item = 5;
>
>   repeated string data = 4;
> }
>
> Finally I have a deserialized descriptor which I use to create a dynamic
> message:
>
> Descriptors.Descriptor descriptor = getDescriptorForBytes(descriptorBytes);
>
> DynamicMessage item = DynamicMessage.parseFrom(descriptor, testItemBytes);
>
> Map<Descriptors.FieldDescriptor, Object> fields = item.getAllFields();
> Set<Descriptors.FieldDescriptor> keys = fields.keySet();
>
> for (Descriptors.FieldDescriptor i : keys) {
>   System.out.println(i.getName());
>   Object field = fields.get(i);
>   System.out.println(field);
>   System.out.println();
> }
>
> Which outputs:
>
> id
> 1
>
> name
> Test 1
>
> type
> com.google.protobuf.Descriptors$EnumValueDescriptor@2993a66f
>
> data
> [Datum 1, Datum 2]
>
> item
> name: "Foo"
> id: 42
>
> So the question is then how can I get the enum over the wire and make use
> of it on the other end? Also, using the method in the functions above are
> there any other gotcha's that I might be missing?
>
> Thanks,
> -N
>
>  --
> 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/-/FtyKvJNj4joJ.
> To post to this group, send email to protobuf@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.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@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