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

feat(build): support adding attributes to clients and servers #684

Merged
merged 1 commit into from
Jul 8, 2021

Conversation

cab
Copy link
Contributor

@cab cab commented Jun 17, 2021

Motivation

Implements #504.

Having the ability to add attributes to generated servers and clients allows things like feature gating or deriving additional service functionality without the need for a fork.

Solution

This PR only changes tonic-build. Four new methods are added to Builder:

method description
server_mod_attribute Adds a new attribute to the mod wrapping the server implementation.
server_attribute Adds a new attribute to the service server struct.
client_mod_attribute Adds a new attribute to the mod wrapping the client implementation.
client_attribute Adds a new attribute to the service client struct.

These are all optional values. Calling them more than once is additive.

Each method accepts a pattern and an attribute. pattern follows similar rules as tonic's attribute matching rules.

If pattern matches for a given service, the respective attribute values are parsed as syn::Attributes and added to the generated code in server::generate and client::generate.

} else if pattern == path {
true
} else {
let pattern_segments = pattern.split('.').collect::<Vec<_>>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left a few questions.

tonic-build/src/client.rs Outdated Show resolved Hide resolved
tonic-build/src/prost.rs Outdated Show resolved Hide resolved
@davidpdrsn davidpdrsn changed the title support for adding attributes to generated clients and servers feat(build): support adding attributes to clients and servers Jul 8, 2021
@cab cab force-pushed the service-attrs branch 2 times, most recently from 8bb9130 to 68fae34 Compare July 8, 2021 15:12
@cab cab force-pushed the service-attrs branch 2 times, most recently from 00c6df6 to 37ccbbd Compare July 8, 2021 15:16
@cab cab force-pushed the service-attrs branch 2 times, most recently from 755b989 to 55a7554 Compare July 8, 2021 15:46
Comment on lines 162 to 165
/// `mod` attributes.
pub module: Vec<(String, String)>,
/// `struct` attributes.
pub structure: Vec<(String, String)>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do these need to be public?

Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thank you for getting this over the finish line!

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 this pull request may close these issues.

3 participants