0

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

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

4

2 回答 2

1

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

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

我绝望地加入了dynamodbav 结构标签。解组时似乎不需要它们。现在我返回地图值,而不是整个地图result.Item我从来不知道如何使用UnmarshalMap。:/

err = dynamodbattribute.Unmarshal(result.Item["video_lite"], &t)
于 2017-06-13T03:30:43.070 回答
0

可能您可以将结构修改为:

  type Video struct {
      video_lite struct {
        name struct {
           N *string
           S *string
        }
        id struct {
           N *string
           S *string
        }
     }
    }
于 2017-06-12T11:10:58.240 回答