Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there some way of getting a fields tag from its name? #137

Open
timthelion opened this issue Nov 18, 2018 · 6 comments
Open

Is there some way of getting a fields tag from its name? #137

timthelion opened this issue Nov 18, 2018 · 6 comments

Comments

@timthelion
Copy link
Contributor

I'd like to be able to write something like

get_tag!(message_name, field_name)

Rather than writing the numbers directly into my code in places.

@danburkert
Copy link
Collaborator

No, currently the mapping of fields to tag numbers isn’t exposed. Can you expand on why you would want to do this? I’ve never had a need to work with tags directly.

@timthelion
Copy link
Contributor Author

I use the tags for field permission management. I have a broker which can be configured to block messages with certain fields set. And the broker is configured with protobuf. I cannot think of any other way to programatically refer to fields.

@danburkert
Copy link
Collaborator

That’s an interesting usecase, thanks. If I were solving that with the upstream C++ library I’d probably use a custom annotation in the .proto along with a code generator plugin. Unfortunately prost doesn’t allow custom code gen plugins or hooks at the moment, and there’s no way to work with generic messages and schemes, so I don’t know of a synonymous way to do this.

An alternative is to manually implement a trait for your message types which would hold the filtering logic, but then it’s obviously configured through rust source code instead of the .proto.

@timthelion
Copy link
Contributor Author

@danburkert It shouldn't be necessary to modify the proto file with any sort of annotation. All that is needed is to find out the field's tag number so that the field can be referred to over the wire. This is a simple operation in other protobuf libraries:

I expected that something similar to golang's reflection would exist in rust for just this kind of problem, but it seems that is not the case: https://stackoverflow.com/questions/54573185/how-do-i-read-attributes-programatically-in-rust/54578601?noredirect=1#comment95957359_54578601

Would you accept a PR which appended to the generated code output a simple traversable data-structure which would include this metadata?

@timthelion
Copy link
Contributor Author

Re-reading your comment it appears to me that you misunderstood what I meant by "Configured with protobuf". What I meant, is that a list or map of disallowed field tags is set over the wire to the broker in protobuf format. Not that the broker is configured in a .proto file.

@benma
Copy link

benma commented Aug 21, 2020

I would also like this feature.

I have a oneof enum with one field per API call. I also keep a list of field tag numbers of which api calls are currently legal and compare against it when an API call is made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants