Skip to content

Commit

Permalink
fork prost-build and prost-derive; allow to use custom types for byte…
Browse files Browse the repository at this point in the history
…s type
  • Loading branch information
fafhrd91 committed Jun 27, 2022
1 parent 6f68dc9 commit b95a16b
Show file tree
Hide file tree
Showing 54 changed files with 9,469 additions and 37 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
members = [
"ntex-grpc",
"ntex-grpc-codegen",
"prost-build",
"prost-derive",
"examples/helloworld"
]

[patch.crates-io]
ntex-grpc = { path = "./ntex-grpc" }
ntex-grpc-codegen = { path = "./ntex-grpc-codegen" }

ntex-h2 = { git = "https://github.com/ntex-rs/ntex-h2.git" }
ntex-prost-build = { path = "./prost-build" }
ntex-prost-derive = { path = "./prost-derive" }
4 changes: 2 additions & 2 deletions examples/helloworld/helloworld.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ service Greeter {

// The request message containing the user's name.
message HelloRequest {
string name = 1;
bytes name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}
}
3 changes: 2 additions & 1 deletion examples/helloworld/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ fn main() {
}

loop {
client
let res = client
.say_hello(&HelloRequest {
name: "world".into(),
})
.await
.unwrap();
println!("RES: {:?}", res);
counters.register_request();
break;
}
Expand Down
36 changes: 22 additions & 14 deletions examples/helloworld/src/helloworld.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
#![allow(dead_code)]
/// DO NOT MODIFY. Auto-generated file
use ntex_grpc::codegen as ngrpc;

/// The request message containing the user's name.
#[derive(Clone, PartialEq, ::prost::Message)]
/// The request message containing the user's name.
#[derive(Clone, PartialEq, ::ntex_grpc::Message)]
pub struct HelloRequest {
#[prost(string, tag = "1")]
pub name: ::prost::alloc::string::String,
#[prost(bytes, tag = "1")]
pub name: ::ntex_grpc::types::Bytes,
}
/// The response message containing the greetings
#[derive(Clone, PartialEq, ::prost::Message)]
/// The response message containing the greetings
#[derive(Clone, PartialEq, ::ntex_grpc::Message)]
pub struct HelloReply {
#[prost(string, tag = "1")]
pub message: ::prost::alloc::string::String,
pub message: ::ntex_grpc::types::ByteString,
}

/// `Greeter` service client definition
#[doc = " The greeting service definition."]
#[derive(Clone)]
pub struct GreeterClient<T>(T);
impl<T> ngrpc::ClientInformation<T> for GreeterClient<T> {
impl<T> GreeterClient<T> {
#[inline]
#[doc = r" Create new client instance"]
pub fn new(transport: T) -> Self {
Self(transport)
}
}
impl<T> ::ntex_grpc::ClientInformation<T> for GreeterClient<T> {
#[inline]
#[doc = r" Create new client instance"]
fn create(transport: T) -> Self {
Expand All @@ -42,18 +49,19 @@ impl<T> ngrpc::ClientInformation<T> for GreeterClient<T> {
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct GreeterSayHelloMethod;
impl ngrpc::MethodDef for GreeterSayHelloMethod {
impl ::ntex_grpc::MethodDef for GreeterSayHelloMethod {
const NAME: &'static str = "SayHello";
const PATH: ngrpc::ByteString = ngrpc::ByteString::from_static("/helloworld.Greeter/SayHello");
const PATH: ::ntex_grpc::types::ByteString =
::ntex_grpc::types::ByteString::from_static("/helloworld.Greeter/SayHello");
type Input = HelloRequest;
type Output = HelloReply;
}
impl<T: ngrpc::Transport<GreeterSayHelloMethod>> GreeterClient<T> {
impl<T: ::ntex_grpc::Transport<GreeterSayHelloMethod>> GreeterClient<T> {
#[doc = " Sends a greeting"]
pub fn say_hello<'a>(
&'a self,
req: &'a HelloRequest,
) -> ngrpc::Request<'a, T, GreeterSayHelloMethod> {
ngrpc::Request::new(&self.0, req)
) -> ::ntex_grpc::Request<'a, T, GreeterSayHelloMethod> {
::ntex_grpc::Request::new(&self.0, req)
}
}
2 changes: 1 addition & 1 deletion ntex-grpc-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ path = "src/main.rs"

[dependencies]
clap = { version = "3.2", features = ["derive"] }
prost-build = "0.10"
ntex-prost-build = "0.10.1"
proc-macro2 = "1.0"
quote = "1.0"
syn = "1.0"
Expand Down
7 changes: 6 additions & 1 deletion ntex-grpc-codegen/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{env, io, ops, path::Path};

use prost_build::Config;
use ntex_prost_build::Config;

use crate::generator::GrpcServiceGenerator;

Expand All @@ -21,6 +21,11 @@ impl NGrpcGenerator {
Self { pconfig: cfg }
}

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

/// Compile `.proto` files into Rust files during a Cargo build with additional code generator
/// configuration options.
pub fn compile_protos(
Expand Down
16 changes: 8 additions & 8 deletions ntex-grpc-codegen/src/generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ntex_prost_build::{Method, Service, ServiceGenerator};
use proc_macro2::TokenStream;
use prost_build::{Method, Service, ServiceGenerator};
use quote::quote;

#[derive(Debug, Copy, Clone)]
Expand All @@ -23,7 +23,7 @@ impl ServiceGenerator for GrpcServiceGenerator {
fn finalize(&mut self, buf: &mut String) {
buf.insert_str(
0,
"/// DO NOT MODIFY. Auto-generated file\nuse ntex_grpc::codegen as ngrpc;\n\n",
"#![allow(dead_code)]\n/// DO NOT MODIFY. Auto-generated file\n\n",
)
}
}
Expand Down Expand Up @@ -53,7 +53,7 @@ fn generate_client(service: &Service, buf: &mut String) {
}
}

impl<T> ngrpc::ClientInformation<T> for #service_ident<T> {
impl<T> ::ntex_grpc::ClientInformation<T> for #service_ident<T> {
#[inline]
/// Create new client instance
fn create(transport: T) -> Self {
Expand Down Expand Up @@ -105,17 +105,17 @@ fn gen_method(method: &Method, service: &Service) -> TokenStream {
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct #def_ident;

impl ngrpc::MethodDef for #def_ident {
impl ::ntex_grpc::MethodDef for #def_ident {
const NAME: &'static str = #proto_name;
const PATH: ngrpc::ByteString = ngrpc::ByteString::from_static(#path);
const PATH: ::ntex_grpc::types::ByteString = ::ntex_grpc::types::ByteString::from_static(#path);
type Input = #input_type;
type Output = #output_type;
}

impl<T: ngrpc::Transport<#def_ident>> #service_ident<T> {
impl<T: ::ntex_grpc::Transport<#def_ident>> #service_ident<T> {
#[doc = #(#comments)*]
pub fn #method_ident<'a>(&'a self, req: &'a #input_type) -> ngrpc::Request<'a, T, #def_ident> {
ngrpc::Request::new(&self.0, req)
pub fn #method_ident<'a>(&'a self, req: &'a #input_type) -> ::ntex_grpc::Request<'a, T, #def_ident> {
::ntex_grpc::Request::new(&self.0, req)
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions ntex-grpc-codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ struct Args {
#[clap(short, long, value_parser, name = "INCLUDE-DIR")]
include_dir: Vec<path::PathBuf>,

/// Map protobuf bytes type to custom rust type that implements BytesAdapter trait. {name}={rust-type-name}
#[clap(short, long, value_parser, name = "MAP-BYTES")]
map_bytes: Vec<String>,

/// Path to rustfmt configuration file
#[clap(short, long, value_parser, name = "RUSTFMT-PATH")]
rustfmt_path: Option<path::PathBuf>,
Expand All @@ -43,6 +47,15 @@ fn main() -> io::Result<()> {
if let Some(out_dir) = args.out_dir.clone() {
cfg.out_dir(out_dir);
}

for map in args.map_bytes {
if let Some((s1, s2)) = map.split_once('=') {
cfg.map_bytes(s1, s2)
} else {
println!("Cannot parse bytes mapping: {:?}", map);
}
}

if let Err(e) = cfg.compile_protos(&args.proto, &args.include_dir) {
println!("{}", e);
} else {
Expand Down
2 changes: 2 additions & 0 deletions ntex-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ ntex-bytes = "0.1.15"
ntex-service = "0.3"
ntex-rt = "0.4"

ntex-prost-derive = "0.10.1"

async-trait = "0.1.0"
bitflags = "1.3"
prost = "0.10"
Expand Down
7 changes: 3 additions & 4 deletions ntex-grpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use ntex_http::{header, HeaderMap, Method};
use ntex_io::{IoBoxed, OnDisconnect};
use ntex_service::{fn_service, Service};
use ntex_util::{channel::oneshot, future::Ready, HashMap};
use prost::Message;

use crate::service::{ClientInformation, MethodDef, Transport};
use crate::{consts, ServiceError};
use crate::{consts, Message, ServiceError};

#[derive(thiserror::Error, Debug)]
pub enum ClientError {
Expand Down Expand Up @@ -105,7 +104,7 @@ impl<T: MethodDef> Transport<T> for Client {
let stream = self
.0
.client
.send_request(Method::POST, T::PATH, hdrs)
.send_request(Method::POST, T::PATH, hdrs, false)
.await?;
stream.send_payload(buf.freeze(), true).await?;

Expand All @@ -123,7 +122,7 @@ impl<T: MethodDef> Transport<T> for Client {
Ok(Ok((mut data, trailers))) => {
let _compressed = data.get_u8();
let len = data.get_u32();
match <T::Output as Message>::decode(data.split_to(len as usize)) {
match <T::Output as Message>::decode(&mut data.split_to(len as usize)) {
Ok(item) => Ok((item, trailers)),
Err(_e) => Err(ServiceError::Canceled),
}
Expand Down
Loading

0 comments on commit b95a16b

Please sign in to comment.