Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant unmarshall dynamodb attribute

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....

like image 514
Pedro Avatar asked Oct 16 '25 03:10

Pedro


1 Answers

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)
like image 140
notionquest Avatar answered Oct 17 '25 16:10

notionquest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!