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: expose jmespath powertools functions #736

Merged

Conversation

heitorlessa
Copy link
Contributor

@heitorlessa heitorlessa commented Oct 5, 2021

Issue #, if available: #661

Description of changes:

This exposes an internal utility in Lambda Powertools to extract and deserialize JSON data using JMESPath w/ custom functions.

This will allow customers to a) reduce their boilerplate in extracting and decoding portions of their JSON objects, b) quickly extract JSON objects from popular event sources without resorting to documentation, and c) bring their own deserializers to recursively and conditionally decode data in any format.

from aws_lambda_powertools.utilities.jmespath_utils import extract_data_from_envelope

from aws_lambda_powertools.utilities.typing import LambdaContext


def handler(event: dict, context: LambdaContext):
    payload = extract_data_from_envelope(data=event, envelope="powertools_json(body)")
    customer = payload.get("customerId")  # now deserialized
    ...

Bring Your Own Custom JMESPath functions

from aws_lambda_powertools.utilities.jmespath_utils import (
    PowertoolsFunctions, extract_data_from_envelope)
from jmespath.functions import signature


class CustomFunctions(PowertoolsFunctions):
    @signature({'types': ['string']})  # Only decode if value is a string
    def _func_special_decoder(self, s):
        return my_custom_decoder_logic(s)

custom_jmespath_options = {"custom_functions": CustomFunctions()}

def handler(event, context):
    # use the custom name after `_func_`
    extract_data_from_envelope(data=event,
                               envelope="special_decoder(body)",
                               jmespath_options=**custom_jmespath_options)
    ...

Checklist

Breaking change checklist

RFC issue #:

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

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


View rendered docs/utilities/idempotency.md
View rendered docs/utilities/jmespath_functions.md

@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 5, 2021
@boring-cyborg boring-cyborg bot added area/logger documentation Improvements or additions to documentation tests labels Oct 5, 2021
@codecov-commenter
Copy link

codecov-commenter commented Oct 5, 2021

Codecov Report

Merging #736 (348aee2) into develop (dec3c88) will decrease coverage by 0.16%.
The diff coverage is 33.33%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #736      +/-   ##
===========================================
- Coverage    99.93%   99.77%   -0.17%     
===========================================
  Files          116      117       +1     
  Lines         4914     4922       +8     
  Branches       271      271              
===========================================
  Hits          4911     4911              
- Misses           1        9       +8     
  Partials         2        2              
Impacted Files Coverage Δ
aws_lambda_powertools/logging/logger.py 98.43% <ø> (ø)
...a_powertools/utilities/jmespath_utils/envelopes.py 0.00% <0.00%> (ø)
...da_powertools/utilities/feature_flags/appconfig.py 100.00% <100.00%> (ø)
...wertools/utilities/idempotency/persistence/base.py 99.36% <100.00%> (ø)
...da_powertools/utilities/jmespath_utils/__init__.py 100.00% <100.00%> (ø)
...ambda_powertools/utilities/validation/validator.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dec3c88...348aee2. Read the comment docs.

@heitorlessa heitorlessa added the feature New feature or functionality label Oct 5, 2021
@heitorlessa heitorlessa changed the title refactor: expose jmespath powertools functions feat: expose jmespath powertools functions Oct 5, 2021
@heitorlessa heitorlessa merged commit af36fb5 into aws-powertools:develop Oct 5, 2021
@heitorlessa heitorlessa deleted the feat/expose-jmespath-functions branch October 5, 2021 11:12
@heitorlessa heitorlessa removed the documentation Improvements or additions to documentation label Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants