Description
Context: Separating this out of #1657. With the change in 3.0.0 to separate the type definitions in Amazon.Lambda.DynamoDBEvents from the equivalent types in the AWS SDK, users may now need to manually convert from the event types to SDK types or vice-versa. For #1657 in versions 3.1.0 and 3.1.1, we addressed the Event -> SDK direction by adding ToJson
. This issue is now tracking the opposite direction, from SDK (or JSON) -> Event.
We have a scenario (testing) where we are building a DynamoDB stream event based of an object - see code
public static DynamoDBEvent CreateDynamoDbEvent(OperationType type, object newObj, object prevObj)
{
return new DynamoDBEvent
{
Records = new List<DynamodbStreamRecord>
{
new()
{
Dynamodb = new DynamoDBEvent.StreamRecord
{
NewImage = ToDynamoDbAttributes(newObj),
OldImage = ToDynamoDbAttributes(prevObj)
},
EventName = new OperationType(type)
}
}
};
}
private static Dictionary<string, DynamoDBEvent.AttributeValue> ToDynamoDbAttributes(object obj)
{
if (obj == null) return null;
var attributes = Document.FromJson(obj.ToJson()).ToAttributeMap();
return attributes;
}
Now I want to return an Dictionary<string, DynamoDBEvent.AttributeValue, but not really sure how I would go about it now that ToAttributeMap() returns a different AttributeValue. What's the best approach here?
Originally posted by @psdanielhosseini in #1657 (comment)
Any workaround using AWSSDK.DynamoDBv2 would be fine, e.g. JSON -> DynamoDBv2.Document -> Dictionary<string, DynamoDBEvent.AttributeValue>.
I've tried to get a "DynamoDB" JSON from Document, so I could deserialize it to Dictionary<string, DynamoDBEvent.AttributeValue>. Unfortunately, I haven't found an easy way to do that.
Originally posted by @Dreamescaper in #1657 (comment)