Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Make the http method in the Websocket request struct optional. #34

Merged
merged 3 commits into from
Mar 5, 2021
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
28 changes: 25 additions & 3 deletions aws_lambda_events/src/custom_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,10 @@ where
pub mod http_method {
use http::Method;
use serde::de;
use serde::de::{Unexpected, Visitor};
use serde::de::{Deserialize, Unexpected, Visitor};
use serde::{Deserializer, Serializer};
use std::fmt;

/// Implementation detail. Use derive annotations instead.
pub fn serialize<S: Serializer>(method: &Method, ser: S) -> Result<S::Ok, S::Error> {
ser.serialize_str(method.as_str())
}
Expand All @@ -241,13 +240,36 @@ pub mod http_method {
}
}

/// Implementation detail.
pub fn deserialize<'de, D>(de: D) -> Result<Method, D::Error>
where
D: Deserializer<'de>,
{
de.deserialize_str(MethodVisitor)
}

pub fn deserialize_optional<'de, D>(deserializer: D) -> Result<Option<Method>, D::Error>
where
D: Deserializer<'de>,
{
let s: Option<&str> = Option::deserialize(deserializer)?;
if let Some(val) = s {
let visitor = MethodVisitor {};
return visitor.visit_str(val).map(Some);
}

Ok(None)
}

pub fn serialize_optional<S: Serializer>(
method: &Option<Method>,
ser: S,
) -> Result<S::Ok, S::Error> {
if let Some(method) = method {
return serialize(method, ser);
}

ser.serialize_none()
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_events/src/generated/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
These types are automatically generated from the
[official Go SDK](https://github.com/aws/aws-lambda-go/tree/master/events).

Generated from commit [b3dd246b7d83fd62856549646711e5328075c69a](https://github.com/aws/aws-lambda-go/commit/b3dd246b7d83fd62856549646711e5328075c69a).
Generated from commit [a8e138ffcbc1fffe8b5a615dc9d8276b844d670a](https://github.com/aws/aws-lambda-go/commit/a8e138ffcbc1fffe8b5a615dc9d8276b844d670a).
6 changes: 4 additions & 2 deletions aws_lambda_events/src/generated/apigw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,11 @@ pub struct ApiGatewayWebsocketProxyRequest {
#[serde(deserialize_with = "deserialize_lambda_string")]
#[serde(default)]
pub path: Option<String>,
#[serde(with = "http_method")]
#[serde(deserialize_with = "http_method::deserialize_optional")]
#[serde(serialize_with = "http_method::serialize_optional")]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "httpMethod")]
pub http_method: Method,
pub http_method: Option<Method>,
#[serde(deserialize_with = "http_serde::header_map::deserialize")]
#[serde(serialize_with = "serialize_headers")]
pub headers: HeaderMap,
Expand Down
4 changes: 4 additions & 0 deletions aws_lambda_events/src/generated/cognito.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ pub struct CognitoEventUserPoolsMigrateUserRequest {
pub password: Option<String>,
#[serde(deserialize_with = "deserialize_lambda_map")]
#[serde(default)]
#[serde(rename = "validationData")]
pub validation_data: HashMap<String, String>,
#[serde(deserialize_with = "deserialize_lambda_map")]
#[serde(default)]
#[serde(rename = "clientMetadata")]
pub client_metadata: HashMap<String, String>,
}
Expand Down
18 changes: 18 additions & 0 deletions aws_lambda_events/src/generated/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct LexEvent {
pub output_dialog_mode: Option<String>,
#[serde(rename = "currentIntent")]
pub current_intent: Option<LexCurrentIntent>,
#[serde(rename = "alternativeIntents")]
pub alternative_intents: Option<Vec<LexAlternativeIntents>>,
#[serde(rename = "dialogAction")]
pub dialog_action: Option<LexDialogAction>,
}
Expand All @@ -36,6 +38,22 @@ pub struct LexBot {
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct LexCurrentIntent {
pub name: Option<String>,
#[serde(rename = "nluIntentConfidenceScore")]
pub nlu_intent_confidence_score: Option<f64>,
pub slots: Option<Slots>,
#[serde(deserialize_with = "deserialize_lambda_map")]
#[serde(default)]
#[serde(rename = "slotDetails")]
pub slot_details: HashMap<String, SlotDetail>,
#[serde(rename = "confirmationStatus")]
pub confirmation_status: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct LexAlternativeIntents {
pub name: Option<String>,
#[serde(rename = "nluIntentConfidenceScore")]
pub nlu_intent_confidence_score: Option<f64>,
pub slots: Option<Slots>,
#[serde(deserialize_with = "deserialize_lambda_map")]
#[serde(default)]
Expand Down
28 changes: 21 additions & 7 deletions aws_lambda_events_codegen/go_to_rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ struct FieldDef {
struct StructureFieldDef<'a> {
struct_name: &'a str,
member_name: &'a str,
omit_empty: bool,
}

fn parse_comment(c: &str) -> String {
Expand Down Expand Up @@ -290,6 +291,7 @@ fn parse_struct(pairs: Pairs<'_, Rule>) -> Result<(codegen::Struct, HashSet<Stri
let member_def = StructureFieldDef {
struct_name: &camel_cased_struct_name,
member_name: &member_name,
omit_empty: f.omit_empty,
};

let mut rust_data =
Expand Down Expand Up @@ -729,11 +731,24 @@ fn translate_go_type_to_rust_type<'a>(
libraries.insert("crate::custom_serde::*".to_string());
libraries.insert("http::Method".to_string());

let mut annotations = vec!["#[serde(with = \"http_method\")]".to_string()];
if let Some(def) = member_def {
if def.struct_name == "ApiGatewayWebsocketProxyRequest" {
annotations = vec![
"#[serde(deserialize_with = \"http_method::deserialize_optional\")]"
.to_string(),
"#[serde(serialize_with = \"http_method::serialize_optional\")]"
.to_string(),
"#[serde(skip_serializing_if = \"Option::is_none\")]".to_string(),
];
}
}

RustType {
value: "Method".into(),
annotations: vec!["#[serde(with = \"http_method\")]".to_string()],
generics: vec![],
annotations,
libraries,
generics: vec![],
}
}
GoType::StringType if is_http_body(member_def) => {
Expand Down Expand Up @@ -960,15 +975,14 @@ fn is_http_multivalue_headers<'a>(def: Option<&'a StructureFieldDef>) -> bool {

fn is_http_method<'a>(def: Option<&'a StructureFieldDef>) -> bool {
match def {
Some(&StructureFieldDef { member_name, .. }) if member_name == "http_method" => true,
Some(&StructureFieldDef {
member_name,
struct_name,
..
}) if struct_name == "ApiGatewayV2httpRequestContextHttpDescription"
&& member_name == "method" =>
{
true
}) => {
member_name == "http_method"
|| (struct_name == "ApiGatewayV2httpRequestContextHttpDescription"
&& member_name == "method")
}
_ => false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@ pub struct HttpMessage {
pub struct ApiGatewayProxyResponse {
#[serde(skip_serializing_if = "Option::is_none")]
pub body: Option<Body>,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct ApiGatewayWebsocketProxyRequest {
#[serde(deserialize_with = "http_method::deserialize_optional")]
#[serde(serialize_with = "http_method::serialize_optional")]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "httpMethod")]
pub http_method: Option<Method>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ type HttpMessage struct {
type ApiGatewayProxyResponse struct {
Body string `json:"body"`
}

type ApiGatewayWebsocketProxyRequest struct {
HttpMethod string `json:"httpMethod,omitempty"`
}