Skip to content

Commit

Permalink
Allow to access request and create custom responses for server
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Nov 23, 2022
1 parent e60ca30 commit ed711ad
Show file tree
Hide file tree
Showing 22 changed files with 436 additions and 319 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ ntex-grpc = { path = "./ntex-grpc" }
ntex-grpc-codegen = { path = "./ntex-grpc-codegen" }
ntex-grpc-derive = { path = "./ntex-grpc-derive" }
ntex-prost-build = { path = "./prost-build" }

ntex-h2 = { path = "../ntex-h2" }
37 changes: 20 additions & 17 deletions examples/custom/src/helloworld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,9 @@ pub struct Greeter;
impl ::ntex_grpc::ServiceDef for Greeter {
const NAME: &'static str = "helloworld.Greeter";
type Methods = GreeterMethods;
}

pub enum GreeterMethods {
SayHello(GreeterSayHelloMethod),
}

impl ::ntex_grpc::MethodsDef for GreeterMethods {
#[inline]
fn by_name(name: &str) -> Option<Self> {
fn method_by_name(name: &str) -> Option<Self::Methods> {
use ::ntex_grpc::MethodDef;
match name {
GreeterSayHelloMethod::NAME => Some(GreeterMethods::SayHello(GreeterSayHelloMethod)),
Expand All @@ -44,6 +38,10 @@ impl ::ntex_grpc::MethodsDef for GreeterMethods {
}
}

pub enum GreeterMethods {
SayHello(GreeterSayHelloMethod),
}

#[derive(Clone)]
/// The greeting service definition.
pub struct GreeterClient<T>(T);
Expand All @@ -56,7 +54,7 @@ impl<T> GreeterClient<T> {
}
}

impl<T> ::ntex_grpc::ClientInformation<T> for GreeterClient<T> {
impl<T> ::ntex_grpc::client::ClientInformation<T> for GreeterClient<T> {
#[inline]
/// Create new client instance
fn create(transport: T) -> Self {
Expand Down Expand Up @@ -93,24 +91,29 @@ impl ::ntex_grpc::MethodDef for GreeterSayHelloMethod {
type Output = HelloReply;
}

impl<T: ::ntex_grpc::Transport<GreeterSayHelloMethod>> GreeterClient<T> {
impl<T: ::ntex_grpc::client::Transport<GreeterSayHelloMethod>> GreeterClient<T> {
/// Sends a greeting
pub fn say_hello<'a>(
&'a self,
req: &'a HelloRequest,
) -> ::ntex_grpc::Request<'a, T, GreeterSayHelloMethod> {
::ntex_grpc::Request::new(&self.0, req)
) -> ::ntex_grpc::client::Request<'a, T, GreeterSayHelloMethod> {
::ntex_grpc::client::Request::new(&self.0, req)
}
}

impl ::ntex_grpc::Message for HelloRequest {
#[inline]
fn write(&self, dst: &mut ::ntex_grpc::BytesMut) {
::ntex_grpc::NativeType::serialize(&self.name, 1, ::ntex_grpc::DefaultValue::Default, dst);
::ntex_grpc::NativeType::serialize(
&self.name,
1,
::ntex_grpc::types::DefaultValue::Default,
dst,
);
::ntex_grpc::NativeType::serialize(
&self.msg_id,
2,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
dst,
);
}
Expand Down Expand Up @@ -139,11 +142,11 @@ impl ::ntex_grpc::Message for HelloRequest {
0 + ::ntex_grpc::NativeType::serialized_len(
&self.name,
1,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
) + ::ntex_grpc::NativeType::serialized_len(
&self.msg_id,
2,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
)
}
}
Expand All @@ -163,7 +166,7 @@ impl ::ntex_grpc::Message for HelloReply {
::ntex_grpc::NativeType::serialize(
&self.message,
1,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
dst,
);
}
Expand All @@ -190,7 +193,7 @@ impl ::ntex_grpc::Message for HelloReply {
0 + ::ntex_grpc::NativeType::serialized_len(
&self.message,
1,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
)
}
}
Expand Down
61 changes: 32 additions & 29 deletions examples/helloworld/src/helloworld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,26 @@ pub mod hello_reply {
fn serialize(
&self,
_: u32,
_: ::ntex_grpc::DefaultValue<&Self>,
_: ::ntex_grpc::types::DefaultValue<&Self>,
dst: &mut ::ntex_grpc::BytesMut,
) {
match *self {
Result::Success(ref value) => ::ntex_grpc::NativeType::serialize(
value,
1,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
dst,
),
Result::ServiceError(ref value) => ::ntex_grpc::NativeType::serialize(
value,
2,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
dst,
),
Result::InvalidRequest(ref value) => ::ntex_grpc::NativeType::serialize(
value,
3,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
dst,
),
}
Expand Down Expand Up @@ -173,22 +173,22 @@ pub mod hello_reply {
Ok(())
}
/// Returns the encoded length of the message without a length delimiter.
fn serialized_len(&self, _: u32, _: ::ntex_grpc::DefaultValue<&Self>) -> usize {
fn serialized_len(&self, _: u32, _: ::ntex_grpc::types::DefaultValue<&Self>) -> usize {
match *self {
Result::Success(ref value) => ::ntex_grpc::NativeType::serialized_len(
value,
1,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
),
Result::ServiceError(ref value) => ::ntex_grpc::NativeType::serialized_len(
value,
2,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
),
Result::InvalidRequest(ref value) => ::ntex_grpc::NativeType::serialized_len(
value,
3,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
),
}
}
Expand All @@ -211,15 +211,9 @@ pub struct Greeter;
impl ::ntex_grpc::ServiceDef for Greeter {
const NAME: &'static str = "helloworld.Greeter";
type Methods = GreeterMethods;
}

pub enum GreeterMethods {
SayHello(GreeterSayHelloMethod),
}

impl ::ntex_grpc::MethodsDef for GreeterMethods {
#[inline]
fn by_name(name: &str) -> Option<Self> {
fn method_by_name(name: &str) -> Option<Self::Methods> {
use ::ntex_grpc::MethodDef;
match name {
GreeterSayHelloMethod::NAME => Some(GreeterMethods::SayHello(GreeterSayHelloMethod)),
Expand All @@ -228,6 +222,10 @@ impl ::ntex_grpc::MethodsDef for GreeterMethods {
}
}

pub enum GreeterMethods {
SayHello(GreeterSayHelloMethod),
}

#[derive(Clone)]
/// The greeting service definition.
pub struct GreeterClient<T>(T);
Expand All @@ -240,7 +238,7 @@ impl<T> GreeterClient<T> {
}
}

impl<T> ::ntex_grpc::ClientInformation<T> for GreeterClient<T> {
impl<T> ::ntex_grpc::client::ClientInformation<T> for GreeterClient<T> {
#[inline]
/// Create new client instance
fn create(transport: T) -> Self {
Expand Down Expand Up @@ -277,20 +275,25 @@ impl ::ntex_grpc::MethodDef for GreeterSayHelloMethod {
type Output = HelloReply;
}

impl<T: ::ntex_grpc::Transport<GreeterSayHelloMethod>> GreeterClient<T> {
impl<T: ::ntex_grpc::client::Transport<GreeterSayHelloMethod>> GreeterClient<T> {
/// Sends a greeting
pub fn say_hello<'a>(
&'a self,
req: &'a HelloRequest,
) -> ::ntex_grpc::Request<'a, T, GreeterSayHelloMethod> {
::ntex_grpc::Request::new(&self.0, req)
) -> ::ntex_grpc::client::Request<'a, T, GreeterSayHelloMethod> {
::ntex_grpc::client::Request::new(&self.0, req)
}
}

impl ::ntex_grpc::Message for HelloRequest {
#[inline]
fn write(&self, dst: &mut ::ntex_grpc::BytesMut) {
::ntex_grpc::NativeType::serialize(&self.name, 1, ::ntex_grpc::DefaultValue::Default, dst);
::ntex_grpc::NativeType::serialize(
&self.name,
1,
::ntex_grpc::types::DefaultValue::Default,
dst,
);
}

#[inline]
Expand All @@ -315,7 +318,7 @@ impl ::ntex_grpc::Message for HelloRequest {
0 + ::ntex_grpc::NativeType::serialized_len(
&self.name,
1,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
)
}
}
Expand All @@ -334,19 +337,19 @@ impl ::ntex_grpc::Message for HelloReply {
::ntex_grpc::NativeType::serialize(
&self.metadata,
4,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
dst,
);
::ntex_grpc::NativeType::serialize(
&self.reply_type,
5,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
dst,
);
::ntex_grpc::NativeType::serialize(
&self.result,
0,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
dst,
);
}
Expand Down Expand Up @@ -381,15 +384,15 @@ impl ::ntex_grpc::Message for HelloReply {
0 + ::ntex_grpc::NativeType::serialized_len(
&self.metadata,
4,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
) + ::ntex_grpc::NativeType::serialized_len(
&self.reply_type,
5,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
) + ::ntex_grpc::NativeType::serialized_len(
&self.result,
0,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
)
}
}
Expand All @@ -410,7 +413,7 @@ impl ::ntex_grpc::Message for ResponseResult {
::ntex_grpc::NativeType::serialize(
&self.message,
1,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
dst,
);
}
Expand All @@ -437,7 +440,7 @@ impl ::ntex_grpc::Message for ResponseResult {
0 + ::ntex_grpc::NativeType::serialized_len(
&self.message,
1,
::ntex_grpc::DefaultValue::Default,
::ntex_grpc::types::DefaultValue::Default,
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/helloworld/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl GreeterServer {
}
}

impl ServiceFactory<server::Request> for GreeterServer {
type Response = server::Response;
impl ServiceFactory<server::ServerRequest> for GreeterServer {
type Response = server::ServerResponse;
type Error = server::ServerError;
type InitError = ();
type Service = GreeterServer;
Expand Down
4 changes: 2 additions & 2 deletions ntex-grpc-codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-grpc-codegen"
version = "0.2.0-b.1"
version = "0.2.0"
license = "MIT"
authors = ["Nikolay Kim <[email protected]>"]
description = "GRPC Client/Server framework (codegen)"
Expand All @@ -17,6 +17,6 @@ path = "src/main.rs"

[dependencies]
clap = { version = "3.2", features = ["derive"] }
ntex-prost-build = "0.11.0-b.1"
ntex-prost-build = "0.11.0"
log = "0.4"
env_logger = "0.9"
23 changes: 10 additions & 13 deletions ntex-grpc-codegen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,20 @@ fn generate_client(service: &Service, buf: &mut String) {
impl ::ntex_grpc::ServiceDef for {} {{
const NAME: &'static str = \"{}\";
type Methods = {};
}}
pub enum {} {{
{}
}}
impl ::ntex_grpc::MethodsDef for {} {{
#[inline]
fn by_name(name: &str) -> Option<Self> {{
fn method_by_name(name: &str) -> Option<Self::Methods> {{
use ::ntex_grpc::MethodDef;
match name {{
{}
}}
}}
}}
pub enum {} {{
{}
}}
#[derive(Clone)]
{}
pub struct {}<T>(T);
Expand All @@ -103,7 +101,7 @@ fn generate_client(service: &Service, buf: &mut String) {
}}
}}
impl<T> ::ntex_grpc::ClientInformation<T> for {}<T> {{
impl<T> ::ntex_grpc::client::ClientInformation<T> for {}<T> {{
#[inline]
/// Create new client instance
fn create(transport: T) -> Self {{
Expand Down Expand Up @@ -134,10 +132,9 @@ fn generate_client(service: &Service, buf: &mut String) {
service_ident,
service_name,
service_methods_name,
service_methods_match,
service_methods_name,
service_methods,
service_methods_name,
service_methods_match,
comments,
client_ident,
client_ident,
Expand Down Expand Up @@ -183,10 +180,10 @@ fn gen_method(method: &Method, service: &Service) -> String {
type Output = {};
}}
impl<T: ::ntex_grpc::Transport<{}>> {}<T> {{
impl<T: ::ntex_grpc::client::Transport<{}>> {}<T> {{
{}
pub fn {}<'a>(&'a self, req: &'a {}) -> ::ntex_grpc::Request<'a, T, {}> {{
::ntex_grpc::Request::new(&self.0, req)
pub fn {}<'a>(&'a self, req: &'a {}) -> ::ntex_grpc::client::Request<'a, T, {}> {{
::ntex_grpc::client::Request::new(&self.0, req)
}}
}}",
def_ident,
Expand Down
Loading

0 comments on commit ed711ad

Please sign in to comment.