Skip to content

Instantly share code, notes, and snippets.

@mageddo
Last active January 13, 2020 19:18
Show Gist options
  • Select an option

  • Save mageddo/9337f2c4790e395949c5ebc32bb1b546 to your computer and use it in GitHub Desktop.

Select an option

Save mageddo/9337f2c4790e395949c5ebc32bb1b546 to your computer and use it in GitHub Desktop.
Parse aws dynamo events.DynamoDBAttributeValue to Struct
func UnmarshalEvent(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
}
if err := json.Unmarshal(bytes, &dbAttr); err != nil {
return errors.WithMessage(err, fmt.Sprintf("can't umarshal: %s", attribute))
}
dbAttrMap[k] = &dbAttr
}
return dynamodbattribute.UnmarshalMap(dbAttrMap, out)
}
func main(){
var compositionEvent events.DynamoDBAttributeValue
var composition []SomeStruct
switch compositionEvent.DataType() {
case events.DataTypeString:
compositionJson := compositionEvent.String()
if err := json.Unmarshal([]byte(compositionJson), &composition); err == nil {
return &composition
} else {
panic(errors.WithMessage(err, fmt.Sprintf("can't parse composition: %s, event=%s", compositionJson, compositionEvent)))
}
case events.DataTypeList:
for _, v := range compositionEvent.List() {
c := new(SomeStruct)
fmt.Println(UnmarshalEvent(v.Map(), &c))
}
}
}
@mageddo

mageddo commented Jan 13, 2020

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment