Support gRPC packages in generated code
Include package names in the `NamedService` identifier and request paths. Change generated imports to use `crate` and add `tokio` and `tokio-stream` dependencies to `roto-tonic`.
This commit is contained in:
@@ -562,7 +562,7 @@ pub fn generate_rust_code(
|
||||
output.push_str("use futures_util::StreamExt;\n");
|
||||
output.push_str("use http_body_util::BodyExt;\n");
|
||||
output.push_str("use http_body::Body;\n");
|
||||
output.push_str("use roto_tonic::{BufferPool, StatusBody};\n\n");
|
||||
output.push_str("use crate::{BufferPool, StatusBody};\n\n");
|
||||
|
||||
for dep_res in file_proto.dependency() {
|
||||
let (dep_data, _) = dep_res.expect("Failed to iterate dependency");
|
||||
@@ -595,6 +595,7 @@ pub fn generate_rust_code(
|
||||
let (svc_data, _) = svc_res.expect("Failed to iterate service");
|
||||
write_service(
|
||||
&ServiceDescriptorProto::new(svc_data).expect("Failed to parse ServiceDescriptorProto"),
|
||||
file_proto.package().unwrap_or(""),
|
||||
&mut output,
|
||||
);
|
||||
}
|
||||
@@ -654,7 +655,7 @@ pub fn generate_rust_code(
|
||||
generated_files
|
||||
}
|
||||
|
||||
fn write_service(svc_proto: &ServiceDescriptorProto, output: &mut String) {
|
||||
fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut String) {
|
||||
let svc_name = to_pascal_case(svc_proto.name().unwrap());
|
||||
output.push_str(&format!("#[tonic::async_trait]\npub trait {}: Send + Sync + 'static {{\n", svc_name));
|
||||
|
||||
@@ -707,7 +708,12 @@ fn write_service(svc_proto: &ServiceDescriptorProto, output: &mut String) {
|
||||
output.push_str("}\n\n");
|
||||
|
||||
output.push_str(&format!("impl tonic::server::NamedService for {} {{\n", server_name));
|
||||
output.push_str(&format!(" const NAME: &'static str = \"{}\";\n", svc_proto.name().unwrap()));
|
||||
let full_svc_name = if package.is_empty() {
|
||||
svc_proto.name().unwrap().to_string()
|
||||
} else {
|
||||
format!("{}.{}", package, svc_proto.name().unwrap())
|
||||
};
|
||||
output.push_str(&format!(" const NAME: &'static str = \"{}\";\n", full_svc_name));
|
||||
output.push_str("}\n\n");
|
||||
|
||||
output.push_str(&format!("impl Service<http::Request<BoxBody>> for {} {{\n", server_name));
|
||||
@@ -756,7 +762,12 @@ fn write_service(svc_proto: &ServiceDescriptorProto, output: &mut String) {
|
||||
}
|
||||
|
||||
for (method_name, input_owned) in methods {
|
||||
output.push_str(&format!(" if path == \"/{}/{}\" {{\n", svc_proto.name().unwrap(), method_name));
|
||||
let full_path = if package.is_empty() {
|
||||
format!("/{}/{}", svc_proto.name().unwrap(), method_name)
|
||||
} else {
|
||||
format!("/{}.{}/{}", package, svc_proto.name().unwrap(), method_name)
|
||||
};
|
||||
output.push_str(&format!(" if path == \"{}\" {{\n", full_path));
|
||||
output.push_str(&format!(" let request_msg = match {}::decode(payload) {{\n", input_owned));
|
||||
output.push_str(" Ok(msg) => msg,\n");
|
||||
output.push_str(" Err(e) => {\n");
|
||||
|
||||
Reference in New Issue
Block a user