Skip to content

Instantly share code, notes, and snippets.

@ksindi
Last active August 18, 2022 13:41
Show Gist options
  • Save ksindi/afe62a8aeaf5dd4d1a05fe9474af2d12 to your computer and use it in GitHub Desktop.
Save ksindi/afe62a8aeaf5dd4d1a05fe9474af2d12 to your computer and use it in GitHub Desktop.
use aws_sdk_dynamodb::model::AttributeValue;
use serde_json::Value;
fn parse_item(value: Value) -> HashMap<String, AttributeValue> {
match value_to_item(value) {
AttributeValue::M(map) => map,
other => panic!("can only insert top level values, got {:?}", other),
}
}
fn value_to_item(value: Value) -> AttributeValue {
match value {
Value::Null => AttributeValue::Null(true),
Value::Bool(b) => AttributeValue::Bool(b),
Value::Number(n) => AttributeValue::N(n.to_string()),
Value::String(s) => AttributeValue::S(s),
Value::Array(a) => AttributeValue::L(a.into_iter().map(value_to_item).collect()),
Value::Object(o) => {
AttributeValue::M(o.into_iter().map(|(k, v)| (k, value_to_item(v))).collect())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment