-1

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?

Richard
  • 62,943
  • 126
  • 334
  • 542
  • `note: perhaps two different versions of crate `rusoto_dynamodb` are being used?` Are you using multiple crate version of same crate `rusoto_dynamodb`? check your `cargo.toml ` – Dhaval Purohit Mar 14 '23 at 09:00
  • Thank you. My `cargo.toml` only has one entry: `rusoto_dynamodb = "0.47.0"`. – Richard Mar 14 '23 at 19:33

1 Answers1

0

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.

cafce25
  • 15,907
  • 4
  • 25
  • 31
  • Thank you very much for the explanation :) That's quite mysterious, because `cargo tree` only shows v0.47.0 of `rusoto_dynamodb` being used! – Richard Mar 14 '23 at 19:36
  • Yes only v0.47.0 in the dependencies but you depend on [0.48.0 in your `Cargo.toml`](https://stackoverflow.com/questions/75730866/insert-string-into-dynamodb-with-serde-dynamodb-expected-struct-attributevalue/75730976?noredirect=1#comment133607182_75730866) so the easiest fix is replace 48 with 47 in your Cargo.toml but you're missing out on patches and features from 0.48. – cafce25 Mar 14 '23 at 19:38
  • Actually, I was referencing v0.47.0 of rusoto_dynamodb in Cargo.toml. The problem seemed to be that I was using v0.48.0 of rusoto_core. I've downgraded both crates to v0.47.0 (to be compatible with serde_dyamodb, which wants 0.47.0 of rusoto_dynamodb) and... now have a compilation error from v0.47.0 of rusoto_core :( but I guess that's a separate issue, so will accept. Thank you! – Richard Mar 14 '23 at 20:11