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

feat: serde dynamo #40

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

hffmnn
Copy link
Contributor

@hffmnn hffmnn commented Feb 4, 2022

🔥 Don't merge: this is rather experimental and up for discussion!
🔥 Note: So far I didn't deploy the API with theses changes so I am not sure, if it works as expected.

No issue, I wanted to try out serde_dynamo crate with the experimental support for the official aws-sdk.

Description of changes:

  • Use serde_dynamo from_item and from_items and remove handcrafted From and TryFrom implementations
  • Use serde_dynamo from_attribute_value and remove src/store/dynamodb/ext.rs

let products: Vec<Product> = match res.items {
Some(items) => from_items(items).map_err(|_|
// TODO: Find out correct error from underlying error?
Error::InternalError("Missing name"))?,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This already shows a problem: How to map the underlying error to our very specific error Missing name. I don't think that the underlying serde errors give us that info: https://github.com/zenlist/serde_dynamo/blob/3.0.0-alpha/src/error.rs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into the source code, it looks like it just uses serde_dynamo::ErrorImpl::Message, which might contain the right message - but that's not a guarantee. The best thing would be to write a small sample using the Product struct and serde_dynamo.

@@ -69,7 +68,8 @@ impl StoreGet for DynamoDBStore {
.await?;

Ok(match res.item {
Some(item) => Some(item.try_into()?),
// TODO: Find out correct error from underlying error?
Some(item) => from_item(item).map_err(|_| Error::InternalError("Missing name"))?,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem as above.

@@ -84,7 +84,8 @@ impl StorePut for DynamoDBStore {
self.client
.put_item()
.table_name(&self.table_name)
.set_item(Some(product.into()))
// TODO: Can this fail?
.set_item(Some(to_item(product).unwrap()))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have a unit test for that, so we tested that to_item can serialize a product.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That shouldn't fail since all the fields in a product map to valid DynamoDB attributes.

let next = res.last_evaluated_key.map(|m| m.get_s("id").unwrap());
let next = res
.last_evaluated_key
.map(|m| from_attribute_value(m["id"].clone()).unwrap());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is a more elegant way to write this?

@nmoutschen
Copy link
Contributor

Hey @hffmnn! Thanks a lot for creating this!

I think given that serde_dynamo is in alpha for support for the AWS SDK, we should probably hold merging this until it's stable, but it's definitively a great idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants