Skip to content

DynamoDB - Convenient way to read attribute of an Item #521

Answered by rcoh
sosnowski asked this question in Q&A
Discussion options

You must be logged in to vote

And later, even if it actually exists, I have to check if it has the "expected" type:
This is a general problem with untyped data—you need to check that it exists and has the type you expect. There are crates like https://crates.io/crates/serde_dynamo/3.0.3/dependencies which provide an ergonomic wrapper around AttributeValue

The as_ methods return the untyped value so you can try to resolve a different type. You could do something like:

db_item
        .get("some_column_name")
        .ok_or("missing field")?
        .as_s()
        .map_err(|value| format!("expected a string, found: {:?}", value))?;

you could write a macro or helper function to shorten this pattern, but in general, Ru…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@sosnowski
Comment options

Answer selected by sosnowski
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants