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
http-dynamodb example #562
Conversation
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.
Thanks a lot for this contribution! I left you some comments to cleanup the code a little bit.
Hello Thanks for all the useful comment. I have attempted to implement all of the requested changes. In the end, I created two Item structs using serde, one that gets sent to Dynamo and one that is sent back to the user. I doubt this is the correct way. However, add_item requires ownership of the struct and with serde (serialize , deserialize) I could not figure out a way to implement the Copy or Move trait.. The compiler was not allowing me to reuse Item to return to the user as it had been changed.. So, I implemented this hack instead. I would also like to return a 400 error if the rest call made by the user is missing parameters.. Now the function crashes and returns a 500. Any ideas how to do this ? I see examples that you wrote in how to return errors.. But I can't figure out how to determine when serde realizes the request is missing a field and to return the missing field(s) back to the user.. Thanks.. getting there. |
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.
this is looking really nice! I just left a minor comment to be consistent with formatting.
examples/http-dynamodb/src/main.rs
Outdated
pub async fn add_item(client: &Client, item: Item, table: &String) -> Result<(), OtherError> { | ||
let user_av = AttributeValue::S(item.username); | ||
pub async fn add_item(client: &Client, item: Item, table: &str) -> Result<(), OtherError> | ||
{ let user_av = AttributeValue::S(item.username); |
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.
can you run cargo fmt
?
examples/http-dynamodb/src/main.rs
Outdated
//Serialize the data into the struct. | ||
let item: Item = serde_json::from_str(s).map_err(Box::new)?; | ||
//Create a copy to send back via the Response. | ||
let copy_item: Item = serde_json::from_str(s).map_err(Box::new)?; |
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.
since you're basically cloning the struct, it might be better to add Clone
to the structure's derive
attributes, and use item.clone()
when you call add_item
.
Code was formatted. |
examples/http-dynamodb/src/main.rs
Outdated
use aws_sdk_dynamodb::model::AttributeValue; | ||
use aws_sdk_dynamodb::{Client, Error as OtherError}; | ||
use lambda_http::{run, service_fn, Error, IntoResponse, Request}; | ||
use tracing::info; | ||
|
||
#[derive(serde::Deserialize, serde::Serialize)] |
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.
add
#[derive(serde::Deserialize, serde::Serialize)] | |
#[derive(Clone, serde::Deserialize, serde::Serialize)] |
examples/http-dynamodb/src/main.rs
Outdated
impl Clone for Item { | ||
fn clone(&self) -> Item { | ||
return Item { | ||
p_type: self.p_type.clone(), | ||
age: self.age.clone(), | ||
username: self.username.clone(), | ||
first: self.first.clone(), | ||
last: self.last.clone(), | ||
}; | ||
} | ||
} |
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 don't think you need this if you add the derive
to the struct.
impl Clone for Item { | |
fn clone(&self) -> Item { | |
return Item { | |
p_type: self.p_type.clone(), | |
age: self.age.clone(), | |
username: self.username.clone(), | |
first: self.first.clone(), | |
last: self.last.clone(), | |
}; | |
} | |
} |
Thanks. I had tried adding Clone to derive, and the compiler wasn't having it.. I thought maybe serde was somehow blocking Clone.. but it looks like I just had the syntax wrong. ;-) I've made the change. There is one last thing, should I be checking if the input request is correctly formatted and missing fields ? Right now, if you send a request with a missing field, the function crashes and returns a 500. I can see how to return an error from your examples, but I can't figure out how to pull out the errors from serde when calling let item: Item = serde_json::from_str(s).map_err(Box::new)?; |
Check if JSON is valid. If not, return 400 and abort. |
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.
Thanks for adding this example, I think it's good to be merged now! 🎊
Issue #, if available:
This is an example of a rust lambda function that takes in a JSON payload and writes to a Dynamodb table.
Description of changes:
First commit.
By submitting this pull request