Cant unmarshall dynamodb attribute

眉间皱痕 提交于 2021-02-09 11:47:22

问题


I'm experimenting with AWS-SDK-GO with the DynamoDB API...

I'm trying to query the db and return a string. But I'm having some issues unmarshelling the return value....

struct

type Item struct {
     slug string
     destination string
}

query function

input := &dynamodb.GetItemInput{
    Key: map[string]*dynamodb.AttributeValue{
        "slug": {
            S: aws.String(slug),
        },
    },
    TableName: db.TableName,
}

result, err := db.dynamo.GetItem(input)
if err != nil {
    return "",  err
}

n := Item{}
err = dynamodbattribute.UnmarshalMap(result.Item, &n)

if err != nil {
    log.Printf("Failed to unmarshal Record, %v", err)
    return "", err
}
log.Printf("dump %+v", n)
log.Printf("echo %s", n.slug)
log.Printf("echo %s", n.destination)
log.Printf("orig %v", result.Item)

result

2017/10/11 14:21:34 dump {slug: destination:}
2017/10/11 14:21:34 echo 
2017/10/11 14:21:34 echo 
2017/10/11 14:21:34 orig map[destination:{
  S: "http://example.com"
} slug:{
  S: "abcde"
}]

Why is Item being returned empty?

I've tried to look everywhere but find no solution....


回答1:


I found this issue and seems to be related, unmarsheling a specific attribute of the struct seems to do it.

https://github.com/aws/aws-sdk-go/issues/850

Example

var item Item
if err = dynamodbattribute.Unmarshal(result.Item["destination"], &item.destination); err != nil {
    log.Printf("UnmarshalMap(GetItem response) err=%q", err)
}



回答2:


I am not sure whether you have tried this. I think if you can change the struct as mentioned below, it may resolve the problem.

I assumed that both slug and destination are defined/saved as String attribute in DynamoDB table.

type Item struct {
     Slug string`json:"slug"`
     Destination string`json:"destination"`
}

Change the print to:-

log.Printf("echo %s", n.Slug)
log.Printf("echo %s", n.Destination)


来源:https://stackoverflow.com/questions/46688165/cant-unmarshall-dynamodb-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!