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:
2026-05-16 16:57:01 -07:00
parent 809a0d844c
commit 2202548ae5
4 changed files with 23 additions and 4 deletions
Generated
+2
View File
@@ -1201,6 +1201,8 @@ dependencies = [
"http-body-util",
"prost",
"roto-runtime",
"tokio",
"tokio-stream",
"tonic",
"tower 0.4.13",
]
+15 -4
View File
@@ -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");
+2
View File
@@ -12,4 +12,6 @@ http-body = "1.0"
http-body-util = "0.1"
tower = "0.4"
futures-util = "0.3"
tokio-stream = { version = "0.1", features = ["net"] }
tokio = { version = "1.38", features = ["full"] }
http = "1.1"
+4
View File
@@ -8,6 +8,10 @@ use std::future::Future;
use std::task::{Context, Poll};
use http_body::Body;
pub mod generated {
pub mod helloworld;
}
pub struct RotoCodec<T, U> {
_phantom: PhantomData<(T, U)>,
}