Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize DynamoDBEvent Objects Easily #1700

Open
ashovlin opened this issue Mar 14, 2024 · 0 comments
Open

Initialize DynamoDBEvent Objects Easily #1700

ashovlin opened this issue Mar 14, 2024 · 0 comments
Labels
feature-request A feature should be added or improved. module/lambda-client-lib p1 This is a high priority issue queued

Comments

@ashovlin
Copy link
Member

ashovlin commented Mar 14, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved. module/lambda-client-lib p1 This is a high priority issue queued
Projects
None yet
Development

No branches or pull requests

2 participants