-
Notifications
You must be signed in to change notification settings - Fork 360
Make lambda::Context a first class part of the Handler api #233
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,42 +38,48 @@ pub fn lambda(attr: TokenStream, item: TokenStream) -> TokenStream { | |
} | ||
|
||
let result = match inputs.len() { | ||
1 => { | ||
let event = match inputs.first().unwrap() { | ||
2 => { | ||
let event = match inputs.first().expect("expected event argument") { | ||
FnArg::Typed(arg) => arg, | ||
_ => { | ||
let tokens = quote_spanned! { inputs.span() => | ||
compile_error!("fn main must take a fully formed argument"); | ||
compile_error!("fn main's first argument must be fully formed"); | ||
}; | ||
return TokenStream::from(tokens); | ||
} | ||
}; | ||
let arg_name = &event.pat; | ||
let arg_type = &event.ty; | ||
let event_name = &event.pat; | ||
let event_type = &event.ty; | ||
let context = match inputs.iter().nth(1).expect("expected context argument") { | ||
FnArg::Typed(arg) => arg, | ||
_ => { | ||
let tokens = quote_spanned! { inputs.span() => | ||
compile_error!("fn main's second argument must be fully formed"); | ||
}; | ||
return TokenStream::from(tokens); | ||
} | ||
}; | ||
let context_name = &context.pat; | ||
let context_type = &context.ty; | ||
|
||
if is_http(&args) { | ||
quote_spanned! { input.span() => | ||
use lambda_http::lambda::LambdaCtx; | ||
|
||
#(#attrs)* | ||
#asyncness fn main() { | ||
async fn actual(#arg_name: #arg_type) #ret { | ||
#body | ||
} | ||
async fn actual(#event_name: #event_type, #context_name: #context_type) #ret #body | ||
|
||
let f = lambda_http::handler(actual); | ||
lambda_http::lambda::run(f).await.unwrap(); | ||
} | ||
} | ||
} else { | ||
quote_spanned! { input.span() => | ||
|
||
use lambda::LambdaCtx; | ||
|
||
#(#attrs)* | ||
#asyncness fn main() { | ||
async fn actual(#arg_name: #arg_type) #ret { | ||
#body | ||
} | ||
async fn actual(#event_name: #event_type, #context_name: #context_type) #ret #body | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: I ditched the branches because I noticed a warning emitted about needless braces. we debugging the result I could see that body actually included braces so the result looked like |
||
|
||
let f = lambda::handler_fn(actual); | ||
lambda::run(f).await.unwrap(); | ||
} | ||
|
@@ -82,7 +88,7 @@ pub fn lambda(attr: TokenStream, item: TokenStream) -> TokenStream { | |
} | ||
_ => { | ||
let tokens = quote_spanned! { inputs.span() => | ||
compile_error!("The #[lambda] macro can accept only a single argument."); | ||
compile_error!("The #[lambda] macro can expects two arguments: a triggered event and lambda context."); | ||
}; | ||
return TokenStream::from(tokens); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
use lambda_http::{lambda, IntoResponse, Request}; | ||
use lambda_http::{ | ||
lambda::{lambda, Context}, | ||
IntoResponse, Request, | ||
}; | ||
|
||
type Error = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
|
||
#[lambda(http)] | ||
#[tokio::main] | ||
async fn main(_: Request) -> Result<impl IntoResponse, Error> { | ||
async fn main(_: Request, _: Context) -> Result<impl IntoResponse, Error> { | ||
Ok("👋 world") | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
use lambda::lambda; | ||
use lambda::{lambda, Context}; | ||
use serde_json::Value; | ||
|
||
type Error = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
|
||
#[lambda] | ||
#[tokio::main] | ||
async fn main(event: Value) -> Result<Value, Error> { | ||
async fn main(event: Value, _: Context) -> Result<Value, Error> { | ||
Ok(event) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not actually sure why these were here before but they cased duplicate import errors as handlers no import this type themselves in their function signatures