-
Notifications
You must be signed in to change notification settings - Fork 359
[dont merge] support both apigw and alb events as http::{Request,Response} (cargo features) #65
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dad862e
support both apigw and alb events as http::{Request,Response}
softprops a49ff83
experimenting with cargo flags
softprops a4da78b
make struct fields accessible when not embedded within a struct
softprops a988721
Merge branch 'master' of github.com:softprops/aws-lambda-rust-runtime…
softprops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,10 +44,10 @@ pub(crate) struct LambdaRequest<'a> { | |
/// the `lambda.multi_value_headers.enabled` target group setting turned on | ||
#[serde(default, deserialize_with = "nullable_default")] | ||
pub(crate) multi_value_query_string_parameters: StrMap, | ||
/// ALB events do not have path parameters. | ||
#[cfg(feature = "apigw")] | ||
#[serde(default, deserialize_with = "nullable_default")] | ||
pub(crate) path_parameters: StrMap, | ||
/// ALB events do not have stage variables. | ||
#[cfg(feature = "apigw")] | ||
#[serde(default, deserialize_with = "nullable_default")] | ||
pub(crate) stage_variables: StrMap, | ||
pub(crate) body: Option<Cow<'a, str>>, | ||
|
@@ -58,6 +58,7 @@ pub(crate) struct LambdaRequest<'a> { | |
|
||
/// Event request context as an enumeration of request contexts | ||
/// for both ALB and API Gateway http events | ||
#[cfg(all(feature = "apigw", feature = "alb"))] | ||
#[derive(Deserialize, Debug, Clone)] | ||
#[serde(untagged)] | ||
pub enum RequestContext { | ||
|
@@ -81,6 +82,17 @@ pub enum RequestContext { | |
Alb { elb: Elb }, | ||
} | ||
|
||
/// ALB request context | ||
#[cfg(all(feature = "alb", not(all(feature = "apigw", feature = "alb"))))] | ||
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. TIL learned cfg attrs are like a smaller dialect of lisp! |
||
#[derive(Default, Deserialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct RequestContext { | ||
pub elb: Elb, | ||
} | ||
|
||
/// Api Gateway request context | ||
#[cfg(all(feature = "apigw", not(all(feature = "apigw", feature = "alb"))))] | ||
#[derive(Default, Deserialize, Debug, Clone)] | ||
impl Default for RequestContext { | ||
fn default() -> Self { | ||
RequestContext::ApiGateway { | ||
|
@@ -97,16 +109,6 @@ impl Default for RequestContext { | |
} | ||
} | ||
|
||
impl RequestContext { | ||
/// Return true if this request context represents an ALB request | ||
pub fn is_alb(&self) -> bool { | ||
match self { | ||
RequestContext::Alb { .. } => true, | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
/// Elastic load balancer context information | ||
#[derive(Deserialize, Debug, Default, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
|
@@ -115,6 +117,41 @@ pub struct Elb { | |
pub target_group_arn: String, | ||
} | ||
|
||
#[cfg(all(feature = "apigw", feature = "alb"))] | ||
impl Default for RequestContext { | ||
fn default() -> Self { | ||
RequestContext::ApiGateway { | ||
account_id: Default::default(), | ||
resource_id: Default::default(), | ||
stage: Default::default(), | ||
request_id: Default::default(), | ||
resource_path: Default::default(), | ||
http_method: Default::default(), | ||
authorizer: Default::default(), | ||
api_id: Default::default(), | ||
identity: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "alb")] | ||
impl RequestContext { | ||
/// Return true if this request context represents an ALB request | ||
pub fn is_alb(&self) -> bool { | ||
#[cfg(all(feature = "apigw", feature = "alb"))] | ||
{ | ||
match self { | ||
RequestContext::Alb { .. } => true, | ||
_ => false, | ||
} | ||
} | ||
#[cfg(all(feature = "alb", not(all(feature = "apigw", feature = "alb"))))] | ||
{ | ||
true | ||
} | ||
} | ||
} | ||
|
||
/// Identity assoicated with API Gateway request | ||
#[derive(Deserialize, Debug, Default, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
|
@@ -336,6 +373,7 @@ mod tests { | |
fn requests_convert() { | ||
let mut headers = HeaderMap::new(); | ||
headers.insert("Host", "www.rust-lang.org".parse().unwrap()); | ||
|
||
let lambda_request: LambdaRequest<'_> = LambdaRequest { | ||
path: "/foo".into(), | ||
headers, | ||
|
@@ -348,6 +386,7 @@ mod tests { | |
assert_eq!(expected.method(), actual.method()); | ||
} | ||
|
||
#[cfg(feature = "apigw")] | ||
#[test] | ||
fn deserializes_apigw_request_events() { | ||
// from the docs | ||
|
@@ -357,6 +396,7 @@ mod tests { | |
assert!(result.is_ok(), format!("event was not parsed as expected {:?}", result)); | ||
} | ||
|
||
#[cfg(feature = "alb")] | ||
#[test] | ||
fn deserialize_alb_request_events() { | ||
// from the docs | ||
|
@@ -366,6 +406,7 @@ mod tests { | |
assert!(result.is_ok(), format!("event was not parsed as expected {:?}", result)); | ||
} | ||
|
||
#[cfg(feature = "apigw")] | ||
#[test] | ||
fn deserializes_apigw_multi_value_request_events() { | ||
// from docs | ||
|
@@ -388,6 +429,7 @@ mod tests { | |
); | ||
} | ||
|
||
#[cfg(feature = "alb")] | ||
#[test] | ||
fn deserializes_alb_multi_value_request_events() { | ||
// from docs | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
big drawback here. if a consumer sets default-flags to false you'll get neither apigw nor alb! I don't think there's a way to express "there can be at least one!" in cargo