Skip to content

Commit 0a067fd

Browse files
author
Michael Brewer
committed
feat(data-classes): add AttributeValueType to DynamoDBStreamEvent
Changes: Add new enum AttributeValueType for the type of AttributeValue Add new method `get_type` to return said enum
1 parent 675cc24 commit 0a067fd

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

aws_lambda_powertools/utilities/data_classes/dynamo_db_stream_event.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,30 @@
44
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
55

66

7+
class AttributeValueType(Enum):
8+
Binary = "B"
9+
BinarySet = "BS"
10+
Boolean = "BOOL"
11+
List = "L"
12+
Map = "M"
13+
Number = "N"
14+
NumberSet = "NS"
15+
Null = "NULL"
16+
String = "S"
17+
StringSet = "SS"
18+
19+
720
class AttributeValue(DictWrapper):
821
"""Represents the data for an attribute
922
1023
Documentation: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_AttributeValue.html
1124
"""
1225

26+
@property
27+
def get_type(self) -> AttributeValueType:
28+
"""Get the attribute value type based on the contained data"""
29+
return AttributeValueType(list(self.raw_event.keys())[0])
30+
1331
@property
1432
def b_value(self) -> Optional[str]:
1533
"""An attribute of type Base64-encoded binary data object

tests/functional/test_data_classes.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
)
5959
from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import (
6060
AttributeValue,
61+
AttributeValueType,
6162
DynamoDBRecordEventName,
6263
DynamoDBStreamEvent,
6364
StreamViewType,
@@ -443,13 +444,38 @@ def test_dynamo_db_stream_trigger_event():
443444
assert record.user_identity is None
444445

445446

447+
def test_dynamo_attribute_value_b_value():
448+
example_attribute_value = {"B": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"}
449+
450+
attribute_value = AttributeValue(example_attribute_value)
451+
452+
assert attribute_value.get_type == AttributeValueType.Binary
453+
454+
455+
def test_dynamo_attribute_value_bs_value():
456+
example_attribute_value = {"BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]}
457+
458+
attribute_value = AttributeValue(example_attribute_value)
459+
460+
assert attribute_value.get_type == AttributeValueType.BinarySet
461+
462+
463+
def test_dynamo_attribute_value_bool_value():
464+
example_attribute_value = {"BOOL": True}
465+
466+
attribute_value = AttributeValue(example_attribute_value)
467+
468+
assert attribute_value.get_type == AttributeValueType.Boolean
469+
470+
446471
def test_dynamo_attribute_value_list_value():
447472
example_attribute_value = {"L": [{"S": "Cookies"}, {"S": "Coffee"}, {"N": "3.14159"}]}
448473
attribute_value = AttributeValue(example_attribute_value)
449474
list_value = attribute_value.list_value
450475
assert list_value is not None
451476
item = list_value[0]
452477
assert item.s_value == "Cookies"
478+
assert attribute_value.get_type == AttributeValueType.List
453479

454480

455481
def test_dynamo_attribute_value_map_value():
@@ -461,6 +487,47 @@ def test_dynamo_attribute_value_map_value():
461487
assert map_value is not None
462488
item = map_value["Name"]
463489
assert item.s_value == "Joe"
490+
assert attribute_value.get_type == AttributeValueType.Map
491+
492+
493+
def test_dynamo_attribute_value_n_value():
494+
example_attribute_value = {"N": "123.45"}
495+
496+
attribute_value = AttributeValue(example_attribute_value)
497+
498+
assert attribute_value.get_type == AttributeValueType.Number
499+
500+
501+
def test_dynamo_attribute_value_ns_value():
502+
example_attribute_value = {"NS": ["42.2", "-19", "7.5", "3.14"]}
503+
504+
attribute_value = AttributeValue(example_attribute_value)
505+
506+
assert attribute_value.get_type == AttributeValueType.NumberSet
507+
508+
509+
def test_dynamo_attribute_value_null_value():
510+
example_attribute_value = {"NULL": True}
511+
512+
attribute_value = AttributeValue(example_attribute_value)
513+
514+
assert attribute_value.get_type == AttributeValueType.Null
515+
516+
517+
def test_dynamo_attribute_value_s_value():
518+
example_attribute_value = {"S": "Hello"}
519+
520+
attribute_value = AttributeValue(example_attribute_value)
521+
522+
assert attribute_value.get_type == AttributeValueType.String
523+
524+
525+
def test_dynamo_attribute_value_ss_value():
526+
example_attribute_value = {"SS": ["Giraffe", "Hippo", "Zebra"]}
527+
528+
attribute_value = AttributeValue(example_attribute_value)
529+
530+
assert attribute_value.get_type == AttributeValueType.StringSet
464531

465532

466533
def test_event_bridge_event():

0 commit comments

Comments
 (0)