Skip to content

Commit

Permalink
fix(#515): codegen and default problem (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWhiteWu authored Nov 6, 2024
1 parent 29f80e5 commit 5d85c3d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 52 deletions.
39 changes: 13 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/code-generation/thrift/test.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ struct TestResponse {
}

service TestService {
TestResponse Test (1: TestRequest req),
TestResponse Test (1: TestRequest req, 2: list<TestRequest> reqs),
TestResponse test (1: TestRequest Req),
TestResponse Test2 (1: TestRequest type),
TestResponse Test3 (1: TestRequest self),
}

service testService {
TestResponse Test (1: TestRequest req),
TestResponse Test (1: TestRequest req, 2: list<TestRequest> reqs),
TestResponse test (1: TestRequest Req),
}
2 changes: 1 addition & 1 deletion volo-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "volo-build"
version = "0.10.16"
version = "0.10.17"
edition.workspace = true
homepage.workspace = true
repository.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions volo-build/src/grpc_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl VoloGrpcBackend {
) -> FastStr {
let ty = self.cx().codegen_item_ty(ty.kind);
let ty_str = if global_path {
format!("volo_gen{}", ty.global_path())
format!("{}", ty.global_path("volo_gen"))
} else {
format!("{}", ty)
};
Expand All @@ -54,7 +54,7 @@ impl VoloGrpcBackend {
) -> FastStr {
let ret_ty = self.cx().codegen_item_ty(ty.kind);
let ret_ty_str = if global_path {
format!("volo_gen{}", ret_ty.global_path())
format!("{}", ret_ty.global_path("volo_gen"))
} else {
format!("{}", ret_ty)
};
Expand Down Expand Up @@ -402,7 +402,7 @@ impl CodegenBackend for VoloGrpcBackend {
"",
|enum_variant_names| -> "Self::{enum_variant_names}(s) => {{
::volo_grpc::codec::encode::encode(s, compression_encoding)
}},"
}},"
);

let req_recv_from_body = crate::join_multi_strs!(
Expand Down
25 changes: 6 additions & 19 deletions volo-build/src/thrift_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use pilota_build::{
db::RirDatabase,
rir::{self, Method},
tags::RustWrapperArc,
ty::TyKind,
CodegenBackend, Context, DefId, IdentName, Symbol, ThriftBackend,
};
use quote::format_ident;
Expand Down Expand Up @@ -648,11 +647,8 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
let mut ret_ty = self
.inner
.codegen_item_ty(method.ret.kind.clone())
.global_path()
.global_path("volo_gen")
.to_string();
if need_prepend_volo_gen_path(&method.ret.kind) {
ret_ty = format!("volo_gen{ret_ty}");
}
if let Some(RustWrapperArc(true)) = self
.cx()
.tags(method.ret.tags_id)
Expand All @@ -665,13 +661,12 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
.args
.iter()
.map(|a| {
let ty = self.inner.codegen_item_ty(a.ty.kind.clone()).global_path();
let ty = self
.inner
.codegen_item_ty(a.ty.kind.clone())
.global_path("volo_gen");
let ident = self.cx().rust_name(a.def_id).0.field_ident(); // use the _{rust-style fieldname} without keyword escaping
if need_prepend_volo_gen_path(&a.ty.kind) {
format!("_{ident}: volo_gen{ty}")
} else {
format!("_{ident}: {ty}")
}
format!("_{ident}: {ty}")
})
.join(",");

Expand Down Expand Up @@ -701,14 +696,6 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
}
}

fn need_prepend_volo_gen_path(ty: &TyKind) -> bool {
match ty {
TyKind::Arc(t) => need_prepend_volo_gen_path(&t.kind),
TyKind::Path(_) => true,
_ => false,
}
}

fn rust_name(cx: &Context, def_id: DefId) -> FastStr {
let name = cx.rust_name(def_id);
if cx.names.contains(&def_id) {
Expand Down
2 changes: 1 addition & 1 deletion volo-thrift/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "volo-thrift"
version = "0.10.4"
version = "0.10.5"
edition.workspace = true
homepage.workspace = true
repository.workspace = true
Expand Down
6 changes: 6 additions & 0 deletions volo-thrift/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,9 @@ pub enum MaybeException<T, E> {
Ok(T),
Exception(E),
}

impl<T: Default, E> Default for MaybeException<T, E> {
fn default() -> Self {
MaybeException::Ok(Default::default())
}
}

0 comments on commit 5d85c3d

Please sign in to comment.