Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(volo-build): escape keywords in code generation for idl service method arguments #496

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
265 changes: 122 additions & 143 deletions Cargo.lock

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions examples/src/thrift/hello/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,35 @@ async fn main() {
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("{e:?}"),
}

let req = volo_gen::thrift_gen::hello::HelloRequest {
name: "volo".into(),
common: None,
common2: None,
};

let resp = CLIENT
.clone()
.with_callopt(CallOpt::default())
.hello2(req)
.await;
match resp {
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("{e:?}"),
}

let req = volo_gen::thrift_gen::hello::HelloRequest {
name: "volo".into(),
common: None,
common2: None,
};
let resp = CLIENT
.clone()
.with_callopt(CallOpt::default())
.hello3(req)
.await;
match resp {
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("{e:?}"),
}
}
20 changes: 20 additions & 0 deletions examples/src/thrift/hello/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ impl volo_gen::thrift_gen::hello::HelloService for S {
};
Ok(resp)
}

async fn hello2(
&self,
r#type: volo_gen::thrift_gen::hello::HelloRequest,
) -> Result<volo_gen::thrift_gen::hello::HelloResponse, volo_thrift::ServerError> {
let resp = volo_gen::thrift_gen::hello::HelloResponse {
message: format!("Hello2, {}!", r#type.name).into(),
};
Ok(resp)
}

async fn hello3(
&self,
self_: volo_gen::thrift_gen::hello::HelloRequest,
) -> Result<volo_gen::thrift_gen::hello::HelloResponse, volo_thrift::ServerError> {
let resp = volo_gen::thrift_gen::hello::HelloResponse {
message: format!("Hello3, {}!", self_.name).into(),
};
Ok(resp)
}
}

#[volo::main]
Expand Down
15 changes: 15 additions & 0 deletions examples/src/thrift/hello/server_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ impl volo_gen::thrift_gen::hello::HelloService for S {
) -> Result<volo_gen::thrift_gen::hello::HelloResponse, volo_thrift::ServerError> {
panic!("panic in hello");
}

async fn hello2(
&self,
_type: volo_gen::thrift_gen::hello::HelloRequest,
) -> Result<volo_gen::thrift_gen::hello::HelloResponse, volo_thrift::ServerError> {
panic!("panic in hello");
}

#[allow(clippy::duplicate_underscore_argument)]
async fn hello3(
&self,
_self: volo_gen::thrift_gen::hello::HelloRequest,
) -> Result<volo_gen::thrift_gen::hello::HelloResponse, volo_thrift::ServerError> {
panic!("panic in hello");
}
}

#[volo::main]
Expand Down
14 changes: 14 additions & 0 deletions examples/src/thrift/multiplex/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ impl volo_gen::thrift_gen::hello::HelloService for S {
};
Ok(resp)
}

async fn hello2(
&self,
_req: volo_gen::thrift_gen::hello::HelloRequest,
) -> Result<volo_gen::thrift_gen::hello::HelloResponse, volo_thrift::ServerError> {
panic!("panic in hello");
}

async fn hello3(
&self,
_req: volo_gen::thrift_gen::hello::HelloRequest,
) -> Result<volo_gen::thrift_gen::hello::HelloResponse, volo_thrift::ServerError> {
panic!("panic in hello");
}
}

#[volo::main]
Expand Down
2 changes: 2 additions & 0 deletions examples/thrift/hello.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ struct HelloResponse {

service HelloService {
HelloResponse Hello (1: HelloRequest req),
HelloResponse Hello2 (1: HelloRequest type),
HelloResponse Hello3 (1: HelloRequest self),
}
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.14"
version = "0.10.15"
edition.workspace = true
homepage.workspace = true
repository.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions volo-build/src/thrift_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
let name = self.cx().rust_name(m.def_id);
let resp_type = self.cx().codegen_item_ty(m.ret.kind.clone());
let req_fields = m.args.iter().map(|a| {
let name = self.cx().rust_name(a.def_id).0.field_ident();
let name = self.cx().rust_name(a.def_id); // use the rust name as string format which will escape the keyword
let ty = self.cx().codegen_item_ty(a.ty.kind.clone());
let mut ty = format!("{ty}");
if let Some(RustWrapperArc(true)) = self.cx().tags(a.tags_id).as_ref().and_then(|tags| tags.get::<RustWrapperArc>()) {
Expand All @@ -393,7 +393,7 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
} else {
"None => unreachable!()"
};
let req_field_names = m.args.iter().map(|a| self.cx().rust_name(a.def_id).0.field_ident()).join(",");
let req_field_names = m.args.iter().map(|a| self.cx().rust_name(a.def_id)).join(","); // use the rust name as string format which will escape the keyword
let anonymous_args_send_name = self.method_args_path(&service_name, m, true);
let exception = if let Some(p) = &m.exceptions {
self.cx().cur_related_item_path(p.did)
Expand Down Expand Up @@ -477,7 +477,7 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
let args = m
.args
.iter()
.map(|a| format!("args.{}", self.cx().rust_name(a.def_id).0.field_ident()))
.map(|a| format!("args.{}", self.cx().rust_name(a.def_id))) // use the rust name as string format which will escape the keyword
.join(",");

let has_exception = m.exceptions.is_some();
Expand Down Expand Up @@ -623,7 +623,7 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
.iter()
.map(|a| {
let ty = self.inner.codegen_item_ty(a.ty.kind.clone());
let ident = self.cx().rust_name(a.def_id).0.field_ident();
let ident = self.cx().rust_name(a.def_id); // use the rust name as string format which will escape the keyword
format!("{ident}: {ty}")
})
.join(",");
Expand Down Expand Up @@ -666,7 +666,7 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
.iter()
.map(|a| {
let ty = self.inner.codegen_item_ty(a.ty.kind.clone()).global_path();
let ident = self.cx().rust_name(a.def_id);
let ident = self.cx().rust_name(a.def_id).0.field_ident(); // use the _{rust-style fieldname} without keyword escaping
format!("_{ident}: volo_gen{ty}")
})
.join(",");
Expand Down
5 changes: 4 additions & 1 deletion volo-http/src/server/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,10 @@ impl Matcher {
Ok(())
}

fn at<'a>(&'a self, path: &'a str) -> Result<matchit::Match<&RouteId>, MatcherError> {
fn at<'a>(
&'a self,
path: &'a str,
) -> Result<matchit::Match<'a, 'a, &'a RouteId>, MatcherError> {
self.router.at(path).map_err(MatcherError::RouterMatchError)
}
}
Expand Down
Loading