Skip to content

Commit

Permalink
feat(build): Add 'use_generic_streaming_requests'
Browse files Browse the repository at this point in the history
Adds an option for server trait generation that causes client-streaming
methods to receive a generic `impl IntoStreamingRequest` param
instead of a concrete `Request<Streaming<...>>` param.
This allows for testing trait implementions with any type implementing `Stream`,
and not having to go through the trouble of encoding a `Streaming`
object.

Fixes #462
  • Loading branch information
yotamofek committed Jan 2, 2025
1 parent 363ae8a commit 47ee328
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ members = [
"tests/web",
"tests/service_named_result",
"tests/use_arc_self",
"tests/use_generic_streaming_requests",
"tests/default_stubs",
"tests/deprecated_methods",
"tests/skip_debug",
Expand Down
17 changes: 17 additions & 0 deletions tests/use_generic_streaming_requests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
authors = ["Yotam Ofek <[email protected]>"]
edition = "2021"
license = "MIT"
name = "use_generic_streaming_requests"

[dependencies]
tokio-stream = "0.1"
prost = "0.13"
tonic = {path = "../../tonic"}
tokio = {version = "1.0", features = ["macros"]}

[build-dependencies]
tonic-build = {path = "../../tonic-build" }

[package.metadata.cargo-machete]
ignored = ["prost"]
19 changes: 19 additions & 0 deletions tests/use_generic_streaming_requests/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020 Lucio Franco

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
6 changes: 6 additions & 0 deletions tests/use_generic_streaming_requests/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
tonic_build::configure()
.use_generic_streaming_requests(true)
.compile_protos(&["proto/test.proto"], &["proto"])
.unwrap();
}
9 changes: 9 additions & 0 deletions tests/use_generic_streaming_requests/proto/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

package test;

service Test {
rpc TestRequest(stream Message) returns (Message);
}

message Message {}
35 changes: 35 additions & 0 deletions tests/use_generic_streaming_requests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use tokio_stream::StreamExt;
use tonic::{IntoStreamingRequest, Response, Status};

tonic::include_proto!("test");

#[derive(Debug, Default)]
pub struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn test_request(
&self,
req: impl IntoStreamingRequest<Message = Result<Message, Status>, Stream: Unpin> + Send,
) -> Result<Response<Message>, Status> {
let mut req = req.into_streaming_request().into_inner();
while let Some(message) = req.try_next().await? {
println!("Got message: {message:?}")
}

Ok(Response::new(Message {}))
}
}

#[cfg(test)]
mod tests {
use super::test_server::Test;
use super::*;

#[tokio::test]
async fn test_request_handler() {
let incoming_messages = tokio_stream::iter([Message {}, Message {}].map(Ok));
let svc = Svc;
svc.test_request(incoming_messages).await.unwrap();
}
}
16 changes: 16 additions & 0 deletions tonic-build/src/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct CodeGenBuilder {
disable_comments: HashSet<String>,
use_arc_self: bool,
generate_default_stubs: bool,
use_generic_streaming_requests: bool,
}

