Skip to content

Commit 91f50c4

Browse files
author
Michael Brewer
committed
refactor(data-classes): change to get_value and update docs
- Refactor value to get_value - Add code example
1 parent 989b756 commit 91f50c4

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

aws_lambda_powertools/utilities/data_classes/dynamo_db_stream_event.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ class AttributeValueType(Enum):
2020
class AttributeValue(DictWrapper):
2121
"""Represents the data for an attribute
2222
23-
Documentation: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_AttributeValue.html
23+
Documentation:
24+
--------------
25+
- https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_AttributeValue.html
26+
- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html
2427
"""
2528

2629
def __init__(self, data: Dict[str, Any]):
@@ -135,14 +138,6 @@ def get_type(self) -> AttributeValueType:
135138
"""Get the attribute value type based on the contained data"""
136139
return AttributeValueType(self.dynamodb_type)
137140

138-
@property
139-
def value(self) -> Union[Optional[bool], Optional[str], Optional[List], Optional[Dict]]:
140-
"""Get the attribute value"""
141-
try:
142-
return getattr(self, f"{self.dynamodb_type.lower()}_value")
143-
except AttributeError:
144-
raise TypeError(f"Dynamodb type {self.dynamodb_type} is not supported")
145-
146141
@property
147142
def l_value(self) -> Optional[List["AttributeValue"]]:
148143
"""Alias of list_value"""
@@ -153,6 +148,14 @@ def m_value(self) -> Optional[Dict[str, "AttributeValue"]]:
153148
"""Alias of map_value"""
154149
return self.map_value
155150

151+
@property
152+
def get_value(self) -> Union[Optional[bool], Optional[str], Optional[List], Optional[Dict]]:
153+
"""Get the attribute value"""
154+
try:
155+
return getattr(self, f"{self.dynamodb_type.lower()}_value")
156+
except AttributeError:
157+
raise TypeError(f"Dynamodb type {self.dynamodb_type} is not supported")
158+
156159

157160
def _attribute_value_dict(attr_values: Dict[str, dict], key: str) -> Optional[Dict[str, AttributeValue]]:
158161
"""A dict of type String to AttributeValue object map
@@ -271,6 +274,31 @@ class DynamoDBStreamEvent(DictWrapper):
271274
Documentation:
272275
-------------
273276
- https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html
277+
278+
Example
279+
-------
280+
**Process dynamodb stream events and use get_type and get_value for handling conversions**
281+
282+
from aws_lambda_powertools.utilities.data_classes import event_source, DynamoDBStreamEvent
283+
from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import (
284+
AttributeValueType,
285+
AttributeValue,
286+
)
287+
from aws_lambda_powertools.utilities.typing import LambdaContext
288+
289+
290+
@event_source(data_class=DynamoDBStreamEvent)
291+
def lambda_handler(event: DynamoDBStreamEvent, context: LambdaContext):
292+
for record in event.records:
293+
key: AttributeValue = record.dynamodb.keys["id"]
294+
if key == AttributeValueType.Number:
295+
assert key.get_value == key.n_value
296+
print(key.get_value)
297+
elif key == AttributeValueType.Map:
298+
assert key.get_value == key.map_value
299+
print(key.get_value)
300+
301+
274302
"""
275303

276304
@property

tests/functional/test_data_classes.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def test_dynamo_attribute_value_b_value():
450450
attribute_value = AttributeValue(example_attribute_value)
451451

452452
assert attribute_value.get_type == AttributeValueType.Binary
453-
assert attribute_value.b_value == attribute_value.value
453+
assert attribute_value.b_value == attribute_value.get_value
454454

455455

456456
def test_dynamo_attribute_value_bs_value():
@@ -459,7 +459,7 @@ def test_dynamo_attribute_value_bs_value():
459459
attribute_value = AttributeValue(example_attribute_value)
460460

461461
assert attribute_value.get_type == AttributeValueType.BinarySet
462-
assert attribute_value.bs_value == attribute_value.value
462+
assert attribute_value.bs_value == attribute_value.get_value
463463

