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

feat(event_source): deserialize dynamodb new_image, old_image as dict #1580

Closed
wants to merge 25 commits into from
Closed

feat(event_source): deserialize dynamodb new_image, old_image as dict #1580

wants to merge 25 commits into from

Conversation

shanab
Copy link
Contributor

@shanab shanab commented Oct 10, 2022

Issue number: #1432

Summary

Add DynamoDBImageDeserializer class to data classes. The class can be used to parse DynamoDB StreamRecord's old_image and new_image properties of type Dict[str, AttrValue] to Dict with Python types.

Changes

  • Add DynamoDBImageDeserializer class to data classes.
  • Add unit test.

User experience

Users will be able to convert a StreamRecord's complex image type into a Python dictionary with Python types; without needing to recurse through the data.

Example:

    data = {
        "Keys": {"key1": {"attr1": "value1"}},
        "NewImage": {
            "Name": {"S": "Joe"},
            "Age": {"N": "35"},
            "TypesMap": {
                "M": {
                    "string": {"S": "value"},
                    "number": {"N": "100"},
                    "bool": {"BOOL": True},
                    "dict": {"M": {"key": {"S": "value"}}},
                    "stringSet": {"SS": ["item1", "item2"]},
                    "numberSet": {"NS": ["100", "200", "300"]},
                    "byteSet": {"BS": byte_list},
                    "list": {"L": [{"S": "item1"}, {"N": "3.14159"}, {"BOOL": False}]},
                    "null": {"NULL": True},
                },
            },
        },
    }
    record = StreamRecord(data)
    deserializer = DynamoDBImageDeserializer()
    new_image = deserializer.deserialize(record.new_image)
    assert new_image == {
        "Name": "Joe",
        "Age": "35",
        "TypesMap": {
            "string": "value",
            "number": "100",
            "bool": True,
            "dict": {"key": "value"},
            "stringSet": {"item1", "item2"},
            "numberSet": {"100", "200", "300"},
            "byteSet": set(byte_list),
            "list": ["item1", "3.14159", False],
            "null": None,
        },
    }

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

No
RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@shanab shanab requested a review from a team as a code owner October 10, 2022 10:45
@shanab shanab requested review from rubenfonseca and removed request for a team October 10, 2022 10:45
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 10, 2022
@boring-cyborg boring-cyborg bot added the tests label Oct 10, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Oct 10, 2022

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our AWS Lambda Powertools Discord: Invite link

@shanab shanab changed the title Feature/dynamodb image deserializer feat/dynamodb-image-deserializer Oct 10, 2022
@shanab
Copy link
Contributor Author

shanab commented Oct 10, 2022

Do I need to update the public docs for this change?

@leandrodamascena
Copy link
Contributor

Do I need to update the public docs for this change?

Hi @shanab! First of all thank you so much for submitting this PR and helping to improve this project!

It looks like we have errors in the pipeline. Could you check for errors and fix them? We suggest running make pr before push the code and checking that everything is ok.

@leandrodamascena leandrodamascena requested review from leandrodamascena and removed request for rubenfonseca October 10, 2022 12:20
@leandrodamascena leandrodamascena self-assigned this Oct 10, 2022
@shanab
Copy link
Contributor Author

shanab commented Oct 10, 2022

Thanks @leandrodamascena! I've fixed the typing errors.

@leandrodamascena
Copy link
Contributor

Thanks @leandrodamascena! I've fixed the typing errors.

Awesome @shanab! I'll review this PR this week and let you know if we need to fix anything, ok?

BTW, we can update the documentation for you, but let us know if you want to update this and push the code 🥇 .

Thank you!

@heitorlessa
Copy link
Contributor

heitorlessa commented Oct 12, 2022

@shanab a few questions if I may as I'm only seeing this now:

  • What would you expect customers to do with TypesMap key? Traverse, pick the type they're interested in, or something else?

  • Since it's a Map, what's the typical customer expectation on deserialisation? Get it back as a Dict?

  • Given we're releasing v2 and we can make a breaking change, would it be easier to get rid of Attribute Value and store the values already deserialised with boto3 TypeDeserializer?

I think what I'm missing is the customer experience after you get the data deserialised like that vs a Dict and its values - v2 is just around the corner, we might simplify maintenance and perf by making any necessary breaking change.

Thanks a lot

dependabot bot and others added 3 commits October 13, 2022 20:16
Bumps [mypy-boto3-ssm](https://github.com/youtype/mypy_boto3_builder) from 1.24.81 to 1.24.90.
- [Release notes](https://github.com/youtype/mypy_boto3_builder/releases)
- [Commits](https://github.com/youtype/mypy_boto3_builder/commits)

---
updated-dependencies:
- dependency-name: mypy-boto3-ssm
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@github-actions github-actions bot added the feature New feature or functionality label Oct 17, 2022
@heitorlessa
Copy link
Contributor

heitorlessa commented Oct 17, 2022

Thanks for accepting a 1:1 call on this @shanab ! Here's a quick recap on the conditions we could make it this work if we can get it done until Thursday to not impact V2 launch:

  • Update new_image, old_image, and keys properties to return a deserialized Dict - Code here
  • Keep Decimals as str to continue compatibility with current implementation, and to support precisions higher than 38 digits (IIRC DynamoDB doesn't by default)
  • Let's not cache deserialized data as we might be dealing with thousands of records and we need to keep it memory efficient (we already use a generator for .records)
  • Update the docs to describe how to use that data (now being dict) - Docs here
  • Switch your base to v2 - it might be easier to create a new branch from v2 given the amount of conflicts right now
  • Ping me directly if you need any help - we can do peer review live to get this faster, and I can write the Upgrade Guide too.

@shanab shanab changed the base branch from develop to v2 October 18, 2022 08:07
@pull-request-size pull-request-size bot removed the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 18, 2022
@boring-cyborg boring-cyborg bot added dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation github-templates internal Maintenance changes labels Oct 18, 2022
@pull-request-size pull-request-size bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Oct 18, 2022
@shanab shanab changed the base branch from v2 to develop October 18, 2022 08:07
@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Oct 18, 2022
@shanab shanab changed the base branch from develop to v2 October 18, 2022 11:12
@pull-request-size pull-request-size bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 18, 2022
@shanab
Copy link
Contributor Author

shanab commented Oct 18, 2022

I've agreed with @rubenfonseca to move to a new PR, as the diff just blew up once I changed the base branch from develop to v2.
I'll link the new PR here once I do that.

@boring-cyborg boring-cyborg bot added the github-actions Pull requests that update Github_actions code label Oct 19, 2022
@shanab shanab closed this by deleting the head repository Oct 19, 2022
@shanab
Copy link
Contributor Author

shanab commented Oct 19, 2022

Link to the new PR: #1619

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation feature New feature or functionality github-actions Pull requests that update Github_actions code github-templates internal Maintenance changes size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: support DynamoDB image values to dict with built-in Python types
7 participants