3

I was using dynamodbattribute.ConvertToMap(item) function to save items in dynamodb. But later ConvertToMap deprecated and I started to use dynamodbattribute.MarshalMap(item) function. But I can not get items from db which are saved with ConvertToMap function.

dynamodbattribute.ConvertToMap(item) saved items in dynamodb like this:enter image description here dynamodbattribute.Marshal(item) saved items in dynamodb like this:enter image description here

I'm getting items from db like this:

err := d.table.Get("id", appId).Order(false).Limit(1).One(&item)

This function works with items which saved with dynamodbattribute.MarshalMap(*item) function. But old items which are saved with dynamodbattribute.ConvertToMap(*item) function are not readable with

err := d.table.Get("id", appId).Order(false).Limit(1).One(&item)

It gives this error:dynamo: cannot unmarshal map data into slice

How can I read old items with same function ? I'm using aws sdk: github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute

levniko
  • 91
  • 7
  • DynamoDB stores all data in the same DynamoDB JSON format. There must be a physical difference on how they were saved, please update your question showing the item as saved by `ConvertToMap` and a similar item saved by `MarshalMap`. Also share which specific client you are using. – Leeroy Hannigan Jul 24 '23 at 11:18
  • I shared which function how it is saved item in dynamodb. I hope it is what you expect. @LeeHannigan – levniko Jul 24 '23 at 11:31
  • I think problem is the new MarshalMap function uses reflection to inspect the structure of the data it's marshaling. It behaves differently depending on the data's type. For example, if the data is a struct, it iterates over the fields of the struct and encodes each field individually. But ConvertToMap function is simply converts each value to the corresponding DynamoDB AttributeValue type, without trying to inspect the structure of the data. If the data is a struct, it is first converted to a map[string]interface{} using JSON encoding/decoding. @LeeHannigan – levniko Jul 24 '23 at 12:41
  • The data is very different, and you're trying to map it to a stuct which is not aligned with the new data format. In the first snippet you store a Map of maps and string values. In the second you store a map of binary values. How do you define your struct to handle the difference? – Leeroy Hannigan Jul 24 '23 at 13:16
  • 1
    This answer should help: https://stackoverflow.com/questions/72313698/custom-unmarshaling-a-struct-into-a-map-of-slices – Leeroy Hannigan Jul 24 '23 at 13:20
  • Thanks for your answers. Data is different because ConvertToMap and MarshalMap are behaving differently, I will use my own Unmarshal as your answer. But still New functions for marshal and unmarshal didn't work data saved with old functions. @LeeHannigan – levniko Jul 24 '23 at 14:32

0 Answers0