Skip to content

Commit

Permalink
fix default value handling for NativeType trait
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Nov 15, 2022
1 parent b9e0379 commit 9a9aa33
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 92 deletions.
2 changes: 1 addition & 1 deletion examples/custom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ path = "src/client.rs"

[dependencies]
ntex = "0.5"
ntex-grpc = "0.1.0"
ntex-grpc = "0.2.0-b.2"

async-trait = "0.1.0"
clap = "3.2"
Expand Down
40 changes: 31 additions & 9 deletions examples/custom/src/helloworld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
unused_mut,
unused_variables,
clippy::identity_op,
clippy::derivable_impls
clippy::derivable_impls,
clippy::unit_arg
)]
/// DO NOT MODIFY. Auto-generated file
/// The request message containing the user's name.
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct HelloRequest {
pub name: ::ntex_grpc::ByteString,
pub msg_id: crate::unique_id::UniqueId,
}

/// The response message containing the greetings
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct HelloReply {
pub message: ::ntex_grpc::ByteString,
}
Expand Down Expand Up @@ -105,8 +106,13 @@ impl<T: ::ntex_grpc::Transport<GreeterSayHelloMethod>> GreeterClient<T> {
impl ::ntex_grpc::Message for HelloRequest {
#[inline]
fn write(&self, dst: &mut ::ntex_grpc::BytesMut) {
::ntex_grpc::NativeType::serialize(&self.name, 1, None, dst);
::ntex_grpc::NativeType::serialize(&self.msg_id, 2, None, dst);
::ntex_grpc::NativeType::serialize(&self.name, 1, ::ntex_grpc::DefaultValue::Default, dst);
::ntex_grpc::NativeType::serialize(
&self.msg_id,
2,
::ntex_grpc::DefaultValue::Default,
dst,
);
}

#[inline]
Expand All @@ -130,8 +136,15 @@ impl ::ntex_grpc::Message for HelloRequest {

#[inline]
fn encoded_len(&self) -> usize {
0 + ::ntex_grpc::NativeType::serialized_len(&self.name, 1, None)
+ ::ntex_grpc::NativeType::serialized_len(&self.msg_id, 2, None)
0 + ::ntex_grpc::NativeType::serialized_len(
&self.name,
1,
::ntex_grpc::DefaultValue::Default,
) + ::ntex_grpc::NativeType::serialized_len(
&self.msg_id,
2,
::ntex_grpc::DefaultValue::Default,
)
}
}

Expand All @@ -147,7 +160,12 @@ impl ::std::default::Default for HelloRequest {
impl ::ntex_grpc::Message for HelloReply {
#[inline]
fn write(&self, dst: &mut ::ntex_grpc::BytesMut) {
::ntex_grpc::NativeType::serialize(&self.message, 1, None, dst);
::ntex_grpc::NativeType::serialize(
&self.message,
1,
::ntex_grpc::DefaultValue::Default,
dst,
);
}

#[inline]
Expand All @@ -169,7 +187,11 @@ impl ::ntex_grpc::Message for HelloReply {

#[inline]
fn encoded_len(&self) -> usize {
0 + ::ntex_grpc::NativeType::serialized_len(&self.message, 1, None)
0 + ::ntex_grpc::NativeType::serialized_len(
&self.message,
1,
::ntex_grpc::DefaultValue::Default,
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/custom/src/unique_id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ntex::util::{Bytes, BytesMut};
use uuid::Uuid;

#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct UniqueId(Uuid);

impl UniqueId {
Expand Down
2 changes: 1 addition & 1 deletion examples/helloworld/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ path = "src/server.rs"

[dependencies]
ntex = "0.5"
ntex-grpc = "0.1.0"
ntex-grpc = "0.2.0-b.2"

async-trait = "0.1.0"
clap = "2"
Expand Down
129 changes: 94 additions & 35 deletions examples/helloworld/src/helloworld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
unused_mut,
unused_variables,
clippy::identity_op,
clippy::derivable_impls
clippy::derivable_impls,
clippy::unit_arg
)]
/// DO NOT MODIFY. Auto-generated file
/// The request message containing the user's name.
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct HelloRequest {
pub name: ::ntex_grpc::ByteString,
}

/// The response message containing the greetings
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct HelloReply {
pub metadata: i64,
pub reply_type: hello_reply::Type,
Expand Down Expand Up @@ -102,7 +103,7 @@ pub mod hello_reply {
}
}

#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Result {
Success(super::ResponseResult),
ServiceError(i64),
Expand All @@ -122,17 +123,31 @@ pub mod hello_reply {
}

/// Encodes the message to a buffer.
fn serialize(&self, _: u32, _: Option<&Self>, dst: &mut ::ntex_grpc::BytesMut) {
fn serialize(
&self,
_: u32,
_: ::ntex_grpc::DefaultValue<&Self>,
dst: &mut ::ntex_grpc::BytesMut,
) {
match *self {
Result::Success(ref value) => {
::ntex_grpc::NativeType::serialize(value, 1, None, dst)
}
Result::ServiceError(ref value) => {
::ntex_grpc::NativeType::serialize(value, 2, None, dst)
}
Result::InvalidRequest(ref value) => {
::ntex_grpc::NativeType::serialize(value, 3, None, dst)
}
Result::Success(ref value) => ::ntex_grpc::NativeType::serialize(
value,
1,
::ntex_grpc::DefaultValue::Default,
dst,
),
Result::ServiceError(ref value) => ::ntex_grpc::NativeType::serialize(
value,
2,
::ntex_grpc::DefaultValue::Default,
dst,
),
Result::InvalidRequest(ref value) => ::ntex_grpc::NativeType::serialize(
value,
3,
::ntex_grpc::DefaultValue::Default,
dst,
),
}
}
/// Decodes an instance of the message from a buffer, and merges it into self.
Expand All @@ -158,17 +173,23 @@ pub mod hello_reply {
Ok(())
}
/// Returns the encoded length of the message without a length delimiter.
fn serialized_len(&self, _: u32, _: Option<&Self>) -> usize {
fn serialized_len(&self, _: u32, _: ::ntex_grpc::DefaultValue<&Self>) -> usize {
match *self {
Result::Success(ref value) => {
::ntex_grpc::NativeType::serialized_len(value, 1, None)
}
Result::ServiceError(ref value) => {
::ntex_grpc::NativeType::serialized_len(value, 2, None)
}
Result::InvalidRequest(ref value) => {
::ntex_grpc::NativeType::serialized_len(value, 3, None)
}
Result::Success(ref value) => ::ntex_grpc::NativeType::serialized_len(
value,
1,
::ntex_grpc::DefaultValue::Default,
),
Result::ServiceError(ref value) => ::ntex_grpc::NativeType::serialized_len(
value,
2,
::ntex_grpc::DefaultValue::Default,
),
Result::InvalidRequest(ref value) => ::ntex_grpc::NativeType::serialized_len(
value,
3,
::ntex_grpc::DefaultValue::Default,
),
}
}
}
Expand All @@ -179,7 +200,7 @@ pub mod hello_reply {
}
}
}
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ResponseResult {
pub message: ::ntex_grpc::ByteString,
}
Expand Down Expand Up @@ -269,7 +290,7 @@ impl<T: ::ntex_grpc::Transport<GreeterSayHelloMethod>> GreeterClient<T> {
impl ::ntex_grpc::Message for HelloRequest {
#[inline]
fn write(&self, dst: &mut ::ntex_grpc::BytesMut) {
::ntex_grpc::NativeType::serialize(&self.name, 1, None, dst);
::ntex_grpc::NativeType::serialize(&self.name, 1, ::ntex_grpc::DefaultValue::Default, dst);
}

#[inline]
Expand All @@ -291,7 +312,11 @@ impl ::ntex_grpc::Message for HelloRequest {

#[inline]
fn encoded_len(&self) -> usize {
0 + ::ntex_grpc::NativeType::serialized_len(&self.name, 1, None)
0 + ::ntex_grpc::NativeType::serialized_len(
&self.name,
1,
::ntex_grpc::DefaultValue::Default,
)
}
}

Expand All @@ -306,9 +331,24 @@ impl ::std::default::Default for HelloRequest {
impl ::ntex_grpc::Message for HelloReply {
#[inline]
fn write(&self, dst: &mut ::ntex_grpc::BytesMut) {
::ntex_grpc::NativeType::serialize(&self.metadata, 4, None, dst);
::ntex_grpc::NativeType::serialize(&self.reply_type, 5, None, dst);
::ntex_grpc::NativeType::serialize(&self.result, 0, None, dst);
::ntex_grpc::NativeType::serialize(
&self.metadata,
4,
::ntex_grpc::DefaultValue::Default,
dst,
);
::ntex_grpc::NativeType::serialize(
&self.reply_type,
5,
::ntex_grpc::DefaultValue::Default,
dst,
);
::ntex_grpc::NativeType::serialize(
&self.result,
0,
::ntex_grpc::DefaultValue::Default,
dst,
);
}

#[inline]
Expand Down Expand Up @@ -338,9 +378,19 @@ impl ::ntex_grpc::Message for HelloReply {

#[inline]
fn encoded_len(&self) -> usize {
0 + ::ntex_grpc::NativeType::serialized_len(&self.metadata, 4, None)
+ ::ntex_grpc::NativeType::serialized_len(&self.reply_type, 5, None)
+ ::ntex_grpc::NativeType::serialized_len(&self.result, 0, None)
0 + ::ntex_grpc::NativeType::serialized_len(
&self.metadata,
4,
::ntex_grpc::DefaultValue::Default,
) + ::ntex_grpc::NativeType::serialized_len(
&self.reply_type,
5,
::ntex_grpc::DefaultValue::Default,
) + ::ntex_grpc::NativeType::serialized_len(
&self.result,
0,
::ntex_grpc::DefaultValue::Default,
)
}
}

Expand All @@ -357,7 +407,12 @@ impl ::std::default::Default for HelloReply {
impl ::ntex_grpc::Message for ResponseResult {
#[inline]
fn write(&self, dst: &mut ::ntex_grpc::BytesMut) {
::ntex_grpc::NativeType::serialize(&self.message, 1, None, dst);
::ntex_grpc::NativeType::serialize(
&self.message,
1,
::ntex_grpc::DefaultValue::Default,
dst,
);
}

#[inline]
Expand All @@ -379,7 +434,11 @@ impl ::ntex_grpc::Message for ResponseResult {

#[inline]
fn encoded_len(&self) -> usize {
0 + ::ntex_grpc::NativeType::serialized_len(&self.message, 1, None)
0 + ::ntex_grpc::NativeType::serialized_len(
&self.message,
1,
::ntex_grpc::DefaultValue::Default,
)
}
}

Expand Down
4 changes: 4 additions & 0 deletions ntex-grpc-codegen/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [0.2.0-b.0] - 2022-11-15

* Generate code for ntex-grpc 0.2

## [0.1.6] - 2022-10-07

* Fix service name generation for empty package
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.1.6"
version = "0.2.0-b.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.10.7"
ntex-prost-build = "0.11.0-b.0"
log = "0.4"
env_logger = "0.9"
2 changes: 1 addition & 1 deletion ntex-grpc-codegen/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl NGrpcGenerator {

/// Map protobuf type to custom rust type
pub fn map_field_type(&mut self, path: &str, rust_type: &str) {
let _ = self.pconfig.map_field_type(&[path], rust_type);
let _ = self.pconfig.map_field_type([path], rust_type);
}

/// Compile `.proto` files into Rust files during a Cargo build with additional code generator
Expand Down
4 changes: 4 additions & 0 deletions ntex-grpc/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [0.2.0-b.2] - 2022-11-15

* Fix Option<T> encodinging

## [0.2.0-b.1] - 2022-11-14

* Fix default value for Option<T>, None is always default
Expand Down
6 changes: 1 addition & 5 deletions ntex-grpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-grpc"
version = "0.2.0-b.1"
version = "0.2.0-b.2"
license = "MIT"
authors = ["Nikolay Kim <[email protected]>"]
description = "GRPC Client/Server framework"
Expand All @@ -27,11 +27,7 @@ ntex-rt = "0.4"
ntex-grpc-derive = "0.1.0"

async-trait = "0.1.0"
bitflags = "1.3"
futures-util = "0.3.21"
slab = "0.4.2"
log = "0.4"
pin-project-lite = "0.2"
thiserror = "1.0"

[dev-dependencies]
Expand Down
Loading

0 comments on commit 9a9aa33

Please sign in to comment.