impl CodeGenBuilder {
Expand Down Expand Up @@ -71,6 +72,19 @@ impl CodeGenBuilder {
self
}

/// Enable or disable using `impl IntoStreamingRequest` instead of `Request<Streaming<Message>>`
/// as the parameter type for generated trait methods of client-streaming functions.
///
/// This allows calling those trait methods with any object that implements `Stream<Item = Result<Message, Status>>`,
/// which can be helpful for testing request handler logic.
pub fn use_generic_streaming_requests(
&mut self,
use_generic_streaming_requests: bool,
) -> &mut Self {
self.use_generic_streaming_requests = use_generic_streaming_requests;
self
}

/// Generate client code based on `Service`.
///
/// This takes some `Service` and will generate a `TokenStream` that contains
Expand Down Expand Up @@ -101,6 +115,7 @@ impl CodeGenBuilder {
&self.disable_comments,
self.use_arc_self,
self.generate_default_stubs,
self.use_generic_streaming_requests,
)
}
}
Expand All @@ -115,6 +130,7 @@ impl Default for CodeGenBuilder {
disable_comments: HashSet::default(),
use_arc_self: false,
generate_default_stubs: false,
use_generic_streaming_requests: false,
}
}
}
15 changes: 15 additions & 0 deletions tonic-build/src/prost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn configure() -> Builder {
disable_comments: HashSet::default(),
use_arc_self: false,
generate_default_stubs: false,
use_generic_streaming_requests: false,
compile_settings: CompileSettings::default(),
skip_debug: HashSet::default(),
}
Expand Down Expand Up @@ -228,6 +229,7 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
.disable_comments(self.builder.disable_comments.clone())
.use_arc_self(self.builder.use_arc_self)
.generate_default_stubs(self.builder.generate_default_stubs)
.use_generic_streaming_requests(self.builder.use_generic_streaming_requests)
.generate_server(
&TonicBuildService::new(service.clone(), self.builder.compile_settings.clone()),
&self.builder.proto_path,
Expand Down Expand Up @@ -310,6 +312,7 @@ pub struct Builder {
pub(crate) disable_comments: HashSet<String>,
pub(crate) use_arc_self: bool,
pub(crate) generate_default_stubs: bool,
pub(crate) use_generic_streaming_requests: bool,
pub(crate) compile_settings: CompileSettings,
pub(crate) skip_debug: HashSet<String>,

Expand Down Expand Up @@ -584,6 +587,18 @@ impl Builder {
self
}

/// Enable or disable using `impl IntoStreamingRequest` instead of `Request<Streaming<Message>>`
/// as the parameter type for generated trait methods of client-streaming functions.
///
/// This allows calling those trait methods with any object that implements `Stream<Item = Result<Message, Status>>`,
/// which can be helpful for testing request handler logic.
///
/// This defaults to `false`.
pub fn use_generic_streaming_requests(mut self, use_generic_streaming_requests: bool) -> Self {
self.use_generic_streaming_requests = use_generic_streaming_requests;
self
}

/// Override the default codec.
///
/// If set, writes `{codec_path}::default()` in generated code wherever a codec is created.
Expand Down
19 changes: 14 additions & 5 deletions tonic-build/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub(crate) fn generate_internal<T: Service>(
disable_comments: &HashSet<String>,
use_arc_self: bool,
generate_default_stubs: bool,
use_generic_streaming_requests: bool,
) -> TokenStream {
let methods = generate_methods(
service,
Expand All @@ -41,6 +42,7 @@ pub(crate) fn generate_internal<T: Service>(
disable_comments,
use_arc_self,
generate_default_stubs,
use_generic_streaming_requests,
);
let package = if emit_package { service.package() } else { "" };
// Transport based implementations
Expand Down Expand Up @@ -203,6 +205,7 @@ fn generate_trait<T: Service>(
disable_comments: &HashSet<String>,
use_arc_self: bool,
generate_default_stubs: bool,
use_generic_streaming_requests: bool,
) -> TokenStream {
let methods = generate_trait_methods(
service,
Expand All @@ -212,6 +215,7 @@ fn generate_trait<T: Service>(
disable_comments,
use_arc_self,
generate_default_stubs,
use_generic_streaming_requests,
);
let trait_doc = generate_doc_comment(format!(
" Generated trait containing gRPC methods that should be implemented for use with {}Server.",
Expand All @@ -227,6 +231,7 @@ fn generate_trait<T: Service>(
}
}

#[allow(clippy::too_many_arguments)]
fn generate_trait_methods<T: Service>(
service: &T,
emit_package: bool,
Expand All @@ -235,6 +240,7 @@ fn generate_trait_methods<T: Service>(
disable_comments: &HashSet<String>,
use_arc_self: bool,
generate_default_stubs: bool,
use_generic_streaming_requests: bool,
) -> TokenStream {
let mut stream = TokenStream::new();

Expand All @@ -257,10 +263,16 @@ fn generate_trait_methods<T: Service>(
quote!(&self)
};

let req_param_type = if method.client_streaming() {
let result = |ok| quote!(std::result::Result<#ok, tonic::Status>);
let response_result = |message| result(quote!(tonic::Response<#message>));

let req_param_type = if !method.client_streaming() {
quote!(tonic::Request<#req_message>)
} else if !use_generic_streaming_requests {
quote!(tonic::Request<tonic::Streaming<#req_message>>)
} else {
quote!(tonic::Request<#req_message>)
let message_ty = result(req_message);
quote!(impl tonic::IntoStreamingRequest<Message = #message_ty, Stream: Unpin> + std::marker::Send)
};

let partial_sig = quote! {
Expand All @@ -278,9 +290,6 @@ fn generate_trait_methods<T: Service>(
quote!(;)
};

let result = |ok| quote!(std::result::Result<#ok, tonic::Status>);
let response_result = |message| result(quote!(tonic::Response<#message>));

let method = if !method.server_streaming() {
let return_ty = response_result(res_message);
quote! {
Expand Down

0 comments on commit 47ee328

Please sign in to comment.