Add package to client handler params

This commit is contained in:
NGnius (Graham) 2023-04-17 22:43:24 -04:00
parent 7d326ea183
commit 4f629e3421
6 changed files with 11 additions and 9 deletions

4
Cargo.lock generated
View file

@ -281,7 +281,7 @@ checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a"
[[package]]
name = "nrpc"
version = "0.2.0"
version = "0.6.0"
dependencies = [
"async-trait",
"bytes",
@ -290,7 +290,7 @@ dependencies = [
[[package]]
name = "nrpc-build"
version = "0.5.0"
version = "0.6.0"
dependencies = [
"nrpc",
"prettyplease 0.2.4",

View file

@ -1,6 +1,6 @@
[package]
name = "nrpc-build"
version = "0.5.0"
version = "0.6.0"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/NGnius/nRPC"
@ -21,4 +21,4 @@ quote = "1.0"
syn = "2.0"
proc-macro2 = "1.0"
nrpc = { version = "0.2", path = "../nrpc" }
nrpc = { version = "0.6", path = "../nrpc" }

View file

@ -86,7 +86,7 @@ fn trait_methods_server(descriptors: &Vec<prost_build::Method>) -> proc_macro2::
}
}
fn struct_methods_client(service_name: &str, descriptors: &Vec<prost_build::Method>) -> proc_macro2::TokenStream {
fn struct_methods_client(package_name: &str, service_name: &str, descriptors: &Vec<prost_build::Method>) -> proc_macro2::TokenStream {
let mut gen_methods = Vec::with_capacity(descriptors.len());
for descriptor in descriptors {
match (descriptor.client_streaming, descriptor.server_streaming) {
@ -101,7 +101,7 @@ fn struct_methods_client(service_name: &str, descriptors: &Vec<prost_build::Meth
let mut in_buf = ::nrpc::_helpers::bytes::BytesMut::new();
input.encode(&mut in_buf)?;
let mut out_buf = ::nrpc::_helpers::bytes::BytesMut::new();
self.inner.call(#service_name, #method_name, in_buf.into(), &mut out_buf).await?;
self.inner.call(#package_name, #service_name, #method_name, in_buf.into(), &mut out_buf).await?;
Ok(#output_ty::decode(out_buf)?)
}
}
@ -183,7 +183,7 @@ impl ServiceGenerator for ProtobufServiceGenerator {
}
if self.generate_client {
let service_mod_name = quote::format_ident!("{}_mod_client", service.name.to_lowercase());
let service_methods = struct_methods_client(&service.name, &service.methods);
let service_methods = struct_methods_client(&service.package, &service.name, &service.methods);
let service_struct_name = quote::format_ident!("{}Service", service.name);
let descriptor_str = format!("{}.{}", service.package, service.name);
let service_rename = quote::format_ident!("{}Client", service.name);

View file

@ -48,11 +48,12 @@ struct ClientHandler;
#[async_trait::async_trait]
impl nrpc::ClientHandler for ClientHandler {
async fn call(&mut self,
package: &str,
service: &str,
method: &str,
input: bytes::Bytes,
output: &mut bytes::BytesMut) -> Result<(), nrpc::ServiceError> {
println!("call {}/{} with data {:?}", service, method, input);
println!("call {}.{}/{} with data {:?}", package, service, method, input);
// This is ok to hardcode ONLY because it's for testing
Ok(helloworld::HelloReply {
message: "Hello World".into(),

View file

@ -1,6 +1,6 @@
[package]
name = "nrpc"
version = "0.2.0"
version = "0.6.0"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/NGnius/nRPC"

View file

@ -11,6 +11,7 @@ pub trait ServerService {
#[async_trait::async_trait]
pub trait ClientHandler {
async fn call(&mut self,
package: &str,
service: &str,
method: &str,
input: bytes::Bytes,