-
Hi! I'm writing a pet project in Rust, using Dynamodb. My overall experience of using the SDK is very good, however, one thing seems to be quite verbose and problematic. It's reading the attribute value from the Item retrieved from the table. Maybe I'm missing something as my knowledge of Rust is relatively fresh. Every time I read an attribute, I have to run two checks:
And later, even if it actually exists, I have to check if it has the "expected" type:
Only then I can use the attribute value. The thing is, it's very verbose and I have to do it every time I want to read some attribute.
Is there a better way to do it? Some smart shortcut or method that I'm missing? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The 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, Rust will force you to account for the case where typed data is not what you expect. |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
The
as_
methods return the untyped value so you can try to resolve a different type. You could do something like:you could write a macro or helper function to shorten this pattern, but in general, Ru…