From c284d4ab77140dce0b90ccb8515fefd8ee0194df Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Wed, 20 Dec 2023 13:32:25 +0100 Subject: [PATCH] refactor: split lambda in partial + lambda to ease maintenance --- .../utilities/_data_masking/base.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/aws_lambda_powertools/utilities/_data_masking/base.py b/aws_lambda_powertools/utilities/_data_masking/base.py index d6bf8828736..824d16b34cf 100644 --- a/aws_lambda_powertools/utilities/_data_masking/base.py +++ b/aws_lambda_powertools/utilities/_data_masking/base.py @@ -1,4 +1,5 @@ from __future__ import annotations +import functools import logging from numbers import Number @@ -235,17 +236,14 @@ def _apply_action_to_fields( if not result_parse: raise DataMaskingFieldNotFoundError(f"Field or expression {field_parse} not found in {data_parsed}") - # Update the parsed data using a callback function. + # For in-place updates, json_parse accepts a callback function that receives 3 args: field_value, fields, field_name + # We create a partial callback to pre-populate known provider options (action, provider opts, enc ctx) + update_callback = functools.partial( + self._call_action, action=action, provider_options=provider_options, **encryption_context + ) + json_parse.update( - data_parsed, - lambda field_value, fields, field_name, action=action, provider_options=provider_options, encryption_context=encryption_context: self._call_action( # noqa - field_value, - fields, - field_name, - action, - provider_options, - **encryption_context, - ), + data_parsed, lambda field_value, fields, field_name: update_callback(field_value, fields, field_name) ) return data_parsed