Created
August 29, 2022 22:52
-
-
Save Greyeye/c993da8169a89dba76204131b8d2ce8c to your computer and use it in GitHub Desktop.
dynamoDB stream event trigger handling code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func handler(ctx context.Context, ev events.S3Event) { | |
// need to handle PUT, DELETE, UPDATE, separately | |
for _, record := range ev.Records { | |
switch record.EventName { | |
case "INSERT": | |
fmt.Printf("%+v\n", record) | |
var rr localTypes.ActivityEventsTable | |
UnmarshalStreamImage(record.Change.NewImage, rr) | |
fmt.Printf("%+v\n", rr) | |
case "MODIFY": | |
case "DELETE": | |
fmt.Printf("delete event") | |
} | |
} | |
} | |
// UnmarshalStreamImage converts events.DynamoDBAttributeValue to struct | |
func UnmarshalStreamImage(attribute map[string]events.DynamoDBAttributeValue, out interface{}) error { | |
dbAttrMap := make(map[string]types.AttributeValue) | |
for k, v := range attribute { | |
var dbAttr types.AttributeValue | |
bytes, marshalErr := v.MarshalJSON() | |
if marshalErr != nil { | |
return marshalErr | |
} | |
json.Unmarshal(bytes, &dbAttr) | |
dbAttrMap[k] = dbAttr | |
} | |
return attributevalue.UnmarshalMap(dbAttrMap, out) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment