protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Iterate messages in the order of appearance in file with Python API

Open uraj opened this issue 6 years ago • 3 comments

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.

uraj avatar Jan 02 '19 05:01 uraj

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.

BSBandme avatar Jan 03 '19 18:01 BSBandme

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.

uraj avatar Jan 03 '19 19:01 uraj

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.

github-actions[bot] avatar May 05 '24 10:05 github-actions[bot]

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.

github-actions[bot] avatar May 21 '24 10:05 github-actions[bot]