-
Notifications
You must be signed in to change notification settings - Fork 421
feat(parameters): transform = "auto" #133
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
Conversation
Add a new tranform type called "auto", which looks at the key name to autodetect the encoding of the values in dynamodb or the parameter store. Keys ending with ".json" are assumed to be "json" string Keys ending with ".binary" are assumed to be "binary" base64 decode strings
Codecov Report
@@ Coverage Diff @@
## develop #133 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 28 28
Lines 793 806 +13
Branches 72 76 +4
=========================================
+ Hits 793 806 +13
Continue to review full report at Codecov.
|
@nmoutschen - if it is a matter of flexibility on the naming pattern of the keys, we could create patterns for them. Another optional it giving the new user an option to set a customer converter that accepts "key" and "value". So you could provide an auto-formatter, a yaml one, xml etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @michaelbrewer !
Thanks a lot for your contribution! 😄 I have one open question regarding the user experience for this feature: should we rename the key by dropping the extension?
It would also be good to parameterise the transforms just to have a single place where to add/change transforms to prevent errors in the future if we want to add new transforms. However, this aspect is good as-is for my point of view.
@nmoutschen would this patch make this more flexibile? diff --git a/aws_lambda_powertools/utilities/parameters/base.py b/aws_lambda_powertools/utilities/parameters/base.py
index e1ba9ac..1269432 100644
--- a/aws_lambda_powertools/utilities/parameters/base.py
+++ b/aws_lambda_powertools/utilities/parameters/base.py
@@ -15,6 +15,9 @@ DEFAULT_MAX_AGE_SECS = 5
ExpirableValue = namedtuple("ExpirableValue", ["value", "ttl"])
# These providers will be dynamically initialized on first use of the helper functions
DEFAULT_PROVIDERS = {}
+TRANSFORM_METHOD_JSON = "json"
+TRANSFORM_METHOD_BINARY = "binary"
+SUPPORTED_TRANSFORM_METHODS = [TRANSFORM_METHOD_JSON, TRANSFORM_METHOD_BINARY]
class BaseProvider(ABC):
@@ -194,12 +197,10 @@ def get_transform_method(key: str, transform: str) -> Optional[str]:
if transform != "auto":
return transform
- if key.endswith(".json"):
- return "json"
- elif key.endswith(".binary"):
- return "binary"
- else:
- return None
+ for transform_method in SUPPORTED_TRANSFORM_METHODS:
+ if key.endswith("." + transform_method):
+ return transform_method
+ return None
def transform_value(value: str, transform: str, raise_on_transform_error: bool = True) -> Union[dict, bytes, None]:
@@ -223,9 +224,9 @@ def transform_value(value: str, transform: str, raise_on_transform_error: bool =
"""
try:
- if transform == "json":
+ if transform == TRANSFORM_METHOD_JSON:
return json.loads(value)
- elif transform == "binary":
+ elif transform == TRANSFORM_METHOD_BINARY:
return base64.b64decode(value)
else:
raise ValueError(f"Invalid transform type '{transform}'")
|
Changes: * base.py - Add `SUPPORTED_TRANSFORM_METHODS` as a list of supported transform methods and add constants for the currently supported methods * base.py - Update `get_transform_method` to use `SUPPORTED_TRANSFORM_METHODS` to check for supported extensions
Changes: * Make `transform` optional in `get_transform_method` * Update tests to include this use case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
* develop: (57 commits) chore: bump version to 1.5.0 (#158) chore(batch): Housekeeping for recent changes (#157) docs: address readability feedbacks chore: add debug logging for sqs batch processing chore: remove middlewares module, moving decorator functionality to base and sqs docs: add detail to batch processing fix: throw exception by default if messages processing fails chore: add test for sqs_batch_processor interface fix: add sqs_batch_processor as its own method docs: simplify documentation more SQS specific focus Update for sqs_batch_processor interface chore: add sqs_batch_processor decorator to simplify interface feat(parameters): transform = "auto" (#133) chore: fix typos, docstrings and type hints (#154) chore: tiny changes for readability fix: ensure debug log event has latest ctx docs: rephrase the wording to make it more clear docs: refactor example; improve docs about creating your own processor refactor: remove references to BaseProcessor. Left BasePartialProcessor docs: add newly created Slack Channel fix: update image with correct sample ...
Issue #, if available: #136
When fetching multiple values from the parameter store or dynamodb often each record might have different encoding and not all have uniform encoding. eg:
Description of changes:
Add a new tranform type called "auto", which looks at the
key name to autodetect the encoding of the values in dynamodb or
the parameter store.
strings
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.