2017-10-11 7 views
0

は私が... DynamoDBのAPIでAWS-SDKは-GOを試しています。しかし、私は....カントアンマーシャリングDynamoDBの属性

構造体の戻り値をunmarshellingいくつかの問題を抱えている

type Item struct { 
    slug string 
    destination string 
} 

クエリ機能

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) 

結果

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" 
}] 

なぜ項目が空返されています?

私はこの問題を発見し、関連しているように見える、構造体の特定の属性は、それを行うようだunmarsheling ....どこでも見えるが、何の解決策を見つけるしないために

+0

DynamoDBのテーブルの属性先のデータ型は何ですか? – notionquest

+0

両方の文字列、構造体のように – Pedro

+0

以下の解決策を試しましたか? – notionquest

答えて

0

を試してみました。

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

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

私はあなたがこれを試してみましたかどうかを確認していません。私はあなたが以下に述べるように構造体を変更することができれば、問題を解決するかもしれないと思います。

slugdestinationは、DynamoDBテーブルのString属性として定義/保存されているものとします。

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

変更印刷に: -

log.Printf("echo %s", n.Slug) 
log.Printf("echo %s", n.Destination) 
関連する問題