Last active
January 13, 2020 19:18
-
-
Save mageddo/9337f2c4790e395949c5ebc32bb1b546 to your computer and use it in GitHub Desktop.
Parse aws dynamo events.DynamoDBAttributeValue to Struct
This file contains hidden or 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 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)) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://intellipaat.com/community/16713/unmarshal-map-string-dynamodbattributevalue-into-a-struct