protobuf
protobuf copied to clipboard
Iterate messages in the order of appearance in file with Python API
I'm writing a plugin for protoc and I'm using Python. I'm having trouble with iterating messages in a file, given a FileDescriptor
object. The class provides an attribute message_types_by_name
. When iterating this dictionary, the messages are no longer in the order of their original appearance in the proto file.
Since message definitions have dependencies, being able to iterate them in topological order has certain advantages. Is there any canonical way to do this? I scanned the related APIs but didn't find a nice solution.
I'm not very familiar with python protobuf but I guess there's no public API for that. You may do the topologyical sorting manually by searching all dependency messages.
Based on my research, elements in message_types_by_name
are actually inserted in order when FileDescriptor
objects are created and initialized. It's just that Python dict
does not preserve insertion order. Switching to collections.OrderedDict
seems an easy enough solution to me, if the team is interested in fixing this. Or the API can provide an additional attribute named messages
which is a list that preserves this order.
This applies to enum_types_by_name
too, but as far as I know there are no dependency problems for enums so it's probably not a big deal anyway.
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.
This issue is labeled inactive
because the last activity was over 90 days ago.
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.
This issue was closed and archived because there has been no new activity in the 14 days since the inactive
label was added.