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

Resolve Mismatched Types Error in Macro Expansion and Fix test #501

Merged
merged 6 commits into from
Nov 22, 2023
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
16 changes: 8 additions & 8 deletions crates/macros/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn handle_fn(salvo: &Ident, sig: &Signature) -> syn::Result<TokenStream> {
for input in &sig.inputs {
match parse_input_type(input) {
InputType::Request(_pat) => {
call_args.push(Ident::new("req", Span::call_site()));
call_args.push(Ident::new("__macro_generated_req", Span::call_site()));
}
InputType::Depot(_pat) => {
call_args.push(Ident::new("depot", Span::call_site()));
Expand All @@ -106,7 +106,7 @@ fn handle_fn(salvo: &Ident, sig: &Signature) -> syn::Result<TokenStream> {
let idv = id.to_token_stream().to_string();

extract_ts.push(quote! {
let #id: #ty = match <#ty as #salvo::Extractible>::extract_with_arg(req, #idv).await {
let #id: #ty = match <#ty as #salvo::Extractible>::extract_with_arg(__macro_generated_req, #idv).await {
Ok(data) => data,
Err(e) => {
#salvo::__private::tracing::error!(error = ?e, "failed to extract data");
Expand Down Expand Up @@ -134,15 +134,15 @@ fn handle_fn(salvo: &Ident, sig: &Signature) -> syn::Result<TokenStream> {
if sig.asyncness.is_none() {
Ok(quote! {
#[inline]
async fn handle(&self, req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
async fn handle(&self, __macro_generated_req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
#(#extract_ts)*
Self::#name(#(#call_args),*)
}
})
} else {
Ok(quote! {
#[inline]
async fn handle(&self, req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
async fn handle(&self, __macro_generated_req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
#(#extract_ts)*
Self::#name(#(#call_args),*).await
}
Expand All @@ -153,17 +153,17 @@ fn handle_fn(salvo: &Ident, sig: &Signature) -> syn::Result<TokenStream> {
if sig.asyncness.is_none() {
Ok(quote! {
#[inline]
async fn handle(&self, req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
async fn handle(&self, __macro_generated_req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
#(#extract_ts)*
#salvo::Writer::write(Self::#name(#(#call_args),*), req, depot, res).await;
#salvo::Writer::write(Self::#name(#(#call_args),*), __macro_generated_req, depot, res).await;
}
})
} else {
Ok(quote! {
#[inline]
async fn handle(&self, req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
async fn handle(&self, __macro_generated_req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
#(#extract_ts)*
#salvo::Writer::write(Self::#name(#(#call_args),*).await, req, depot, res).await;
#salvo::Writer::write(Self::#name(#(#call_args),*).await, __macro_generated_req, depot, res).await;
}
})
}
Expand Down
24 changes: 12 additions & 12 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mod tests {
fn test_handler_for_fn() {
let input = quote! {
#[handler]
async fn hello(req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl) {
async fn hello(__macro_generated_req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl) {
res.render_plain_text("Hello World");
}
};
Expand All @@ -82,7 +82,7 @@ mod tests {
struct hello;
impl hello {
#[handler]
async fn hello(req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl) {
async fn hello(__macro_generated_req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl) {
{
res.render_plain_text("Hello World");
}
Expand All @@ -93,12 +93,12 @@ mod tests {
#[inline]
async fn handle(
&self,
req: &mut salvo::Request,
__macro_generated_req: &mut salvo::Request,
depot: &mut salvo::Depot,
res: &mut salvo::Response,
ctrl: &mut salvo::FlowCtrl
) {
Self::hello(req, depot, res, ctrl).await
Self::hello(__macro_generated_req, depot, res, ctrl).await
}
}
}
Expand All @@ -110,7 +110,7 @@ mod tests {
fn test_handler_for_fn_return_result() {
let input = quote! {
#[handler]
async fn hello(req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl) -> Result<(), Error> {
async fn hello(__macro_generated_req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl) -> Result<(), Error> {
Ok(())
}
};
Expand All @@ -124,7 +124,7 @@ mod tests {
impl hello {
#[handler]
async fn hello(
req: &mut Request,
__macro_generated_req: &mut Request,
depot: &mut Depot,
res: &mut Response,
ctrl: &mut FlowCtrl
Expand All @@ -139,12 +139,12 @@ mod tests {
#[inline]
async fn handle(
&self,
req: &mut salvo::Request,
__macro_generated_req: &mut salvo::Request,
depot: &mut salvo::Depot,
res: &mut salvo::Response,
ctrl: &mut salvo::FlowCtrl
) {
salvo::Writer::write(Self::hello(req, depot, res, ctrl).await, req, depot, res).await;
salvo::Writer::write(Self::hello(__macro_generated_req, depot, res, ctrl).await, __macro_generated_req, depot, res).await;
}
}
}
Expand All @@ -157,7 +157,7 @@ mod tests {
let input = quote! {
#[handler]
impl Hello {
fn handle(req: &mut Request, depot: &mut Depot, res: &mut Response) {
fn handle(__macro_generated_req: &mut Request, depot: &mut Depot, res: &mut Response) {
res.render_plain_text("Hello World");
}
}
Expand All @@ -168,7 +168,7 @@ mod tests {
quote! {
#[handler]
impl Hello {
fn handle(req: &mut Request, depot: &mut Depot, res: &mut Response) {
fn handle(__macro_generated_req: &mut Request, depot: &mut Depot, res: &mut Response) {
res.render_plain_text("Hello World");
}
}
Expand All @@ -177,12 +177,12 @@ mod tests {
#[inline]
async fn handle(
&self,
req: &mut salvo::Request,
__macro_generated_req: &mut salvo::Request,
depot: &mut salvo::Depot,
res: &mut salvo::Response,
ctrl: &mut salvo::FlowCtrl
) {
Self::handle(req, depot, res)
Self::handle(__macro_generated_req, depot, res)
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions crates/oapi-macros/src/endpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn handle_fn(salvo: &Ident, oapi: &Ident, sig: &Signature) -> syn::Result<(Token
let idv = id.to_token_stream().to_string();
let idv = idv.trim_start_matches('_');
extract_ts.push(quote! {
let #id: #ty = match <#ty as #salvo::Extractible>::extract_with_arg(req, #idv).await {
let #id: #ty = match <#ty as #salvo::Extractible>::extract_with_arg(__macro_generated_req, #idv).await {
Ok(data) => {
data
},
Expand Down Expand Up @@ -223,15 +223,15 @@ fn handle_fn(salvo: &Ident, oapi: &Ident, sig: &Signature) -> syn::Result<(Token
if sig.asyncness.is_none() {
quote! {
#[inline]
async fn handle(&self, req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
async fn handle(&self, __macro_generated_req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
#(#extract_ts)*
Self::#name(#(#call_args),*)
}
}
} else {
quote! {
#[inline]
async fn handle(&self, req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
async fn handle(&self, __macro_generated_req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
#(#extract_ts)*
Self::#name(#(#call_args),*).await
}
Expand All @@ -245,17 +245,17 @@ fn handle_fn(salvo: &Ident, oapi: &Ident, sig: &Signature) -> syn::Result<(Token
if sig.asyncness.is_none() {
quote! {
#[inline]
async fn handle(&self, req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
async fn handle(&self, __macro_generated_req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
#(#extract_ts)*
#salvo::Writer::write(Self::#name(#(#call_args),*), req, depot, res).await;
#salvo::Writer::write(Self::#name(#(#call_args),*), __macro_generated_req, depot, res).await;
}
}
} else {
quote! {
#[inline]
async fn handle(&self, req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
async fn handle(&self, __macro_generated_req: &mut #salvo::Request, depot: &mut #salvo::Depot, res: &mut #salvo::Response, ctrl: &mut #salvo::FlowCtrl) {
#(#extract_ts)*
#salvo::Writer::write(Self::#name(#(#call_args),*).await, req, depot, res).await;
#salvo::Writer::write(Self::#name(#(#call_args),*).await, __macro_generated_req, depot, res).await;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oapi-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ mod tests {
#[inline]
async fn handle(
&self,
req: &mut salvo::Request,
__macro_generated_req: &mut salvo::Request,
depot: &mut salvo::Depot,
res: &mut salvo::Response,
ctrl: &mut salvo::FlowCtrl
Expand Down
10 changes: 5 additions & 5 deletions examples/oapi-todos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ pub async fn list_todos(offset: QueryParam<usize, false>, limit: QueryParam<usiz

/// Create new todo.
#[endpoint(tags("todos"), status_codes(201, 409))]
pub async fn create_todo(new_todo: JsonBody<Todo>) -> Result<StatusCode, StatusError> {
tracing::debug!(todo = ?new_todo, "create todo");
pub async fn create_todo(req: JsonBody<Todo>) -> Result<StatusCode, StatusError> {
tracing::debug!(todo = ?req, "create todo");

let mut vec = STORE.lock().await;

for todo in vec.iter() {
if todo.id == new_todo.id {
tracing::debug!(id = ?new_todo.id, "todo already exists");
if todo.id == req.id {
tracing::debug!(id = ?req.id, "todo already exists");
return Err(StatusError::bad_request().brief("todo already exists"));
}
}

vec.push(new_todo.into_inner());
vec.push(req.into_inner());
Ok(StatusCode::CREATED)
}

Expand Down