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): Expose prost-build type_attributes and field_attribu… #60

Merged
merged 2 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
tags
29 changes: 29 additions & 0 deletions tonic-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ mod server;
pub struct Builder {
build_client: bool,
build_server: bool,
field_attributes: Vec<(String, String)>,
type_attributes: Vec<(String, String)>,
out_dir: Option<PathBuf>,
#[cfg(feature = "rustfmt")]
format: bool,
Expand Down Expand Up @@ -109,6 +111,24 @@ impl Builder {
self
}

/// Add additional attribute to matched messages, enums, and one-offs.
///
/// Passed directly to `prost_build::Config.field_attribute`.
pub fn field_attribute<P: AsRef<str>, A: AsRef<str>>(mut self, path: P, attribute: A) -> Self {
self.field_attributes
.push((path.as_ref().to_string(), attribute.as_ref().to_string()));
self
}

/// Add additional attribute to matched messages, enums, and one-offs.
///
/// Passed directly to `prost_build::Config.type_attribute`.
pub fn type_attribute<P: AsRef<str>, A: AsRef<str>>(mut self, path: P, attribute: A) -> Self {
self.type_attributes
.push((path.as_ref().to_string(), attribute.as_ref().to_string()));
self
}

/// Compile the .proto files and execute code generation.
pub fn compile<P: AsRef<Path>>(self, protos: &[P], includes: &[P]) -> io::Result<()> {
let mut config = Config::new();
Expand All @@ -122,7 +142,14 @@ impl Builder {
.unwrap_or_else(|| PathBuf::from(std::env::var("OUT_DIR").unwrap()));

config.out_dir(out_dir.clone());
for (path, attr) in self.field_attributes.iter() {
config.field_attribute(path, attr);
}
for (path, attr) in self.type_attributes.iter() {
config.type_attribute(path, attr);
}
config.service_generator(Box::new(ServiceGenerator::new(self)));

config.compile_protos(protos, includes)?;

#[cfg(feature = "rustfmt")]
Expand All @@ -144,6 +171,8 @@ pub fn configure() -> Builder {
build_client: true,
build_server: true,
out_dir: None,
field_attributes: Vec::new(),
type_attributes: Vec::new(),
#[cfg(feature = "rustfmt")]
format: true,
}
Expand Down
3 changes: 3 additions & 0 deletions tonic-build/tests/wellknown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ fn wellknown() {
tonic_build::configure()
.out_dir(tmp)
.format(false)
.type_attribute(".", "#[derive(Serialize, Deserialize)]")
.type_attribute(".", "#[serde(rename_all = \"camelCase\")]")
.field_attribute("in", "#[serde(rename = \"in\")]")
.compile(&["tests/protos/wellknown.proto"], &["tests/protos"])
.unwrap();
}