feat(build): Add build_transport builder option (#1130)
This commit is contained in:
@@ -22,3 +22,7 @@ members = [
|
|||||||
"tests/compression",
|
"tests/compression",
|
||||||
"tonic-web/tests/integration",
|
"tonic-web/tests/integration",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[patch.crates-io]
|
||||||
|
prost-build = { git = "https://github.com/tokio-rs/prost/", branch = "lucio/format" }
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ version = "0.8.2"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
prettyplease = { version = "0.1" }
|
prettyplease = { version = "0.1" }
|
||||||
proc-macro2 = "1.0"
|
proc-macro2 = "1.0"
|
||||||
prost-build = { version = "0.11", optional = true }
|
prost-build = { version = "0.11.1", optional = true }
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
syn = "1.0"
|
syn = "1.0"
|
||||||
|
|
||||||
|
|||||||
@@ -12,13 +12,14 @@ pub fn generate<T: Service>(
|
|||||||
emit_package: bool,
|
emit_package: bool,
|
||||||
proto_path: &str,
|
proto_path: &str,
|
||||||
compile_well_known_types: bool,
|
compile_well_known_types: bool,
|
||||||
|
build_transport: bool,
|
||||||
attributes: &Attributes,
|
attributes: &Attributes,
|
||||||
) -> TokenStream {
|
) -> TokenStream {
|
||||||
let service_ident = quote::format_ident!("{}Client", service.name());
|
let service_ident = quote::format_ident!("{}Client", service.name());
|
||||||
let client_mod = quote::format_ident!("{}_client", naive_snake_case(service.name()));
|
let client_mod = quote::format_ident!("{}_client", naive_snake_case(service.name()));
|
||||||
let methods = generate_methods(service, emit_package, proto_path, compile_well_known_types);
|
let methods = generate_methods(service, emit_package, proto_path, compile_well_known_types);
|
||||||
|
|
||||||
let connect = generate_connect(&service_ident);
|
let connect = generate_connect(&service_ident, build_transport);
|
||||||
let service_doc = generate_doc_comments(service.comment());
|
let service_doc = generate_doc_comments(service.comment());
|
||||||
|
|
||||||
let package = if emit_package { service.package() } else { "" };
|
let package = if emit_package { service.package() } else { "" };
|
||||||
@@ -109,8 +110,8 @@ pub fn generate<T: Service>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "transport")]
|
#[cfg(feature = "transport")]
|
||||||
fn generate_connect(service_ident: &syn::Ident) -> TokenStream {
|
fn generate_connect(service_ident: &syn::Ident, enabled: bool) -> TokenStream {
|
||||||
quote! {
|
let connect_impl = quote! {
|
||||||
impl #service_ident<tonic::transport::Channel> {
|
impl #service_ident<tonic::transport::Channel> {
|
||||||
/// Attempt to create a new client by connecting to a given endpoint.
|
/// Attempt to create a new client by connecting to a given endpoint.
|
||||||
pub async fn connect<D>(dst: D) -> Result<Self, tonic::transport::Error>
|
pub async fn connect<D>(dst: D) -> Result<Self, tonic::transport::Error>
|
||||||
@@ -122,11 +123,17 @@ fn generate_connect(service_ident: &syn::Ident) -> TokenStream {
|
|||||||
Ok(Self::new(conn))
|
Ok(Self::new(conn))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if enabled {
|
||||||
|
connect_impl
|
||||||
|
} else {
|
||||||
|
TokenStream::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "transport"))]
|
#[cfg(not(feature = "transport"))]
|
||||||
fn generate_connect(_service_ident: &syn::Ident) -> TokenStream {
|
fn generate_connect(_service_ident: &syn::Ident, _enabled: bool) -> TokenStream {
|
||||||
TokenStream::new()
|
TokenStream::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -213,6 +213,14 @@ fn generate_attributes<'a>(
|
|||||||
|
|
||||||
// Generate a singular line of a doc comment
|
// Generate a singular line of a doc comment
|
||||||
fn generate_doc_comment<S: AsRef<str>>(comment: S) -> TokenStream {
|
fn generate_doc_comment<S: AsRef<str>>(comment: S) -> TokenStream {
|
||||||
|
let comment = comment.as_ref();
|
||||||
|
|
||||||
|
let comment = if !comment.starts_with(" ") {
|
||||||
|
format!(" {}", comment)
|
||||||
|
} else {
|
||||||
|
comment.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
let mut doc_stream = TokenStream::new();
|
let mut doc_stream = TokenStream::new();
|
||||||
|
|
||||||
doc_stream.append(Ident::new("doc", Span::call_site()));
|
doc_stream.append(Ident::new("doc", Span::call_site()));
|
||||||
|
|||||||
@@ -367,6 +367,7 @@ impl ServiceGenerator {
|
|||||||
true, // emit_package,
|
true, // emit_package,
|
||||||
"", // proto_path, -- not used
|
"", // proto_path, -- not used
|
||||||
false, // compile_well_known_types, -- not used
|
false, // compile_well_known_types, -- not used
|
||||||
|
self.builder.build_transport,
|
||||||
&Attributes::default(),
|
&Attributes::default(),
|
||||||
);
|
);
|
||||||
self.clients.extend(client);
|
self.clients.extend(client);
|
||||||
@@ -409,6 +410,7 @@ impl ServiceGenerator {
|
|||||||
pub struct Builder {
|
pub struct Builder {
|
||||||
build_server: bool,
|
build_server: bool,
|
||||||
build_client: bool,
|
build_client: bool,
|
||||||
|
build_transport: bool,
|
||||||
|
|
||||||
out_dir: Option<PathBuf>,
|
out_dir: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
@@ -418,6 +420,7 @@ impl Default for Builder {
|
|||||||
Self {
|
Self {
|
||||||
build_server: true,
|
build_server: true,
|
||||||
build_client: true,
|
build_client: true,
|
||||||
|
build_transport: true,
|
||||||
out_dir: None,
|
out_dir: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -445,6 +448,15 @@ impl Builder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Enable or disable generated clients and servers to have built-in tonic
|
||||||
|
/// transport features.
|
||||||
|
///
|
||||||
|
/// When the `transport` feature is disabled this does nothing.
|
||||||
|
pub fn build_transport(mut self, enable: bool) -> Self {
|
||||||
|
self.build_transport = enable;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set the output directory to generate code to.
|
/// Set the output directory to generate code to.
|
||||||
///
|
///
|
||||||
/// Defaults to the `OUT_DIR` environment variable.
|
/// Defaults to the `OUT_DIR` environment variable.
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ pub fn configure() -> Builder {
|
|||||||
Builder {
|
Builder {
|
||||||
build_client: true,
|
build_client: true,
|
||||||
build_server: true,
|
build_server: true,
|
||||||
|
build_transport: true,
|
||||||
file_descriptor_set_path: None,
|
file_descriptor_set_path: None,
|
||||||
out_dir: None,
|
out_dir: None,
|
||||||
extern_path: Vec::new(),
|
extern_path: Vec::new(),
|
||||||
@@ -172,6 +173,7 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
|
|||||||
self.builder.emit_package,
|
self.builder.emit_package,
|
||||||
&self.builder.proto_path,
|
&self.builder.proto_path,
|
||||||
self.builder.compile_well_known_types,
|
self.builder.compile_well_known_types,
|
||||||
|
self.builder.build_transport,
|
||||||
&self.builder.client_attributes,
|
&self.builder.client_attributes,
|
||||||
);
|
);
|
||||||
self.clients.extend(client);
|
self.clients.extend(client);
|
||||||
@@ -214,6 +216,7 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
|
|||||||
pub struct Builder {
|
pub struct Builder {
|
||||||
pub(crate) build_client: bool,
|
pub(crate) build_client: bool,
|
||||||
pub(crate) build_server: bool,
|
pub(crate) build_server: bool,
|
||||||
|
pub(crate) build_transport: bool,
|
||||||
pub(crate) file_descriptor_set_path: Option<PathBuf>,
|
pub(crate) file_descriptor_set_path: Option<PathBuf>,
|
||||||
pub(crate) extern_path: Vec<(String, String)>,
|
pub(crate) extern_path: Vec<(String, String)>,
|
||||||
pub(crate) field_attributes: Vec<(String, String)>,
|
pub(crate) field_attributes: Vec<(String, String)>,
|
||||||
@@ -243,6 +246,15 @@ impl Builder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Enable or disable generated clients and servers to have built-in tonic
|
||||||
|
/// transport features.
|
||||||
|
///
|
||||||
|
/// When the `transport` feature is disabled this does nothing.
|
||||||
|
pub fn build_transport(mut self, enable: bool) -> Self {
|
||||||
|
self.build_transport = enable;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Generate a file containing the encoded `prost_types::FileDescriptorSet` for protocol buffers
|
/// Generate a file containing the encoded `prost_types::FileDescriptorSet` for protocol buffers
|
||||||
/// modules. This is required for implementing gRPC Server Reflection.
|
/// modules. This is required for implementing gRPC Server Reflection.
|
||||||
pub fn file_descriptor_set_path(mut self, path: impl AsRef<Path>) -> Self {
|
pub fn file_descriptor_set_path(mut self, path: impl AsRef<Path>) -> Self {
|
||||||
|
|||||||
@@ -10,7 +10,17 @@ pub struct HealthCheckResponse {
|
|||||||
}
|
}
|
||||||
/// Nested message and enum types in `HealthCheckResponse`.
|
/// Nested message and enum types in `HealthCheckResponse`.
|
||||||
pub mod health_check_response {
|
pub mod health_check_response {
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
|
#[derive(
|
||||||
|
Clone,
|
||||||
|
Copy,
|
||||||
|
Debug,
|
||||||
|
PartialEq,
|
||||||
|
Eq,
|
||||||
|
Hash,
|
||||||
|
PartialOrd,
|
||||||
|
Ord,
|
||||||
|
::prost::Enumeration
|
||||||
|
)]
|
||||||
#[repr(i32)]
|
#[repr(i32)]
|
||||||
pub enum ServingStatus {
|
pub enum ServingStatus {
|
||||||
Unknown = 0,
|
Unknown = 0,
|
||||||
|
|||||||
@@ -141,7 +141,10 @@ pub struct ErrorInfo {
|
|||||||
/// {"instanceLimitPerRequest": "100"}, if the client exceeds the number of
|
/// {"instanceLimitPerRequest": "100"}, if the client exceeds the number of
|
||||||
/// instances that can be created in a single (batch) request.
|
/// instances that can be created in a single (batch) request.
|
||||||
#[prost(map = "string, string", tag = "3")]
|
#[prost(map = "string, string", tag = "3")]
|
||||||
pub metadata: ::std::collections::HashMap<::prost::alloc::string::String, ::prost::alloc::string::String>,
|
pub metadata: ::std::collections::HashMap<
|
||||||
|
::prost::alloc::string::String,
|
||||||
|
::prost::alloc::string::String,
|
||||||
|
>,
|
||||||
}
|
}
|
||||||
/// Describes what preconditions have failed.
|
/// Describes what preconditions have failed.
|
||||||
///
|
///
|
||||||
|
|||||||
Reference in New Issue
Block a user