benhodgson / protobuf-to-dict

A small Python library for creating dicts from protocol buffers. Useful as an intermediate step before serialization (e.g. to JSON).
Other
226 stars 108 forks source link

Patch #3

Closed chicagoben closed 11 years ago

chicagoben commented 11 years ago

If I had nested messages it wouldn't work as is. I had to change the fourth line to be:

if field.type not in type_callable_map and field.type != FieldDescriptor.TYPE_MESSAGE:

I added: "and field.type != FieldDescriptor.TYPE_MESSAGE"

Cheers, Ben

chicagoben commented 11 years ago

Another way is just to reorder the conditional:

if field.type == FieldDescriptor.TYPE_MESSAGE:

recursively encode protobuf sub-message

type_callable = lambda pb: protobuf_to_dict(pb,
            type_callable_map=type_callable_map,
            use_enum_labels=use_enum_labels)

elif field.type in type_callable_map: type_callable = type_callable_map[field.type] else: raise TypeError("Field %s.%s has unrecognised type id %d" % (pb.class.name, field.name, field.type))

benhodgson commented 11 years ago

Doh! That's a dumb bug. Thanks for letting me know. I’ll patch it ASAP :)

chicagoben commented 11 years ago

Cheers. Thanks for creating this!