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

Use serde_dynamo for dynamodb::Event items #140

Merged
merged 1 commit into from
Mar 24, 2023
Merged

Use serde_dynamo for dynamodb::Event items #140

merged 1 commit into from
Mar 24, 2023

Conversation

bryanburgers
Copy link
Contributor

@bryanburgers bryanburgers commented Mar 7, 2023

Use serde_dynamo::Item instead of HashMap<String, AttributeValue> to represent the keys, old_image, and new_image in a dynamodb::Event.

This consolidates the Rust/DynamoDB ecosystem around fewer representations of DynamoDB items.

Note that serde_dynamo::AttributeValue and
aws_lambda_events::dynamodb::attributes::AttributeValue are slightly different (especially with regards to the Number enumeration) so this is a breaking change that would either need to go behind a feature flag or a major version bump.


I feel like this might be of interest to the wider community because

  • I see lots of instances in GitHub search of dynamodb::Event and serde_dynamo used together already, and
  • Like using serde_json, this helps prompt users to convert to a typed representation of their entity earlier.

I'm curious whether you think this is useful for aws-lambda-events and the wider Rust+AWS ecosystem.

Copy link
Owner

@calavera calavera left a comment

Choose a reason for hiding this comment

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

can you add some additional tests?

@bryanburgers
Copy link
Contributor Author

can you add some additional tests?

Absolutely! What sorts of tests would you like to see? Parsing from DynamoDB's JSON format to serde_dynamo::Item/serde_dynamo::AttributeValue is well tested in serde_dynamo itself, but I can certainly reproduce some of those here, too.

Or are there other aspects that would help increase confidence that this will work?

@bryanburgers
Copy link
Contributor Author

I just added the existing AttributeValue tests back in and confirmed they pass.

@@ -139,6 +139,18 @@ where
Ok(opt.unwrap_or_default())
}

/// Deserializes `Item`, mapping JSON `null` to an empty item.
pub(crate) fn deserialize_lambda_dynamodb_item<'de, D>(
Copy link
Owner

Choose a reason for hiding this comment

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

I think this code needs to be behind a feature flag. Otherwise, the library will fail to compile when dynamodb is not enabled.

Copy link
Owner

@calavera calavera left a comment

Choose a reason for hiding this comment

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

Can you check the serialization module?

Use `serde_dynamo::Item` instead of `HashMap<String, AttributeValue>` to
represent the `keys`, `old_image`, and `new_image` in a
`dynamodb::Event`.

This consolidates the Rust/DynamoDB ecosystem around fewer
representations of DynamoDB items.

Note that `serde_dynamo::AttributeValue` and
`aws_lambda_events::dynamodb::attributes::AttributeValue` are slightly
different (especially with regards to the `Number` enumeration) so this
is a breaking change that would either need to go behind a feature flag
or a major version bump.
@bryanburgers bryanburgers changed the base branch from main to master March 24, 2023 12:21
@bryanburgers
Copy link
Contributor Author

Changes in this most recent commit:

  • Rebased on top of the rustfmt commit (e4de006)
  • Gated deserialize_lambda_dynamodb_item on #[cfg(feature = "dynamodb")]
  • Added a test test_deserialize_lambda_dynamodb_item
    • Tests pass with cargo test --no-default-features
    • Tests pass with cargo test --features dynamodb
    • Tests pass with cargo test --all-features

@bryanburgers bryanburgers requested a review from calavera March 24, 2023 16:40
@calavera calavera merged commit 50ce8ed into calavera:master Mar 24, 2023
calavera pushed a commit that referenced this pull request Mar 24, 2023
Use `serde_dynamo::Item` instead of `HashMap<String, AttributeValue>` to
represent the `keys`, `old_image`, and `new_image` in a
`dynamodb::Event`.

This consolidates the Rust/DynamoDB ecosystem around fewer
representations of DynamoDB items.

Note that `serde_dynamo::AttributeValue` and
`aws_lambda_events::dynamodb::attributes::AttributeValue` are slightly
different (especially with regards to the `Number` enumeration) so this
is a breaking change that would either need to go behind a feature flag
or a major version bump.

(cherry picked from commit 50ce8ed)
Signed-off-by: David Calavera <david.calavera@gmail.com>
calavera added a commit that referenced this pull request Mar 24, 2023
Use `serde_dynamo::Item` instead of `HashMap<String, AttributeValue>` to
represent the `keys`, `old_image`, and `new_image` in a
`dynamodb::Event`.

This consolidates the Rust/DynamoDB ecosystem around fewer
representations of DynamoDB items.

Note that `serde_dynamo::AttributeValue` and
`aws_lambda_events::dynamodb::attributes::AttributeValue` are slightly
different (especially with regards to the `Number` enumeration) so this
is a breaking change that would either need to go behind a feature flag
or a major version bump.

(cherry picked from commit 50ce8ed)
Signed-off-by: David Calavera <david.calavera@gmail.com>

Co-authored-by: Bryan Burgers <bryan@burgers.io>
@@ -169,7 +29,7 @@ mod test {

let attr: AttributeValue = serde_json::from_value(value.clone()).unwrap();
match attr {
AttributeValue::String(ref s) => assert_eq!("value", s.as_str()),
AttributeValue::S(ref s) => assert_eq!("value", s.as_str()),

Choose a reason for hiding this comment

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

In my humble opinion this is a regression in readability. Fully spelled out variant names with the appropriate #[serde(rename = "…")] attributes seemed better.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants