Skip to content

Commit db99580

Browse files
committed
feat(parameters): transform = "auto"
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
1 parent 026faaf commit db99580

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

aws_lambda_powertools/utilities/parameters/base.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ def get_multiple(
115115
Maximum age of the cached value
116116
transform: str, optional
117117
Optional transformation of the parameter value. Supported values
118-
are "json" for JSON strings and "binary" for base 64 encoded
119-
values.
118+
are "json" for JSON strings, "binary" for base 64 encoded
119+
values or "auto" which looks at the attribute key to determine the type.
120120
raise_on_transform_error: bool, optional
121121
Raises an exception if any transform fails, otherwise this will
122122
return a None value for each transform that failed
@@ -145,7 +145,11 @@ def get_multiple(
145145

146146
if transform is not None:
147147
for (key, value) in values.items():
148-
values[key] = transform_value(value, transform, raise_on_transform_error)
148+
_transform = get_transform_method(key, transform)
149+
if _transform is None:
150+
continue
151+
152+
values[key] = transform_value(value, _transform, raise_on_transform_error)
149153

150154
self.store[key] = ExpirableValue(values, datetime.now() + timedelta(seconds=max_age),)
151155

@@ -159,6 +163,18 @@ def _get_multiple(self, path: str, **sdk_options) -> Dict[str, str]:
159163
raise NotImplementedError()
160164

161165

166+
def get_transform_method(key: str, transform: str) -> Union[str, None]:
167+
if transform != "auto":
168+
return transform
169+
170+
if key.endswith(".json"):
171+
return "json"
172+
elif key.endswith(".binary"):
173+
return "binary"
174+
else:
175+
return None
176+
177+
162178
def transform_value(value: str, transform: str, raise_on_transform_error: bool = True) -> Union[dict, bytes, None]:
163179
"""
164180
Apply a transform to a value

tests/functional/test_utilities_parameters.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,48 @@ def test_dynamodb_provider_get_multiple(mock_name, mock_value, config):
233233
stubber.deactivate()
234234

235235

236+
def test_dynamodb_provider_get_multiple_auto(mock_name, mock_value, config):
237+
"""
238+
Test DynamoDBProvider.get_multiple() with transform = "auto"
239+
"""
240+
mock_binary = mock_value.encode()
241+
mock_binary_data = base64.b64encode(mock_binary).decode()
242+
mock_json_data = json.dumps({mock_name: mock_value})
243+
mock_params = {"D.json": mock_json_data, "E.binary": mock_binary_data, "F": mock_value}
244+
table_name = "TEST_TABLE_AUTO"
245+
246+
# Create a new provider
247+
provider = parameters.DynamoDBProvider(table_name, config=config)
248+
249+
# Stub the boto3 client
250+
stubber = stub.Stubber(provider.table.meta.client)
251+
response = {
252+
"Items": [
253+
{"id": {"S": mock_name}, "sk": {"S": name}, "value": {"S": value}} for (name, value) in mock_params.items()
254+
]
255+
}
256+
expected_params = {"TableName": table_name, "KeyConditionExpression": Key("id").eq(mock_name)}
257+
stubber.add_response("query", response, expected_params)
258+
stubber.activate()
259+
260+
try:
261+
values = provider.get_multiple(mock_name, transform="auto")
262+
263+
stubber.assert_no_pending_responses()
264+
265+
assert len(values) == len(mock_params)
266+
for key in mock_params.keys():
267+
assert key in values
268+
if key.endswith(".json"):
269+
assert values[key][mock_name] == mock_value
270+
elif key.endswith(".binary"):
271+
assert values[key] == mock_binary
272+
else:
273+
assert values[key] == mock_value
274+
finally:
275+
stubber.deactivate()
276+
277+
236278
def test_dynamodb_provider_get_multiple_next_token(mock_name, mock_value, config):
237279
"""
238280
Test DynamoDBProvider.get_multiple() with a non-cached path

0 commit comments

Comments
 (0)