如何将dynamodb结果解析为整数?

时间:2017-06-12 09:10:46

标签: amazon-web-services aws-sdk-go

我无法解开一个相当简单的数据结构:

"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为什么不起作用?

2 个答案:

答案 0 :(得分:0)

您可以将结构修改为:

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

答案 1 :(得分: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)