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

Feature request: support DynamoDB image values to dict with built-in Python types #1432

Closed
1 of 2 tasks
shanab opened this issue Aug 7, 2022 · 10 comments
Closed
1 of 2 tasks
Assignees
Labels
feature-request feature request v2

Comments

@shanab
Copy link
Contributor

shanab commented Aug 7, 2022

Use case

When using DynamoDBRecord class, the values for old_image and new_image are represented as Dict[str, AttributeValue]. This representation is sufficient when the data parsed is simple and does not contain nested maps/lists.
But when the data parsed contains nested maps/lists, then it becomes hard to get to the data that AttributeValue represents.
One would need to recursively parse the AttributeValue objects in the nested dicts to get to the underlying data.

Solution/User Experience

Using a deserializer similar to the TypeDeserializer provided by boto3 (link to code), one would be able to convert image to a Python dict where the values would be built-in Python types.

Alternative solutions

No response

Acknowledgment

@shanab shanab added feature-request feature request triage Pending triage from maintainers labels Aug 7, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Aug 7, 2022

Thanks for opening your first issue here! We'll come back to you as soon as we can.

@heitorlessa
Copy link
Contributor

heitorlessa commented Aug 7, 2022 via email

@heitorlessa heitorlessa added area/event_sources and removed triage Pending triage from maintainers labels Aug 7, 2022
@ran-isenberg
Copy link
Contributor

ran-isenberg commented Aug 11, 2022

@shanab
did you try using the parser? you can specify models for the images and it all gets parsed nicely.
https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/parser/#built-in-envelopes
There's a DynamoDBStreamEnvelope class.

It seems like the data classes features is trying to implement parsing features IMO.

@shanab
Copy link
Contributor Author

shanab commented Aug 16, 2022

@ran-isenberg the problem in our case was that the service was not using Pydantic, and we did not want to introduce it only to parse the DynamoDB record.
I agree that there is an overlap between data classes and Parser, but IMO data classes should still allow traversing the fields of a record.
Also looking at the data classes docs, one of the key features mentioned is "Helper functions for decoding/deserializing nested fields"

@heitorlessa
Copy link
Contributor

hey @shanab Ahmed, checking in to see if you still have the bandwidth to contribute as you already had the code working.

Thank you!

@shanab
Copy link
Contributor Author

shanab commented Oct 3, 2022

@heitorlessa yes, I should have time this week to have a first draft. Sorry for leaving this open until now!

@jplock
Copy link

jplock commented Oct 10, 2022

What about using boto3's TypeDeserializer ?

@shanab
Copy link
Contributor Author

shanab commented Oct 11, 2022

What about using boto3's TypeDeserializer ?

That can be an option, but to my knowledge then one would not be able to do the following:

@event_source(data_class=DynamoDBStreamEvent)
def lambda_handler(event: DynamoDBStreamEvent, context):
    pass

because you'd need the raw Python dictionary for NewImage and OldImage to be able to use TypeDeserializer, and the event won't expose that.

@heitorlessa
Copy link
Contributor

Released in V2 now ;) https://github.com/awslabs/aws-lambda-powertools-python/releases/tag/v2.0.0

@github-actions
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request feature request v2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants