From 0d3996026e3f932eeb1923d23901dda070cbada9 Mon Sep 17 00:00:00 2001 From: Yukiteru Date: Fri, 29 Nov 2024 20:59:35 +0800 Subject: [PATCH 1/8] chore(volo-http): put client_ip to cx.ext (#541) Signed-off-by: Yu Li --- Cargo.lock | 2 +- volo-http/Cargo.toml | 2 +- volo-http/src/server/extract.rs | 12 +++++------- volo-http/src/server/utils/client_ip.rs | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e538d067..f8af07ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4187,7 +4187,7 @@ dependencies = [ [[package]] name = "volo-http" -version = "0.3.0" +version = "0.3.1" dependencies = [ "ahash", "async-broadcast", diff --git a/volo-http/Cargo.toml b/volo-http/Cargo.toml index 8e827d37..4bc62e3a 100644 --- a/volo-http/Cargo.toml +++ b/volo-http/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "volo-http" -version = "0.3.0" +version = "0.3.1" edition.workspace = true homepage.workspace = true repository.workspace = true diff --git a/volo-http/src/server/extract.rs b/volo-http/src/server/extract.rs index 4aaafa14..8ab9cce2 100644 --- a/volo-http/src/server/extract.rs +++ b/volo-http/src/server/extract.rs @@ -295,13 +295,11 @@ impl FromContext for ClientIp { type Rejection = Infallible; async fn from_context(cx: &mut ServerContext, _: &mut Parts) -> Result { - Ok(ClientIp( - cx.rpc_info - .caller() - .tags - .get::() - .and_then(|v| v.0), - )) + if let Some(client_ip) = cx.extensions().get::() { + Ok(client_ip.to_owned()) + } else { + Ok(ClientIp(None)) + } } } diff --git a/volo-http/src/server/utils/client_ip.rs b/volo-http/src/server/utils/client_ip.rs index e8a8c14a..7ef58f1e 100644 --- a/volo-http/src/server/utils/client_ip.rs +++ b/volo-http/src/server/utils/client_ip.rs @@ -268,7 +268,7 @@ where req: Request, ) -> Result { let client_ip = self.get_client_ip(cx, req.headers()); - cx.rpc_info_mut().caller_mut().insert(client_ip); + cx.extensions_mut().insert(client_ip); self.service.call(cx, req).await } From 02e0adf5f64562774bd77c443574e3fbda204242 Mon Sep 17 00:00:00 2001 From: "evgeny.bovykin" Date: Fri, 29 Nov 2024 12:59:03 +0100 Subject: [PATCH 2/8] Cleanup: better variable names in thrift_backend.rs --- volo-build/src/thrift_backend.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/volo-build/src/thrift_backend.rs b/volo-build/src/thrift_backend.rs index 100cf627..ef07cff8 100644 --- a/volo-build/src/thrift_backend.rs +++ b/volo-build/src/thrift_backend.rs @@ -376,16 +376,16 @@ impl VoloThriftBackend { } fn write_item(stream: &mut String, base_dir: &Path, name: String, impl_str: String) { - let req_recv_buf = base_dir.join(&name); - let req_recv_file = req_recv_buf.as_path(); - Self::write_file(req_recv_file, impl_str); + let path_buf = base_dir.join(&name); + let path = path_buf.as_path(); + Self::write_file(path, impl_str); stream.push_str(format!("include!(\"{}\");", &name).as_str()); } fn write_file(path: &Path, stream: String) { - let mut req_file_writer = std::io::BufWriter::new(std::fs::File::create(path).unwrap()); - req_file_writer.write_all(stream.as_bytes()).unwrap(); - req_file_writer.flush().unwrap(); + let mut file_writer = std::io::BufWriter::new(std::fs::File::create(path).unwrap()); + file_writer.write_all(stream.as_bytes()).unwrap(); + file_writer.flush().unwrap(); pilota_build::fmt::fmt_file(path); } From c41802b404103cf81d68df396feb0f605c476b19 Mon Sep 17 00:00:00 2001 From: "evgeny.bovykin" Date: Fri, 29 Nov 2024 13:26:36 +0100 Subject: [PATCH 3/8] Split grpc generated files into several --- .../.gitignore | 2 + .../Cargo.toml | 38 ++++++ .../proto/echo.proto | 15 +++ .../src/bin/gen.rs | 7 + .../volo.workspace.yml | 21 +++ volo-build/src/grpc_backend.rs | 127 ++++++++++++++++-- 6 files changed, 202 insertions(+), 8 deletions(-) create mode 100644 tests/code-generation-workspace-split-grpc/.gitignore create mode 100644 tests/code-generation-workspace-split-grpc/Cargo.toml create mode 100644 tests/code-generation-workspace-split-grpc/proto/echo.proto create mode 100644 tests/code-generation-workspace-split-grpc/src/bin/gen.rs create mode 100644 tests/code-generation-workspace-split-grpc/volo.workspace.yml diff --git a/tests/code-generation-workspace-split-grpc/.gitignore b/tests/code-generation-workspace-split-grpc/.gitignore new file mode 100644 index 00000000..f3d1eed0 --- /dev/null +++ b/tests/code-generation-workspace-split-grpc/.gitignore @@ -0,0 +1,2 @@ +rpc_echo +Cargo.lock diff --git a/tests/code-generation-workspace-split-grpc/Cargo.toml b/tests/code-generation-workspace-split-grpc/Cargo.toml new file mode 100644 index 00000000..5afb15c8 --- /dev/null +++ b/tests/code-generation-workspace-split-grpc/Cargo.toml @@ -0,0 +1,38 @@ +[[bin]] +bench = false +name = "gen" +test = false + +[dependencies.pilota-build] +version = "*" + +[dependencies.volo-build] +workspace = true + +[package] +edition = "2021" +name = "code-generation-workspace-split" +publish = false +version = "0.0.0" + +[workspace] +members = [] + +[workspace.dependencies] +anyhow = "1" +async-trait = "0.1" +lazy_static = "1" +serde = "1" +volo-grpc = "*" + +[workspace.dependencies.pilota] +version = "*" + +[workspace.dependencies.volo] +path = "../../volo" + +[workspace.dependencies.volo-build] +path = "../../volo-build" + +[workspace.dependencies.volo-thrift] +path = "../../volo-thrift" diff --git a/tests/code-generation-workspace-split-grpc/proto/echo.proto b/tests/code-generation-workspace-split-grpc/proto/echo.proto new file mode 100644 index 00000000..3793bbec --- /dev/null +++ b/tests/code-generation-workspace-split-grpc/proto/echo.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package echo; + +message EchoRequest { + string message = 1; +} + +message EchoResponse { + string message = 1; +} + +service Echo { + rpc Echo(EchoRequest) returns (EchoResponse) {} +} \ No newline at end of file diff --git a/tests/code-generation-workspace-split-grpc/src/bin/gen.rs b/tests/code-generation-workspace-split-grpc/src/bin/gen.rs new file mode 100644 index 00000000..dc3498f5 --- /dev/null +++ b/tests/code-generation-workspace-split-grpc/src/bin/gen.rs @@ -0,0 +1,7 @@ +use volo_build::plugin::SerdePlugin; + +fn main() { + volo_build::workspace::Builder::protobuf() + .plugin(SerdePlugin) + .gen() +} diff --git a/tests/code-generation-workspace-split-grpc/volo.workspace.yml b/tests/code-generation-workspace-split-grpc/volo.workspace.yml new file mode 100644 index 00000000..8fc06d7a --- /dev/null +++ b/tests/code-generation-workspace-split-grpc/volo.workspace.yml @@ -0,0 +1,21 @@ +common_crate_name: "common" # common_crate_name = "common" by default +touch_all: true +dedups: [] # remove the repeated structure generation in one entry +special_namings: [] +split_generated_files: true +# repos: # exsit if non-local +# { repo }: # repo = extract the name from url by default +# url: { git } # url = repo git url +# ref: { ref } # ref = "HEAD" by default +# lock: { commit hash } # lock is the last commit hash +services: + - idl: + source: local + path: proto/echo.proto + includes: + - proto + codegen_option: + touch: [] + keep_unknown_fields: false # A->B->C, B could use this to transfer the unknown fields which is needed by A and C. + config: + crate_name: rpc_echo \ No newline at end of file diff --git a/volo-build/src/grpc_backend.rs b/volo-build/src/grpc_backend.rs index 55372bd2..6f08caa4 100644 --- a/volo-build/src/grpc_backend.rs +++ b/volo-build/src/grpc_backend.rs @@ -1,3 +1,5 @@ +use std::io::Write; +use std::path::Path; use itertools::Itertools; use pilota_build::{ db::RirDatabase, @@ -6,6 +8,7 @@ use pilota_build::{ tags::protobuf::{ClientStreaming, ServerStreaming}, CodegenBackend, Context, DefId, IdentName, Symbol, }; +use pilota_build::middle::context::Mode; use volo::FastStr; pub struct MkGrpcBackend; @@ -231,6 +234,20 @@ impl VoloGrpcBackend { .into() } } + + fn write_item(stream: &mut String, base_dir: &Path, name: String, impl_str: String) { + let path_buf = base_dir.join(&name); + let path = path_buf.as_path(); + Self::write_file(path, impl_str); + stream.push_str(format!("include!(\"{}\");", &name).as_str()); + } + + fn write_file(path: &Path, stream: String) { + let mut file_writer = std::io::BufWriter::new(std::fs::File::create(path).unwrap()); + file_writer.write_all(stream.as_bytes()).unwrap(); + file_writer.flush().unwrap(); + pilota_build::fmt::fmt_file(path); + } } impl CodegenBackend for VoloGrpcBackend { @@ -254,6 +271,51 @@ impl CodegenBackend for VoloGrpcBackend { let resp_enum_name_send = format!("{}ResponseSend", service_name); let req_enum_name_recv = format!("{}RequestRecv", service_name); let resp_enum_name_recv = format!("{}ResponseRecv", service_name); + + let path = self.cx().item_path(def_id); + let path = path.as_ref(); + + // Locate directory based on the full item path + let base_dir = match self.cx().mode.as_ref() { + // In a workspace mode, the base directory is next to the `.rs` file for the service + Mode::Workspace(info) => { + let mut dir = info.dir.clone(); + if path.is_empty() { + dir + } else { + dir.push(path[0].0.as_str()); + if path.len() > 1 { + dir.push("src"); + for segment in path.iter().skip(1) { + dir.push(Path::new(segment.0.as_str())); + } + } + dir + } + } + // In single file mode, the files directory is the root + // The base directory path is the root + the item path + Mode::SingleFile { file_path } => { + let mut dir = file_path.clone(); + dir.pop(); + for segment in path { + dir.push(Path::new(segment.0.as_str())); + } + dir + } + }; + + let base_dir = if let Some(suffix) = self.cx().names.get(&def_id) { + format!("{}_{suffix}", base_dir.display()) + } else { + base_dir.display().to_string() + }; + let base_dir = Path::new(&base_dir); + + if self.cx().split { + std::fs::create_dir_all(base_dir).expect("Failed to create base directory"); + } + let paths = s .methods .iter() @@ -426,8 +488,9 @@ impl CodegenBackend for VoloGrpcBackend { }}" ); - stream.push_str(&format! { - r#"pub enum {req_enum_name_send} {{ + let req_enum_send_impl = format! { + r#" + pub enum {req_enum_name_send} {{ {req_enum_send_variants} }} @@ -437,8 +500,11 @@ impl CodegenBackend for VoloGrpcBackend { {req_send_into_body} }} }} - }} + }}"# + }; + let req_enum_recv_impl = format! { + r#" pub enum {req_enum_name_recv} {{ {req_enum_recv_variants} }} @@ -450,8 +516,11 @@ impl CodegenBackend for VoloGrpcBackend { _ => ::std::result::Result::Err(::volo_grpc::Status::new(::volo_grpc::Code::Unimplemented, "Method not found.")), }} }} - }} + }}"# + }; + let resp_enum_send_impl = format! { + r#" pub enum {resp_enum_name_send} {{ {resp_enum_send_variants} }} @@ -462,8 +531,11 @@ impl CodegenBackend for VoloGrpcBackend { {resp_send_into_body} }} }} - }} + }}"# + }; + let resp_enum_recv_impl = format! { + r#" pub enum {resp_enum_name_recv} {{ {resp_enum_recv_variants} }} @@ -478,8 +550,11 @@ impl CodegenBackend for VoloGrpcBackend { _ => ::std::result::Result::Err(::volo_grpc::Status::new(::volo_grpc::Code::Unimplemented, "Method not found.")), }} }} - }} + }}"# + }; + let client_impl = format! { + r#" pub struct {client_builder_name} {{}} impl {client_builder_name} {{ pub fn new( @@ -522,8 +597,11 @@ impl CodegenBackend for VoloGrpcBackend { impl, Response=::volo_grpc::Response<{resp_enum_name_recv}>, Error = ::volo_grpc::Status> + Send + Sync + 'static> {oneshot_client_name} {{ {oneshot_client_methods} - }} + }}"# + }; + let server_impl = format! { + r#" pub struct {server_name} {{ inner: ::std::sync::Arc, }} @@ -570,9 +648,42 @@ impl CodegenBackend for VoloGrpcBackend { impl ::volo_grpc::server::NamedService for {server_name} {{ const NAME: &'static str = "{name}"; }}"# - }); + }; + + if self.cx().split { + let mut mod_rs_stream = String::new(); + Self::write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", req_enum_name_send), req_enum_send_impl); + Self::write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", req_enum_name_recv), req_enum_recv_impl); + Self::write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", resp_enum_name_send), resp_enum_send_impl); + Self::write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", resp_enum_name_recv), resp_enum_recv_impl); + + Self::write_item(&mut mod_rs_stream, base_dir, format!("client_{}.rs", client_name), client_impl); + Self::write_item(&mut mod_rs_stream, base_dir, format!("server_{}.rs", server_name), server_impl); + + let mod_rs_file_path = base_dir.join("mod.rs"); + Self::write_file(&mod_rs_file_path, mod_rs_stream); + stream.push_str( + format!( + "include!(\"{}/mod.rs\");", + base_dir.file_name().unwrap().to_str().unwrap() + ) + .as_str(), + ); + } else { + stream.push_str(&format! { + r#" + {req_enum_send_impl} + {req_enum_recv_impl} + {resp_enum_send_impl} + {resp_enum_recv_impl} + + {client_impl} + {server_impl} + "#}); + } } + fn codegen_service_method(&self, _service_def_id: DefId, method: &rir::Method) -> String { let client_streaming = self .cx() From c2d73b6b41a72c0ca4a5d9c345688c1604a0607e Mon Sep 17 00:00:00 2001 From: "evgeny.bovykin" Date: Fri, 29 Nov 2024 13:43:10 +0100 Subject: [PATCH 4/8] Cleanup: move common functions from grpc_backend.rs and thrift_backend.rs to util.rs --- volo-build/src/grpc_backend.rs | 68 +++++--------------------------- volo-build/src/thrift_backend.rs | 68 +++++--------------------------- volo-build/src/util.rs | 56 ++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 116 deletions(-) diff --git a/volo-build/src/grpc_backend.rs b/volo-build/src/grpc_backend.rs index 6f08caa4..fe33fac5 100644 --- a/volo-build/src/grpc_backend.rs +++ b/volo-build/src/grpc_backend.rs @@ -10,6 +10,7 @@ use pilota_build::{ }; use pilota_build::middle::context::Mode; use volo::FastStr; +use crate::util::{get_base_dir, write_file, write_item}; pub struct MkGrpcBackend; @@ -234,20 +235,6 @@ impl VoloGrpcBackend { .into() } } - - fn write_item(stream: &mut String, base_dir: &Path, name: String, impl_str: String) { - let path_buf = base_dir.join(&name); - let path = path_buf.as_path(); - Self::write_file(path, impl_str); - stream.push_str(format!("include!(\"{}\");", &name).as_str()); - } - - fn write_file(path: &Path, stream: String) { - let mut file_writer = std::io::BufWriter::new(std::fs::File::create(path).unwrap()); - file_writer.write_all(stream.as_bytes()).unwrap(); - file_writer.flush().unwrap(); - pilota_build::fmt::fmt_file(path); - } } impl CodegenBackend for VoloGrpcBackend { @@ -274,43 +261,8 @@ impl CodegenBackend for VoloGrpcBackend { let path = self.cx().item_path(def_id); let path = path.as_ref(); - - // Locate directory based on the full item path - let base_dir = match self.cx().mode.as_ref() { - // In a workspace mode, the base directory is next to the `.rs` file for the service - Mode::Workspace(info) => { - let mut dir = info.dir.clone(); - if path.is_empty() { - dir - } else { - dir.push(path[0].0.as_str()); - if path.len() > 1 { - dir.push("src"); - for segment in path.iter().skip(1) { - dir.push(Path::new(segment.0.as_str())); - } - } - dir - } - } - // In single file mode, the files directory is the root - // The base directory path is the root + the item path - Mode::SingleFile { file_path } => { - let mut dir = file_path.clone(); - dir.pop(); - for segment in path { - dir.push(Path::new(segment.0.as_str())); - } - dir - } - }; - - let base_dir = if let Some(suffix) = self.cx().names.get(&def_id) { - format!("{}_{suffix}", base_dir.display()) - } else { - base_dir.display().to_string() - }; - let base_dir = Path::new(&base_dir); + let buf = get_base_dir(self.cx().mode.as_ref(), self.cx().names.get(&def_id), path); + let base_dir = buf.as_path(); if self.cx().split { std::fs::create_dir_all(base_dir).expect("Failed to create base directory"); @@ -652,16 +604,16 @@ impl CodegenBackend for VoloGrpcBackend { if self.cx().split { let mut mod_rs_stream = String::new(); - Self::write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", req_enum_name_send), req_enum_send_impl); - Self::write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", req_enum_name_recv), req_enum_recv_impl); - Self::write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", resp_enum_name_send), resp_enum_send_impl); - Self::write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", resp_enum_name_recv), resp_enum_recv_impl); + write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", req_enum_name_send), req_enum_send_impl); + write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", req_enum_name_recv), req_enum_recv_impl); + write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", resp_enum_name_send), resp_enum_send_impl); + write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", resp_enum_name_recv), resp_enum_recv_impl); - Self::write_item(&mut mod_rs_stream, base_dir, format!("client_{}.rs", client_name), client_impl); - Self::write_item(&mut mod_rs_stream, base_dir, format!("server_{}.rs", server_name), server_impl); + write_item(&mut mod_rs_stream, base_dir, format!("client_{}.rs", client_name), client_impl); + write_item(&mut mod_rs_stream, base_dir, format!("server_{}.rs", server_name), server_impl); let mod_rs_file_path = base_dir.join("mod.rs"); - Self::write_file(&mod_rs_file_path, mod_rs_stream); + write_file(&mod_rs_file_path, mod_rs_stream); stream.push_str( format!( "include!(\"{}/mod.rs\");", diff --git a/volo-build/src/thrift_backend.rs b/volo-build/src/thrift_backend.rs index ef07cff8..6c509f77 100644 --- a/volo-build/src/thrift_backend.rs +++ b/volo-build/src/thrift_backend.rs @@ -11,6 +11,7 @@ use pilota_build::{ }; use quote::format_ident; use volo::FastStr; +use crate::util::{get_base_dir, write_file, write_item}; #[derive(Clone)] pub struct VoloThriftBackend { @@ -320,25 +321,25 @@ impl VoloThriftBackend { "# }; - Self::write_item( + write_item( stream, base_dir, format!("enum_{}.rs", &req_recv_name), req_recv_stream, ); - Self::write_item( + write_item( stream, base_dir, format!("enum_{}.rs", &res_recv_name), res_recv_stream, ); - Self::write_item( + write_item( stream, base_dir, format!("enum_{}.rs", &req_send_name), req_send_stream, ); - Self::write_item( + write_item( stream, base_dir, format!("enum_{}.rs", &res_send_name), @@ -375,20 +376,6 @@ impl VoloThriftBackend { } } - fn write_item(stream: &mut String, base_dir: &Path, name: String, impl_str: String) { - let path_buf = base_dir.join(&name); - let path = path_buf.as_path(); - Self::write_file(path, impl_str); - stream.push_str(format!("include!(\"{}\");", &name).as_str()); - } - - fn write_file(path: &Path, stream: String) { - let mut file_writer = std::io::BufWriter::new(std::fs::File::create(path).unwrap()); - file_writer.write_all(stream.as_bytes()).unwrap(); - file_writer.flush().unwrap(); - pilota_build::fmt::fmt_file(path); - } - fn method_ty_path(&self, service_name: &Symbol, method: &Method, suffix: &str) -> FastStr { match method.source { rir::MethodSource::Extend(def_id) => { @@ -469,43 +456,8 @@ impl pilota_build::CodegenBackend for VoloThriftBackend { let path = self.cx().item_path(def_id); let path = path.as_ref(); - - // Locate directory based on the full item path - let base_dir = match self.cx().mode.as_ref() { - // In a workspace mode, the base directory is next to the `.rs` file for the service - Mode::Workspace(info) => { - let mut dir = info.dir.clone(); - if path.is_empty() { - dir - } else { - dir.push(path[0].0.as_str()); - if path.len() > 1 { - dir.push("src"); - for segment in path.iter().skip(1) { - dir.push(Path::new(segment.0.as_str())); - } - } - dir - } - } - // In single file mode, the files directory is the root - // The base directory path is the root + the item path - Mode::SingleFile { file_path } => { - let mut dir = file_path.clone(); - dir.pop(); - for segment in path { - dir.push(Path::new(segment.0.as_str())); - } - dir - } - }; - - let base_dir = if let Some(suffix) = self.cx().names.get(&def_id) { - format!("{}_{suffix}", base_dir.display()) - } else { - base_dir.display().to_string() - }; - let base_dir = Path::new(&base_dir); + let buf = get_base_dir(self.cx().mode.as_ref(), self.cx().names.get(&def_id), path); + let base_dir = buf.as_path(); if self.cx().split { std::fs::create_dir_all(base_dir).expect("Failed to create base directory"); @@ -747,13 +699,13 @@ impl pilota_build::CodegenBackend for VoloThriftBackend { }; if self.cx().split { - Self::write_item( + write_item( &mut mod_rs_stream, base_dir, format!("service_{}Server.rs", service_name), server_string, ); - Self::write_item( + write_item( &mut mod_rs_stream, base_dir, format!("service_{}Client.rs", service_name), @@ -772,7 +724,7 @@ impl pilota_build::CodegenBackend for VoloThriftBackend { if self.cx().split { let mod_rs_file_path = base_dir.join("mod.rs"); - Self::write_file(&mod_rs_file_path, mod_rs_stream); + write_file(&mod_rs_file_path, mod_rs_stream); stream.push_str( format!( "include!(\"{}/mod.rs\");", diff --git a/volo-build/src/util.rs b/volo-build/src/util.rs index 12cd396d..cfbde9e9 100644 --- a/volo-build/src/util.rs +++ b/volo-build/src/util.rs @@ -9,6 +9,8 @@ use std::{ use anyhow::{bail, Context}; use mockall_double::double; +use pilota_build::middle::context::Mode; +use pilota_build::Symbol; use serde::de::Error; use volo::FastStr; @@ -857,3 +859,57 @@ mod tests { ); } } + +pub(crate) fn write_item(stream: &mut String, base_dir: &Path, name: String, impl_str: String) { + let path_buf = base_dir.join(&name); + let path = path_buf.as_path(); + write_file(path, impl_str); + stream.push_str(format!("include!(\"{}\");", &name).as_str()); +} + +pub(crate) fn write_file(path: &Path, stream: String) { + let mut file_writer = std::io::BufWriter::new(std::fs::File::create(path).unwrap()); + file_writer.write_all(stream.as_bytes()).unwrap(); + file_writer.flush().unwrap(); + pilota_build::fmt::fmt_file(path); +} + +pub(crate) fn get_base_dir(mode: &Mode, def_id: Option<&usize>, path: &[Symbol]) -> PathBuf { + // Locate directory based on the full item path + let base_dir = match mode { + // In a workspace mode, the base directory is next to the `.rs` file for the service + Mode::Workspace(info) => { + let mut dir = info.dir.clone(); + if path.is_empty() { + dir + } else { + dir.push(path[0].0.as_str()); + if path.len() > 1 { + dir.push("src"); + for segment in path.iter().skip(1) { + dir.push(Path::new(segment.0.as_str())); + } + } + dir + } + } + // In single file mode, the files directory is the root + // The base directory path is the root + the item path + Mode::SingleFile { file_path } => { + let mut dir = file_path.clone(); + dir.pop(); + for segment in path { + dir.push(Path::new(segment.0.as_str())); + } + dir + } + }; + + let base_dir = if let Some(suffix) = def_id { + format!("{}_{suffix}", base_dir.display()) + } else { + base_dir.display().to_string() + }; + let base_dir = Path::new(&base_dir); + base_dir.to_path_buf() +} \ No newline at end of file From 5e2942f18798f19fd361815df3da483ebd7bbe6d Mon Sep 17 00:00:00 2001 From: "evgeny.bovykin" Date: Fri, 29 Nov 2024 13:43:23 +0100 Subject: [PATCH 5/8] Cleanup: cargo fmt --- volo-build/src/grpc_backend.rs | 55 ++++++++++++++++++++++++-------- volo-build/src/thrift_backend.rs | 2 +- volo-build/src/util.rs | 2 +- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/volo-build/src/grpc_backend.rs b/volo-build/src/grpc_backend.rs index fe33fac5..9bfbeba1 100644 --- a/volo-build/src/grpc_backend.rs +++ b/volo-build/src/grpc_backend.rs @@ -1,6 +1,6 @@ -use std::io::Write; -use std::path::Path; +use crate::util::{get_base_dir, write_file, write_item}; use itertools::Itertools; +use pilota_build::middle::context::Mode; use pilota_build::{ db::RirDatabase, rir, @@ -8,9 +8,9 @@ use pilota_build::{ tags::protobuf::{ClientStreaming, ServerStreaming}, CodegenBackend, Context, DefId, IdentName, Symbol, }; -use pilota_build::middle::context::Mode; +use std::io::Write; +use std::path::Path; use volo::FastStr; -use crate::util::{get_base_dir, write_file, write_item}; pub struct MkGrpcBackend; @@ -440,7 +440,7 @@ impl CodegenBackend for VoloGrpcBackend { }}" ); - let req_enum_send_impl = format! { + let req_enum_send_impl = format! { r#" pub enum {req_enum_name_send} {{ {req_enum_send_variants} @@ -604,13 +604,43 @@ impl CodegenBackend for VoloGrpcBackend { if self.cx().split { let mut mod_rs_stream = String::new(); - write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", req_enum_name_send), req_enum_send_impl); - write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", req_enum_name_recv), req_enum_recv_impl); - write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", resp_enum_name_send), resp_enum_send_impl); - write_item(&mut mod_rs_stream, base_dir, format!("enum_{}.rs", resp_enum_name_recv), resp_enum_recv_impl); + write_item( + &mut mod_rs_stream, + base_dir, + format!("enum_{}.rs", req_enum_name_send), + req_enum_send_impl, + ); + write_item( + &mut mod_rs_stream, + base_dir, + format!("enum_{}.rs", req_enum_name_recv), + req_enum_recv_impl, + ); + write_item( + &mut mod_rs_stream, + base_dir, + format!("enum_{}.rs", resp_enum_name_send), + resp_enum_send_impl, + ); + write_item( + &mut mod_rs_stream, + base_dir, + format!("enum_{}.rs", resp_enum_name_recv), + resp_enum_recv_impl, + ); - write_item(&mut mod_rs_stream, base_dir, format!("client_{}.rs", client_name), client_impl); - write_item(&mut mod_rs_stream, base_dir, format!("server_{}.rs", server_name), server_impl); + write_item( + &mut mod_rs_stream, + base_dir, + format!("client_{}.rs", client_name), + client_impl, + ); + write_item( + &mut mod_rs_stream, + base_dir, + format!("server_{}.rs", server_name), + server_impl, + ); let mod_rs_file_path = base_dir.join("mod.rs"); write_file(&mod_rs_file_path, mod_rs_stream); @@ -619,7 +649,7 @@ impl CodegenBackend for VoloGrpcBackend { "include!(\"{}/mod.rs\");", base_dir.file_name().unwrap().to_str().unwrap() ) - .as_str(), + .as_str(), ); } else { stream.push_str(&format! { @@ -635,7 +665,6 @@ impl CodegenBackend for VoloGrpcBackend { } } - fn codegen_service_method(&self, _service_def_id: DefId, method: &rir::Method) -> String { let client_streaming = self .cx() diff --git a/volo-build/src/thrift_backend.rs b/volo-build/src/thrift_backend.rs index 6c509f77..db87b015 100644 --- a/volo-build/src/thrift_backend.rs +++ b/volo-build/src/thrift_backend.rs @@ -1,5 +1,6 @@ use std::{io::Write, path::Path}; +use crate::util::{get_base_dir, write_file, write_item}; use itertools::Itertools; use pilota_build::{ codegen::thrift::DecodeHelper, @@ -11,7 +12,6 @@ use pilota_build::{ }; use quote::format_ident; use volo::FastStr; -use crate::util::{get_base_dir, write_file, write_item}; #[derive(Clone)] pub struct VoloThriftBackend { diff --git a/volo-build/src/util.rs b/volo-build/src/util.rs index cfbde9e9..b2580044 100644 --- a/volo-build/src/util.rs +++ b/volo-build/src/util.rs @@ -912,4 +912,4 @@ pub(crate) fn get_base_dir(mode: &Mode, def_id: Option<&usize>, path: &[Symbol]) }; let base_dir = Path::new(&base_dir); base_dir.to_path_buf() -} \ No newline at end of file +} From 14c2442aea79208ea3b4c37c510a300b1eb7ec43 Mon Sep 17 00:00:00 2001 From: "evgeny.bovykin" Date: Mon, 2 Dec 2024 10:20:52 +0100 Subject: [PATCH 6/8] Cleanup: remove unused imports --- volo-build/src/grpc_backend.rs | 3 --- volo-build/src/thrift_backend.rs | 3 +-- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/volo-build/src/grpc_backend.rs b/volo-build/src/grpc_backend.rs index 9bfbeba1..23eb9a3f 100644 --- a/volo-build/src/grpc_backend.rs +++ b/volo-build/src/grpc_backend.rs @@ -1,6 +1,5 @@ use crate::util::{get_base_dir, write_file, write_item}; use itertools::Itertools; -use pilota_build::middle::context::Mode; use pilota_build::{ db::RirDatabase, rir, @@ -8,8 +7,6 @@ use pilota_build::{ tags::protobuf::{ClientStreaming, ServerStreaming}, CodegenBackend, Context, DefId, IdentName, Symbol, }; -use std::io::Write; -use std::path::Path; use volo::FastStr; pub struct MkGrpcBackend; diff --git a/volo-build/src/thrift_backend.rs b/volo-build/src/thrift_backend.rs index db87b015..e85dc4ad 100644 --- a/volo-build/src/thrift_backend.rs +++ b/volo-build/src/thrift_backend.rs @@ -1,11 +1,10 @@ -use std::{io::Write, path::Path}; +use std::path::Path; use crate::util::{get_base_dir, write_file, write_item}; use itertools::Itertools; use pilota_build::{ codegen::thrift::DecodeHelper, db::RirDatabase, - middle::context::Mode, rir::{self, Method}, tags::RustWrapperArc, CodegenBackend, Context, DefId, IdentName, Symbol, ThriftBackend, From 6165e7a69feaf00c756f7d417c3ce0bc4c4c5cd6 Mon Sep 17 00:00:00 2001 From: "evgeny.bovykin" Date: Mon, 2 Dec 2024 13:31:38 +0100 Subject: [PATCH 7/8] Cleanup: imports order --- volo-build/src/grpc_backend.rs | 3 ++- volo-build/src/thrift_backend.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/volo-build/src/grpc_backend.rs b/volo-build/src/grpc_backend.rs index 23eb9a3f..d2cac78b 100644 --- a/volo-build/src/grpc_backend.rs +++ b/volo-build/src/grpc_backend.rs @@ -1,4 +1,3 @@ -use crate::util::{get_base_dir, write_file, write_item}; use itertools::Itertools; use pilota_build::{ db::RirDatabase, @@ -9,6 +8,8 @@ use pilota_build::{ }; use volo::FastStr; +use crate::util::{get_base_dir, write_file, write_item}; + pub struct MkGrpcBackend; impl pilota_build::MakeBackend for MkGrpcBackend { diff --git a/volo-build/src/thrift_backend.rs b/volo-build/src/thrift_backend.rs index e85dc4ad..73eff15f 100644 --- a/volo-build/src/thrift_backend.rs +++ b/volo-build/src/thrift_backend.rs @@ -1,6 +1,5 @@ use std::path::Path; -use crate::util::{get_base_dir, write_file, write_item}; use itertools::Itertools; use pilota_build::{ codegen::thrift::DecodeHelper, @@ -12,6 +11,8 @@ use pilota_build::{ use quote::format_ident; use volo::FastStr; +use crate::util::{get_base_dir, write_file, write_item}; + #[derive(Clone)] pub struct VoloThriftBackend { inner: ThriftBackend, From 3c3f7db91207d7db11aa7c45ed4da39d60cd8278 Mon Sep 17 00:00:00 2001 From: "evgeny.bovykin" Date: Mon, 2 Dec 2024 14:13:56 +0100 Subject: [PATCH 8/8] Cleanup: imports order v2 --- volo-build/src/util.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/volo-build/src/util.rs b/volo-build/src/util.rs index b2580044..60488295 100644 --- a/volo-build/src/util.rs +++ b/volo-build/src/util.rs @@ -9,8 +9,7 @@ use std::{ use anyhow::{bail, Context}; use mockall_double::double; -use pilota_build::middle::context::Mode; -use pilota_build::Symbol; +use pilota_build::{middle::context::Mode, Symbol}; use serde::de::Error; use volo::FastStr;