From 62f6b28542d46fc040617dc0e54e2bb9f3203d2e Mon Sep 17 00:00:00 2001 From: Millione Date: Fri, 28 Jul 2023 14:54:41 +0800 Subject: [PATCH] feat: keep_unknown_fields support yml config --- Cargo.lock | 28 +++++++++++++-------------- examples/src/unknown/thrift_server.rs | 10 +++++----- examples/volo-gen/build.rs | 6 ------ examples/volo-gen/src/lib.rs | 1 - examples/volo-gen/volo.yml | 3 +++ volo-build/src/config_builder.rs | 21 ++++++++++++++++++-- volo-build/src/lib.rs | 5 ++++- volo-build/src/model.rs | 7 +++++++ volo-build/src/util.rs | 2 ++ volo-cli/src/idl/add.rs | 2 ++ 10 files changed, 56 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 82f7839c..217c78aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -521,9 +521,9 @@ checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" [[package]] name = "faststr" -version = "0.2.3" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfefb0b7147544e93222aa0d5489cfdba2d012603f2f7f0567a67977bb93481f" +checksum = "df6df26864bf2bd44803afbdad5a3b1c32185be12224dd8cb028ecfae59e4bf2" dependencies = [ "bytes", "serde", @@ -1380,7 +1380,7 @@ dependencies = [ [[package]] name = "pilota" version = "0.7.0" -source = "git+https://github.com/cloudwego/pilota?branch=main#75324e8430b2ca615e362df604d3b7e7f86a8c84" +source = "git+https://github.com/cloudwego/pilota?branch=main#62867731ecbf0dab1e6284e29d611a5418f03a24" dependencies = [ "anyhow", "async-recursion", @@ -1401,7 +1401,7 @@ dependencies = [ [[package]] name = "pilota-build" version = "0.7.7" -source = "git+https://github.com/cloudwego/pilota?branch=main#75324e8430b2ca615e362df604d3b7e7f86a8c84" +source = "git+https://github.com/cloudwego/pilota?branch=main#62867731ecbf0dab1e6284e29d611a5418f03a24" dependencies = [ "anyhow", "dashmap", @@ -1433,7 +1433,7 @@ dependencies = [ [[package]] name = "pilota-thrift-parser" version = "0.4.2" -source = "git+https://github.com/cloudwego/pilota?branch=main#75324e8430b2ca615e362df604d3b7e7f86a8c84" +source = "git+https://github.com/cloudwego/pilota?branch=main#62867731ecbf0dab1e6284e29d611a5418f03a24" dependencies = [ "nom", ] @@ -1820,7 +1820,7 @@ checksum = "79ea77c539259495ce8ca47f53e66ae0330a8819f67e23ac96ca02f50e7b7d36" dependencies = [ "log", "ring", - "rustls-webpki 0.101.1", + "rustls-webpki 0.101.2", "sct", ] @@ -1845,9 +1845,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.101.1" +version = "0.101.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15f36a6828982f422756984e47912a7a51dcbc2a197aa791158f8ca61cd8204e" +checksum = "513722fd73ad80a71f72b61009ea1b584bcfa1483ca93949c8f290298837fa59" dependencies = [ "ring", "untrusted", @@ -1939,18 +1939,18 @@ checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" [[package]] name = "serde" -version = "1.0.175" +version = "1.0.177" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d25439cd7397d044e2748a6fe2432b5e85db703d6d097bd014b3c0ad1ebff0b" +checksum = "63ba2516aa6bf82e0b19ca8b50019d52df58455d3cf9bdaf6315225fdd0c560a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.175" +version = "1.0.177" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b23f7ade6f110613c0d63858ddb8b94c1041f550eab58a16b371bdf2c9c80ab4" +checksum = "401797fe7833d72109fedec6bfcbe67c0eed9b99772f26eb8afd261f0abc6fd3" dependencies = [ "proc-macro2", "quote", @@ -1959,9 +1959,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.103" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d03b412469450d4404fe8499a268edd7f8b79fecb074b0d812ad64ca21f4031b" +checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c" dependencies = [ "itoa", "ryu", diff --git a/examples/src/unknown/thrift_server.rs b/examples/src/unknown/thrift_server.rs index d71ad001..c9bb3fe1 100644 --- a/examples/src/unknown/thrift_server.rs +++ b/examples/src/unknown/thrift_server.rs @@ -5,12 +5,12 @@ use std::net::SocketAddr; pub struct S; #[volo::async_trait] -impl volo_gen::volo_gen::echo_unknown::EchoService for S { +impl volo_gen::thrift_gen::echo_unknown::EchoService for S { async fn hello( &self, - req: volo_gen::volo_gen::echo_unknown::EchoRequest, - ) -> Result { - let resp = volo_gen::volo_gen::echo_unknown::EchoResponse { + req: volo_gen::thrift_gen::echo_unknown::EchoRequest, + ) -> Result { + let resp = volo_gen::thrift_gen::echo_unknown::EchoResponse { name: format!("{}", req.name).into(), echo_union: req.echo_union, _unknown_fields: req._unknown_fields, @@ -25,7 +25,7 @@ async fn main() { let addr: SocketAddr = "[::]:8081".parse().unwrap(); let addr = volo::net::Address::from(addr); - volo_gen::volo_gen::echo_unknown::EchoServiceServer::new(S) + volo_gen::thrift_gen::echo_unknown::EchoServiceServer::new(S) .run(addr) .await .unwrap(); diff --git a/examples/volo-gen/build.rs b/examples/volo-gen/build.rs index a84fafd6..4998f9dc 100644 --- a/examples/volo-gen/build.rs +++ b/examples/volo-gen/build.rs @@ -1,9 +1,3 @@ fn main() { volo_build::ConfigBuilder::default().write().unwrap(); - - volo_build::Builder::thrift() - .add_service("../thrift_idl/echo_unknown.thrift") - .keep_unknown_fields(true) - .write() - .unwrap(); } diff --git a/examples/volo-gen/src/lib.rs b/examples/volo-gen/src/lib.rs index a32e6efd..97e9cdd6 100644 --- a/examples/volo-gen/src/lib.rs +++ b/examples/volo-gen/src/lib.rs @@ -3,7 +3,6 @@ mod gen { volo::include_service!("thrift_gen.rs"); volo::include_service!("proto_gen.rs"); - volo::include_service!("volo_gen.rs"); } pub use gen::*; diff --git a/examples/volo-gen/volo.yml b/examples/volo-gen/volo.yml index 6478ee61..d6705cf6 100644 --- a/examples/volo-gen/volo.yml +++ b/examples/volo-gen/volo.yml @@ -23,3 +23,6 @@ entries: path: ../thrift_idl/hello.thrift - source: local path: ../thrift_idl/echo.thrift + - source: local + path: ../thrift_idl/echo_unknown.thrift + keep_unknown_fields: true diff --git a/volo-build/src/config_builder.rs b/volo-build/src/config_builder.rs index 640e6268..489ac046 100644 --- a/volo-build/src/config_builder.rs +++ b/volo-build/src/config_builder.rs @@ -86,6 +86,15 @@ impl InnerBuilder { InnerBuilder::Thrift(inner) => InnerBuilder::Thrift(inner.touch(items)), } } + + pub fn keep_unknown_fields(self, keep: impl IntoIterator) -> Self { + match self { + InnerBuilder::Protobuf(inner) => { + InnerBuilder::Protobuf(inner.keep_unknown_fields(keep)) + } + InnerBuilder::Thrift(inner) => InnerBuilder::Thrift(inner.keep_unknown_fields(keep)), + } + } } impl ConfigBuilder { @@ -123,12 +132,16 @@ impl ConfigBuilder { path, includes, touch, + keep_unknown_fields, } = get_or_download_idl(idl, &*DEFAULT_DIR)?; builder = builder .add_service(path.clone()) .includes(includes) - .touch([(path, touch)]) + .touch([(path.clone(), touch)]); + if keep_unknown_fields { + builder = builder.keep_unknown_fields([path]) + } } builder.write()?; @@ -166,12 +179,16 @@ impl InitBuilder { path, includes, touch, + keep_unknown_fields, } = get_or_download_idl(idl, &*DEFAULT_DIR)?; builder = builder .add_service(path.clone()) .includes(includes) - .touch([(path, touch)]) + .touch([(path.clone(), touch)]); + if keep_unknown_fields { + builder = builder.keep_unknown_fields([path]) + } } builder.init_service() diff --git a/volo-build/src/lib.rs b/volo-build/src/lib.rs index e9e926aa..bfce6fd5 100644 --- a/volo-build/src/lib.rs +++ b/volo-build/src/lib.rs @@ -103,7 +103,10 @@ impl Builder { self } - pub fn keep_unknown_fields(mut self, keep_unknown_fields: bool) -> Self { + pub fn keep_unknown_fields( + mut self, + keep_unknown_fields: impl IntoIterator, + ) -> Self { self.pilota_builder = self.pilota_builder.keep_unknown_fields(keep_unknown_fields); self } diff --git a/volo-build/src/model.rs b/volo-build/src/model.rs index e8b0f4b4..895b8d1f 100644 --- a/volo-build/src/model.rs +++ b/volo-build/src/model.rs @@ -35,6 +35,12 @@ pub struct Idl { pub includes: Option>, #[serde(skip_serializing_if = "Vec::is_empty", default = "Vec::new")] pub touch: Vec, + #[serde(default = "default_keep_unknown_fields")] + pub keep_unknown_fields: bool, +} + +fn default_keep_unknown_fields() -> bool { + false } #[derive(Serialize, Deserialize, Debug, Clone)] @@ -74,6 +80,7 @@ impl Idl { path: PathBuf::from(""), includes: None, touch: Vec::default(), + keep_unknown_fields: false, } } diff --git a/volo-build/src/util.rs b/volo-build/src/util.rs index ab76ec64..5f3b367f 100644 --- a/volo-build/src/util.rs +++ b/volo-build/src/util.rs @@ -67,6 +67,7 @@ pub struct LocalIdl { pub path: PathBuf, pub includes: Vec, pub touch: Vec, + pub keep_unknown_fields: bool, } pub fn get_or_download_idl(idl: Idl, target_dir: impl AsRef) -> anyhow::Result { @@ -108,6 +109,7 @@ pub fn get_or_download_idl(idl: Idl, target_dir: impl AsRef) -> anyhow::Re path, includes, touch: idl.touch, + keep_unknown_fields: idl.keep_unknown_fields, }) } diff --git a/volo-cli/src/idl/add.rs b/volo-cli/src/idl/add.rs index 8b976d2e..9bb0fdc6 100644 --- a/volo-cli/src/idl/add.rs +++ b/volo-cli/src/idl/add.rs @@ -71,6 +71,7 @@ impl CliCommand for Add { touch: vec![], path: self.idl.clone(), includes: self.includes.clone(), + keep_unknown_fields: false, } } else { Idl { @@ -78,6 +79,7 @@ impl CliCommand for Add { touch: vec![], path: self.idl.clone(), includes: self.includes.clone(), + keep_unknown_fields: false, } } };