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(idempotency): add support to custom serialization/deserialization on idempotency decorator #2951

Conversation

aradyaron
Copy link
Contributor

@aradyaron aradyaron commented Aug 13, 2023

Issue number: #2886

Summary

Changes

  • New parameter in idempotent function handler : output_serializer
  • New base class for dict serializer, this allows further internal changes without breaking interfaces
  • CustomDict implementation for serializer
  • Pydantic implementation for serializer
  • Tests for both serializaer types
  • Examples for both serializer types
  • Documentation regarding the new output_serializer parameter

User experience

  • Users can now add output serializer on idempotent function decorator in order to use custom Dto types on their functions
from aws_lambda_powertools.utilities.idempotency import (
    DynamoDBPersistenceLayer,
    IdempotencyConfig,
    idempotent_function,
)
from aws_lambda_powertools.utilities.idempotency.serialization.pydantic import PydanticSerializer
from aws_lambda_powertools.utilities.parser import BaseModel
from aws_lambda_powertools.utilities.typing import LambdaContext

dynamodb = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
config = IdempotencyConfig(event_key_jmespath="order_id")  # see Choosing a payload subset section


class OrderItem(BaseModel):
    sku: str
    description: str


class Order(BaseModel):
    item: OrderItem
    order_id: int


class OrderOutput(BaseModel):
    order_id: int


@idempotent_function(
    data_keyword_argument="order",
    config=config,
    persistence_store=dynamodb,
    output_serializer=PydanticSerializer,
)
# order output is deduced from return type
def deduced_order_output_serializer(order: Order) -> OrderOutput:
    return OrderOutput(order_id=order.order_id)


def lambda_handler(event: dict, context: LambdaContext):
    config.register_lambda_context(context)  # see Lambda timeouts section
    order_item = OrderItem(sku="fake", description="sample")
    order = Order(item=order_item, order_id=1)

    # `order` parameter must be called as a keyword argument to work
    deduced_order_output_serializer(order=order)

Checklist

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

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.

@aradyaron aradyaron requested a review from a team as a code owner August 13, 2023 07:06
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation tests labels Aug 13, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Aug 13, 2023

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 Powertools for AWS Lambda Discord: Invite link

@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 13, 2023
@aradyaron aradyaron changed the title Feat/idempotency output serializer feat(idempotency): Support of custom serizialization/deserisialization (object -> dict) on idempotency decorator 'idempotent_function' Aug 13, 2023
@github-actions github-actions bot added the feature New feature or functionality label Aug 14, 2023
@github-actions
Copy link
Contributor

No related issues found. Please ensure there is an open issue related to this change to avoid significant delays or closure.

@github-actions github-actions bot added do-not-merge need-issue PRs that are missing related issues labels Aug 14, 2023
@leandrodamascena leandrodamascena removed do-not-merge need-issue PRs that are missing related issues labels Aug 14, 2023
@leandrodamascena
Copy link
Contributor

Hi @aradyaron! Thanks so much for sending this PR, it's absolutely awesome to add this support in the Idempotency utility without breaking changes.

We will start reviewing this PR by tomorrow and hope to merge it as soon as possible.

🚀

@codecov-commenter
Copy link

codecov-commenter commented Aug 14, 2023

Codecov Report

Patch coverage: 92.04% and project coverage change: -0.06% ⚠️

Comparison is base (4412736) 96.56% compared to head (cef68c7) 96.51%.
Report is 3 commits behind head on develop.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the GitHub App Integration for your organization. Read more.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2951      +/-   ##
===========================================
- Coverage    96.56%   96.51%   -0.06%     
===========================================
  Files          175      180       +5     
  Lines         7825     7909      +84     
  Branches      1476     1491      +15     
===========================================
+ Hits          7556     7633      +77     
- Misses         217      221       +4     
- Partials        52       55       +3     
Files Changed Coverage Δ
...ls/utilities/idempotency/serialization/pydantic.py 81.81% <81.81%> (ø)
...s/utilities/idempotency/serialization/dataclass.py 89.47% <89.47%> (ø)
...rtools/utilities/idempotency/serialization/base.py 91.66% <91.66%> (ø)
...ws_lambda_powertools/utilities/idempotency/base.py 98.78% <100.00%> (+0.11%) ⬆️
...bda_powertools/utilities/idempotency/exceptions.py 100.00% <100.00%> (ø)
...da_powertools/utilities/idempotency/idempotency.py 100.00% <100.00%> (ø)
...utilities/idempotency/serialization/custom_dict.py 100.00% <100.00%> (ø)
...tools/utilities/idempotency/serialization/no_op.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@leandrodamascena leandrodamascena changed the title feat(idempotency): Support of custom serizialization/deserisialization (object -> dict) on idempotency decorator 'idempotent_function' feat(idempotency): add support to custom serialization/deserialization on idempotency decorator Aug 14, 2023
@rubenfonseca
Copy link
Contributor

Looking at this now

@leandrodamascena leandrodamascena requested review from sthulb and removed request for rubenfonseca September 4, 2023 08:25
@leandrodamascena
Copy link
Contributor

I'm working with @sthulb to review the documentation and merge this PR. 🚀

docs/utilities/idempotency.md Outdated Show resolved Hide resolved
@sonarcloud
Copy link

sonarcloud bot commented Sep 5, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 6 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@leandrodamascena leandrodamascena self-requested a review September 5, 2023 11:50
@sthulb sthulb requested review from rubenfonseca and removed request for rubenfonseca September 5, 2023 11:55
@sthulb sthulb dismissed rubenfonseca’s stale review September 5, 2023 11:57

No longer required.

@leandrodamascena leandrodamascena merged commit 8534b16 into aws-powertools:develop Sep 5, 2023
14 checks passed
@boring-cyborg
Copy link

boring-cyborg bot commented Sep 5, 2023

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Sep 8, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Sep 8, 2023

This is now released under 2.24.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature New feature or functionality size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests
Projects
None yet
5 participants