2017-06-12 47 views
0

我無法來解讀一個相當簡單的數據結構:如何將dynamodb結果解析爲一個整數?

"video_lite": { "id": 1573, "name": "Blade Runner (Movie)" } 

這裏是我的代碼無法正常工作:

package main 

import (
    "fmt" 

    "github.com/aws/aws-sdk-go/aws" 
    "github.com/aws/aws-sdk-go/aws/session" 
    "github.com/aws/aws-sdk-go/service/dynamodb" 
    "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" 
) 

type Video struct { 
    video_lite struct { 
     name string 
     id int 
    } 
} 

func check(err error) { 
    if err != nil { 
     panic(err.Error()) 
    } 
} 

func main() { 

    svc := dynamodb.New(session.New()) 

    input := &dynamodb.GetItemInput{ 
     Key:     map[string]*dynamodb.AttributeValue{"uuid": {S: aws.String("d610e853-5222-462c-9bb3-26ff5aa86e9d")}}, 
     ProjectionExpression: aws.String("video_lite"), 
     TableName:   aws.String("staging_video_ingestion"), 
    } 

    result, err := svc.GetItem(input) 
    check(err) 

    fmt.Printf("%+v\n", result.Item) 
    var t Video 

    err = dynamodbattribute.UnmarshalMap(result.Item, &t) 
    check(err) 

    fmt.Printf("%+v\n", t) 

} 

輸出看起來像:

map[video_lite:{ 
    M: { 
    id: { 
     N: "1573" 
    }, 
    name: { 
     S: "Bladerunner (Movie)" 
    } 
    } 
}] 
{video_lite:{name: id:0}} 

爲什麼UnmarshalMap不工作?

回答

0

也許你可以修改你的結構到這一點:

type Video struct { 
     video_lite struct { 
     name struct { 
      N *string 
      S *string 
     } 
     id struct { 
      N *string 
      S *string 
     } 
    } 
    } 
+0

試過'{video_lite:{名稱:{N: S:} ID:{N: S:}}}'是輸出。也嘗試下載'video_lite'並得到'{name:{N: S:} id:{N: S:}}: – hendry

0

Unmarshal,而不是UnmarshalMap似乎更容易使用。

type Video struct { 
    Name string `dynamodbav:"name"` 
    ID int `dynamodbav:"id"` 
} 

我在絕望的行爲中投擲了dynamodbav struct tags。看來,他們不需要解組時。現在我返回地圖值,而不是整個地圖result.Item,我從來沒有想過如何去與UnmarshalMap。 :/

err = dynamodbattribute.Unmarshal(result.Item["video_lite"], &t)