464464

465465
def test_dynamo_attribute_value_bool_value():
@@ -468,7 +468,7 @@ def test_dynamo_attribute_value_bool_value():
468468
attribute_value = AttributeValue(example_attribute_value)
469469

470470
assert attribute_value.get_type == AttributeValueType.Boolean
471-
assert attribute_value.bool_value == attribute_value.value
471+
assert attribute_value.bool_value == attribute_value.get_value
472472

473473

474474
def test_dynamo_attribute_value_list_value():
@@ -480,7 +480,7 @@ def test_dynamo_attribute_value_list_value():
480480
assert item.s_value == "Cookies"
481481
assert attribute_value.get_type == AttributeValueType.List
482482
assert attribute_value.l_value == attribute_value.list_value
483-
assert attribute_value.list_value == attribute_value.value
483+
assert attribute_value.list_value == attribute_value.get_value
484484

485485

486486
def test_dynamo_attribute_value_map_value():
@@ -494,7 +494,7 @@ def test_dynamo_attribute_value_map_value():
494494
assert item.s_value == "Joe"
495495
assert attribute_value.get_type == AttributeValueType.Map
496496
assert attribute_value.m_value == attribute_value.map_value
497-
assert attribute_value.map_value == attribute_value.value
497+
assert attribute_value.map_value == attribute_value.get_value
498498

499499

500500
def test_dynamo_attribute_value_n_value():
@@ -503,7 +503,7 @@ def test_dynamo_attribute_value_n_value():
503503
attribute_value = AttributeValue(example_attribute_value)
504504

505505
assert attribute_value.get_type == AttributeValueType.Number
506-
assert attribute_value.n_value == attribute_value.value
506+
assert attribute_value.n_value == attribute_value.get_value
507507

508508

509509
def test_dynamo_attribute_value_ns_value():
@@ -512,7 +512,7 @@ def test_dynamo_attribute_value_ns_value():
512512
attribute_value = AttributeValue(example_attribute_value)
513513

514514
assert attribute_value.get_type == AttributeValueType.NumberSet
515-
assert attribute_value.ns_value == attribute_value.value
515+
assert attribute_value.ns_value == attribute_value.get_value
516516

517517

518518
def test_dynamo_attribute_value_null_value():
@@ -521,7 +521,7 @@ def test_dynamo_attribute_value_null_value():
521521
attribute_value = AttributeValue(example_attribute_value)
522522

523523
assert attribute_value.get_type == AttributeValueType.Null
524-
assert attribute_value.null_value == attribute_value.value
524+
assert attribute_value.null_value == attribute_value.get_value
525525

526526

527527
def test_dynamo_attribute_value_s_value():
@@ -530,7 +530,7 @@ def test_dynamo_attribute_value_s_value():
530530
attribute_value = AttributeValue(example_attribute_value)
531531

532532
assert attribute_value.get_type == AttributeValueType.String
533-
assert attribute_value.s_value == attribute_value.value
533+
assert attribute_value.s_value == attribute_value.get_value
534534

535535

536536
def test_dynamo_attribute_value_ss_value():
@@ -539,7 +539,7 @@ def test_dynamo_attribute_value_ss_value():
539539
attribute_value = AttributeValue(example_attribute_value)
540540

541541
assert attribute_value.get_type == AttributeValueType.StringSet
542-
assert attribute_value.ss_value == attribute_value.value
542+
assert attribute_value.ss_value == attribute_value.get_value
543543

544544

545545
def test_dynamo_attribute_value_type_error():
@@ -548,7 +548,7 @@ def test_dynamo_attribute_value_type_error():
548548
attribute_value = AttributeValue(example_attribute_value)
549549

550550
with pytest.raises(TypeError):
551-
print(attribute_value.value)
551+
print(attribute_value.get_value)
552552
with pytest.raises(ValueError):
553553
print(attribute_value.get_type)
554554

0 commit comments

Comments
 (0)