Skip to content

Commit

Permalink
feat(build): Add disable_package_emission option to tonic-build (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
Protryon authored Feb 12, 2021
1 parent ca3b9a1 commit 4f5e160
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
15 changes: 6 additions & 9 deletions tonic-build/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use quote::{format_ident, quote};
///
/// This takes some `Service` and will generate a `TokenStream` that contains
/// a public module with the generated client.
pub fn generate<T: Service>(service: &T, proto_path: &str) -> TokenStream {
pub fn generate<T: Service>(service: &T, emit_package: bool, proto_path: &str) -> TokenStream {
let service_ident = quote::format_ident!("{}Client", service.name());
let client_mod = quote::format_ident!("{}_client", naive_snake_case(&service.name()));
let methods = generate_methods(service, proto_path);
let methods = generate_methods(service, emit_package, proto_path);

let connect = generate_connect(&service_ident);
let service_doc = generate_doc_comments(service.comment());
Expand Down Expand Up @@ -87,18 +87,15 @@ fn generate_connect(_service_ident: &syn::Ident) -> TokenStream {
TokenStream::new()
}

fn generate_methods<T: Service>(service: &T, proto_path: &str) -> TokenStream {
fn generate_methods<T: Service>(service: &T, emit_package: bool, proto_path: &str) -> TokenStream {
let mut stream = TokenStream::new();
let package = if emit_package { service.package() } else { "" };

for method in service.methods() {
let path = format!(
"/{}{}{}/{}",
service.package(),
if service.package().is_empty() {
""
} else {
"."
},
package,
if package.is_empty() { "" } else { "." },
service.identifier(),
method.identifier()
);
Expand Down
22 changes: 20 additions & 2 deletions tonic-build/src/prost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn configure() -> Builder {
proto_path: "super".to_string(),
#[cfg(feature = "rustfmt")]
format: true,
emit_package: true,
}
}

Expand Down Expand Up @@ -136,12 +137,20 @@ impl ServiceGenerator {
impl prost_build::ServiceGenerator for ServiceGenerator {
fn generate(&mut self, service: prost_build::Service, _buf: &mut String) {
if self.builder.build_server {
let server = server::generate(&service, &self.builder.proto_path);
let server = server::generate(
&service,
self.builder.emit_package,
&self.builder.proto_path,
);
self.servers.extend(server);
}

if self.builder.build_client {
let client = client::generate(&service, &self.builder.proto_path);
let client = client::generate(
&service,
self.builder.emit_package,
&self.builder.proto_path,
);
self.clients.extend(client);
}
}
Expand Down Expand Up @@ -184,6 +193,7 @@ pub struct Builder {
pub(crate) field_attributes: Vec<(String, String)>,
pub(crate) type_attributes: Vec<(String, String)>,
pub(crate) proto_path: String,
pub(crate) emit_package: bool,

out_dir: Option<PathBuf>,
#[cfg(feature = "rustfmt")]
Expand Down Expand Up @@ -258,6 +268,14 @@ impl Builder {
self
}

/// Emits GRPC endpoints with no attached package. Effectively ignores protofile package declaration from grpc context.
///
/// This effectively sets prost's exported package to an empty string.
pub fn disable_package_emission(mut self) -> Self {
self.emit_package = false;
self
}

/// Compile the .proto files and execute code generation.
pub fn compile<P>(self, protos: &[P], includes: &[P]) -> io::Result<()>
where
Expand Down
12 changes: 4 additions & 8 deletions tonic-build/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@ use syn::{Ident, Lit, LitStr};
///
/// This takes some `Service` and will generate a `TokenStream` that contains
/// a public module containing the server service and handler trait.
pub fn generate<T: Service>(service: &T, proto_path: &str) -> TokenStream {
pub fn generate<T: Service>(service: &T, emit_package: bool, proto_path: &str) -> TokenStream {
let methods = generate_methods(service, proto_path);

let server_service = quote::format_ident!("{}Server", service.name());
let server_trait = quote::format_ident!("{}", service.name());
let server_mod = quote::format_ident!("{}_server", naive_snake_case(&service.name()));
let generated_trait = generate_trait(service, proto_path, server_trait.clone());
let service_doc = generate_doc_comments(service.comment());

let package = if emit_package { service.package() } else { "" };
// Transport based implementations
let path = format!(
"{}{}{}",
service.package(),
if service.package().is_empty() {
""
} else {
"."
},
package,
if package.is_empty() { "" } else { "." },
service.identifier()
);
let transport = generate_transport(&server_service, &server_trait, &path);
Expand Down

0 comments on commit 4f5e160

Please sign in to comment.