rustrusoto

Insert string into DynamoDB with serde_dynamodb: expected struct `AttributeValue`, found struct `rusoto_dynamodb::generated::AttributeValue`


I'm trying to write a nested JSON string into DynamoDB, using

Here's my string:

json_string = "{\"name\":\"as.up.data.forward\",\"time\":\"2023-03-13T18:58:41.933481750Z\",\"identifiers\":[{\"device_ids\":{\"device_id\":\"eui-a84041411184ad68\",\"application_ids\":{\"application_id\":\"thermometer2\"}]}"

Here's my code:

use std::collections::{HashMap};
use lambda_http::{service_fn, Body, Error, IntoResponse, Request, RequestExt};
use rusoto_core::Region;
use rusoto_dynamodb::{AttributeValue, DynamoDb, DynamoDbClient, PutItemInput};
use serde_dynamodb::to_hashmap;

async fn write_to_dynamodb(json_string: &String) -> Result<(), Box<dyn std::error::Error>> {
    let client = DynamoDbClient::new(Region::EuWest2);

    let data: HashMap<String, AttributeValue> = to_hashmap(&json_string).unwrap();
    client.put_item(PutItemInput{
        item: data,
        table_name: "MyTable".to_string(),
        ..PutItemInput::default()
    }).await?;

    Ok(())
}

However, this gives me the following error:

error[E0308]: mismatched types
  --> src/main.rs:97:49
   |
97 |     let data: HashMap<String, AttributeValue> = to_hashmap(&json_string).unwrap();
   |               -------------------------------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `AttributeValue`, found struct `rusoto_dynamodb::generated::AttributeValue`
   |               |
   |               expected due to this
   |
   = note: struct `rusoto_dynamodb::generated::AttributeValue` and struct `AttributeValue` have similar names, but are actually distinct types
note: struct `rusoto_dynamodb::generated::AttributeValue` is defined in crate `rusoto_dynamodb`
  --> /Users/anna/.cargo/registry/src/github.com-1ecc6299db9ec823/rusoto_dynamodb-0.47.0/src/generated.rs:84:1
   |
84 | pub struct AttributeValue {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
note: struct `AttributeValue` is defined in crate `rusoto_dynamodb`
  --> /Users/anna/.cargo/registry/src/github.com-1ecc6299db9ec823/rusoto_dynamodb-0.48.0/src/generated.rs:84:1
   |
84 | pub struct AttributeValue {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: perhaps two different versions of crate `rusoto_dynamodb` are being used?

How can I fix this?


Solution

  • You should first check which package depends on rusoto_dynamodb = "0.47.0", the dependency tree generated by cargo tree can come in handy for that. If those packages have an update that depend on the newer rusoto_dynamodb then you should update them to have access to the latest bug fixes and features.

    If you can't upgrade or remove the other dependencies you'll have to downgrade rusoto_dynamodb:

    [dependencies]
    rusoto_dynamodb = "0.47.0"
    

    or similar.