Go语言 将Map[字符串]DynamoDBAttributeValue解组到结构中

oprakyz7  于 2023-01-06  发布在  Go
关注(0)|答案(3)|浏览(176)

我正尝试使用aws-sdk-go设置一个AWS-lambda,每当向某个dynamodb表添加新的user时,就会触发这个AWS-lambda。
一切都很好,但我找不到一种方法来解组Mapmap[string]DynamoDBAttributeValue如下:

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

到一个给定的结构体,例如,一个User结构体。这里显示了一个将map[string]*dynamodb.AttributeValue反序列化到一个给定接口的例子,但是我找不到一种方法来对map[string]DynamoDBAttributeValue做同样的事情,即使这些类型看起来适合相同的目的。
map[string]DynamoDBAttributeValueevents.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上,这将是一个相当棘手的过程。

是否有一种方法可以使用与map[string]*dynamodb.AttributeValue类似的方法将来自events.DynamoDBEvent的DynamoDB记录解组到结构体中?

5kgi1eie

5kgi1eie1#

我尝试了您提供的函数,但遇到了一些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)

}
2exbekwf

2exbekwf2#

令我沮丧的是,记录中NewImage的类型不是map[string]* dynamodb.AttributeValue,因此我可以使用dynamodbattribute包。
事件.DynamoDBAttributeValue的JSON表示似乎与dynamodb.AttributeValue的JSON表示相同。
所以我尝试创建自己的DynamoDBEvent类型,并更改OldImage和NewImage的类型,以便将其编组为map[string]*dynamodb.AttributeValue,而不是map[string]事件。
它是有点难看,但它对我很有效。

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"`
}
ccrfmcuu

ccrfmcuu3#

我也发现了同样的问题,解决方法是进行一个简单的类型转换,这是可能的,因为最终lambda事件events.DynamoDBAttributeValue接收到的类型和AWS DynamoDB types.AttributeValue的SDK V2使用的类型是一样的,下面我给大家展示转换代码。

package aws_lambda

import (
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)

func UnmarshalDynamoEventsMap(
    record map[string]events.DynamoDBAttributeValue, out interface{}) error {
    asTypesMap := DynamoDbEventsMapToTypesMap(record)
    err := attributevalue.UnmarshalMap(asTypesMap, out)
    if err != nil {
        return err
    }
    return nil
}

func DynamoDbEventsMapToTypesMap(
    record map[string]events.DynamoDBAttributeValue) map[string]types.AttributeValue {
    resultMap := make(map[string]types.AttributeValue)
    for key, rec := range record {
        resultMap[key] = DynamoDbEventsToTypes(rec)
    }
    return resultMap
}

// DynamoDbEventsToTypes relates the dynamo event received by AWS Lambda with the data type that is
// used in the Amazon SDK V2 to deal with DynamoDB data.
// This function is necessary because Amazon does not provide any kind of solution to make this
// relationship between types of data.
func DynamoDbEventsToTypes(record events.DynamoDBAttributeValue) types.AttributeValue {
    var val types.AttributeValue

    switch record.DataType() {
    case events.DataTypeBinary:
        val = &types.AttributeValueMemberB{
            Value: record.Binary(),
        }
    case events.DataTypeBinarySet:
        val = &types.AttributeValueMemberBS{
            Value: record.BinarySet(),
        }
    case events.DataTypeBoolean:
        val = &types.AttributeValueMemberBOOL{
            Value: record.Boolean(),
        }
    case events.DataTypeList:
        var items []types.AttributeValue
        for _, value := range record.List() {
            items = append(items, DynamoDbEventsToTypes(value))
        }
        val = &types.AttributeValueMemberL{
            Value: items,
        }
    case events.DataTypeMap:
        items := make(map[string]types.AttributeValue)
        for k, v := range record.Map() {
            items[k] = DynamoDbEventsToTypes(v)
        }
        val = &types.AttributeValueMemberM{
            Value: items,
        }
    case events.DataTypeNull:
        val = nil
    case events.DataTypeNumber:
        val = &types.AttributeValueMemberN{
            Value: record.Number(),
        }
    case events.DataTypeNumberSet:
        val = &types.AttributeValueMemberNS{
            Value: record.NumberSet(),
        }
    case events.DataTypeString:
        val = &types.AttributeValueMemberS{
            Value: record.String(),
        }
    case events.DataTypeStringSet:
        val = &types.AttributeValueMemberSS{
            Value: record.StringSet(),
        }
    }

    return val
}

相关问题