I am trying to configure AWS lambda using aws-sdk-go , which starts whenever a new user added to a specific dynamodb table.
Everything works fine, but I canβt find a way to untie the map[string]DynamoDBAttributeValue like:
"name": { "S" : "John" }, residence_address": { "M": { "address": { "S": "some place" } } }
For a given structure, for example, a user struct. Here is an example of unsmarhaling a map[string]*dynamodb.AttributeValue in a given interface, but I cannot find a way to do the same with map[string]DynamoDBAttributeValue , although these types seem to be for the same purpose.
map[string]DynamoDBAttributeValue returns events.DynamoDBEvents from the github.com/aws/aws-lambda-go/events package. This is my code:
package handler import ( "context" "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-sdk-go-v2/service/dynamodb/dynamodbattribute" "github.com/aws/aws-sdk-go-v2/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) } } }
Of course, I can combine record.Change.NewImage in JSON and cancel it back to this structure, but then I will have to manually initialize the dynamoUser attributes, starting with the latter.
Or I could even write a function that parses map[string]DynamoDBAttributeValue on map[string]*dynamodb.AttributeValue like:
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, } }
And then just use dynamodbattribute.UnmarshalMap , but on events.DataTypeMap it will be a rather complicated process.
Is there a way I can decouple the DynamoDB entry from events.DynamoDBEvent into a structure with the same method shown for map[string]*dynamodb.AttributeValue ?