将map [string] DynamoDBAttributeValue解组成结构

时间:2018-03-06 11:16:13

标签: go aws-lambda amazon-dynamodb aws-sdk-go

我正在尝试使用aws-sdk-go设置AWS-lambda,只要将新的user添加到某个dynamodb表,就会触发该事件。

一切都运转正常,但我找不到解开地图map[string]DynamoDBAttributeValue的方法,如:

{
    "name": {
        "S" : "John"
    },
    "residence_address": {
        "M": {
            "address": {
                "S": "some place"
            }
        }
    }
}

对于给定的结构,例如,User结构。 Here显示了一个将map[string]*dynamodb.AttributeValue取消绑定到给定界面的示例,但我找不到使用map[string]DynamoDBAttributeValue执行相同操作的方法,即使这些类型似乎相同目的。

来自map[string]DynamoDBAttributeValue包的events.DynamoDBEvents返回

github.com/aws/aws-lambda-go/events。这是我的代码:

package handler

import (
    "context"
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
    "github.com/aws/aws-sdk-go/service/dynamodb"
)

func HandleDynamoDBRequest(ctx context.Context, e events.DynamoDBEvent) {

    for _, record := range e.Records {

        if record.EventName == "INSERT" {

            // User Struct
            var dynamoUser model.DynamoDBUser

            // Of course this can't be done for incompatible types
            _ := dynamodbattribute.UnmarshalMap(record.Change.NewImage, &dynamoUser)
        }

    }

}

当然,我可以将record.Change.NewImage封送到JSON并将其解组回给定的结构,但是,我必须从后者开始手动初始化dynamoUser属性。

或者我甚至可以编写一个将map[string]DynamoDBAttributeValue解析为map[string]*dynamodb.AttributeValue的函数,如:

func getAttributeValueMapFromDynamoDBStreamRecord(e events.DynamoDBStreamRecord) map[string]*dynamodb.AttributeValue {
    image := e.NewImage
    m := make(map[string]*dynamodb.AttributeValue)
    for k, v := range image {
        if v.DataType() == events.DataTypeString {
            s := v.String()
            m[k] = &dynamodb.AttributeValue{
                S : &s,
            }
        }
        if v.DataType() == events.DataTypeBoolean {
            b := v.Boolean()
            m[k] = &dynamodb.AttributeValue{
                BOOL : &b,
            }
        }
        // . . .
        if v.DataType() == events.DataTypeMap {
            // ?
        }
    }
    return m
}

然后只使用dynamodbattribute.UnmarshalMap,但在events.DataTypeMap上,这将是一个非常棘手的过程。

我是否有办法将events.DynamoDBEvent的DynamoDB记录解组为结构,并为map[string]*dynamodb.AttributeValue显示类似的方法?

3 个答案:

答案 0 :(得分:4)

我尝试了你提供的功能,我遇到了events.DataTypeList的一些问题,所以我设法编写了以下功能:

// UnmarshalStreamImage converts events.DynamoDBAttributeValue to struct
func UnmarshalStreamImage(attribute map[string]events.DynamoDBAttributeValue, out interface{}) error {

    dbAttrMap := make(map[string]*dynamodb.AttributeValue)

    for k, v := range attribute {

        var dbAttr dynamodb.AttributeValue

        bytes, marshalErr := v.MarshalJSON(); if marshalErr != nil {
            return marshalErr
        }

        json.Unmarshal(bytes, &dbAttr)
        dbAttrMap[k] = &dbAttr
    }

    return dynamodbattribute.UnmarshalMap(dbAttrMap, out)

}

答案 1 :(得分:0)

目前我设法通过实现我在问题中提到的功能来解决这个问题。

// May take either `map[string]DynamoDBAttributeValue` 
// or `DynamoDBAttributeValue` as input.
func EventStreamToMap(attribute interface{}) map[string]*dynamodb.AttributeValue {

    // Map to be returned
    m := make(map[string]*dynamodb.AttributeValue)

    tmp := make(map[string]events.DynamoDBAttributeValue)

    switch t := attribute.(type) {
    case map[string]events.DynamoDBAttributeValue:
        tmp = t
    case events.DynamoDBAttributeValue:
        tmp = t.Map()
    }

    for k, v := range tmp {
        switch v.DataType() {
        case events.DataTypeString:
            s := v.String()
            m[k] = &dynamodb.AttributeValue{
                S : &s,
            }
        case events.DataTypeBoolean:
            b := v.Boolean()
            m[k] = &dynamodb.AttributeValue{
                BOOL : &b,
            }
        // case events.SomeOtherType:
        //     ...
        case events.DataTypeMap:
            m[k] = &dynamodb.AttributeValue{
                M : EventStreamToMap(v),
            }
        }
    }
    return m
}

但我仍然愿意接受其他实施。

答案 2 :(得分:0)

我很沮丧,记录中的NewImage类型不是map [string] * dynamodb.AttributeValue所以我可以使用dynamodbattribute包。

events.DynamoDBAttributeValue的JSON表示似乎与dynamodb.AttributeValue的JSON表示相同。

所以我尝试创建自己的DynamoDBEvent类型并更改了OldImage和NewImage的类型,因此它将被编组到map [string] * dynamodb.AttributeValue而不是map [string] events.DynamoDBAttributeValue

它有点难看但它对我有用。

package main

import (
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/aws/aws-sdk-go/service/dynamodb"
    "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
    "fmt"
)

func main() {

    lambda.Start(lambdaHandler)
}

// changed type of event from: events.DynamoDBEvent to DynamoDBEvent (see below)
func lambdaHandler(event DynamoDBEvent) error {

    for _, record := range event.Records {

        change := record.Change
        newImage := change.NewImage // now of type: map[string]*dynamodb.AttributeValue

        var item IdOnly
        err := dynamodbattribute.UnmarshalMap(newImage, &item)
        if err != nil {
            return err
        }

        fmt.Println(item.Id)
    }

    return nil
}

type IdOnly struct {
    Id string `json:"id"`
}

type DynamoDBEvent struct {
    Records []DynamoDBEventRecord `json:"Records"`
}

type DynamoDBEventRecord struct {
    AWSRegion      string                       `json:"awsRegion"`
    Change         DynamoDBStreamRecord         `json:"dynamodb"`
    EventID        string                       `json:"eventID"`
    EventName      string                       `json:"eventName"`
    EventSource    string                       `json:"eventSource"`
    EventVersion   string                       `json:"eventVersion"`
    EventSourceArn string                       `json:"eventSourceARN"`
    UserIdentity   *events.DynamoDBUserIdentity `json:"userIdentity,omitempty"`
}

type DynamoDBStreamRecord struct {
    ApproximateCreationDateTime events.SecondsEpochTime             `json:"ApproximateCreationDateTime,omitempty"`
    // changed to map[string]*dynamodb.AttributeValue
    Keys                        map[string]*dynamodb.AttributeValue `json:"Keys,omitempty"`
    // changed to map[string]*dynamodb.AttributeValue
    NewImage                    map[string]*dynamodb.AttributeValue `json:"NewImage,omitempty"`
    // changed to map[string]*dynamodb.AttributeValue
    OldImage                    map[string]*dynamodb.AttributeValue `json:"OldImage,omitempty"`
    SequenceNumber              string                              `json:"SequenceNumber"`
    SizeBytes                   int64                               `json:"SizeBytes"`
    StreamViewType              string                              `json:"StreamViewType"`
}