Skip to content

Commit 987c559

Browse files
author
Michael Brewer
committed
feat(data-classes): add value property to AttributeValue
1 parent 0a067fd commit 987c559

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

aws_lambda_powertools/utilities/data_classes/dynamo_db_stream_event.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import Enum
2-
from typing import Dict, Iterator, List, Optional
2+
from typing import Any, Dict, Iterator, List, Optional, Union
33

44
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
55

@@ -23,10 +23,16 @@ class AttributeValue(DictWrapper):
2323
Documentation: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_AttributeValue.html
2424
"""
2525

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])
26+
def __init__(self, data: Dict[str, Any]):
27+
"""AttributeValue constructor
28+
29+
Parameters
30+
----------
31+
data: Dict[str, Any]
32+
Raw lambda event dict
33+
"""
34+
super().__init__(data)
35+
self.dynamodb_type = list(data.keys())[0]
3036

3137
@property
3238
def b_value(self) -> Optional[str]:
@@ -124,6 +130,29 @@ def ss_value(self) -> Optional[List[str]]:
124130
"""
125131
return self.get("SS")
126132

133+
@property
134+
def get_type(self) -> AttributeValueType:
135+
"""Get the attribute value type based on the contained data"""
136+
return AttributeValueType(self.dynamodb_type)
137+
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+
146+
@property
147+
def l_value(self) -> Optional[List["AttributeValue"]]:
148+
"""Alias of list_value"""
149+
return self.list_value
150+
151+
@property
152+
def m_value(self) -> Optional[Dict[str, "AttributeValue"]]:
153+
"""Alias of map_value"""
154+
return self.map_value
155+
127156

128157
def _attribute_value_dict(attr_values: Dict[str, dict], key: str) -> Optional[Dict[str, AttributeValue]]:
129158
"""A dict of type String to AttributeValue object map

tests/functional/test_data_classes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +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
453454

454455

455456
def test_dynamo_attribute_value_bs_value():
@@ -458,6 +459,7 @@ def test_dynamo_attribute_value_bs_value():
458459
attribute_value = AttributeValue(example_attribute_value)
459460

460461
assert attribute_value.get_type == AttributeValueType.BinarySet
462+
assert attribute_value.bs_value == attribute_value.value
461463

462464

463465
def test_dynamo_attribute_value_bool_value():
@@ -466,6 +468,7 @@ def test_dynamo_attribute_value_bool_value():
466468
attribute_value = AttributeValue(example_attribute_value)
467469

468470
assert attribute_value.get_type == AttributeValueType.Boolean
471+
assert attribute_value.bool_value == attribute_value.value
469472

470473

471474
def test_dynamo_attribute_value_list_value():
@@ -476,6 +479,8 @@ def test_dynamo_attribute_value_list_value():
476479
item = list_value[0]
477480
assert item.s_value == "Cookies"
478481
assert attribute_value.get_type == AttributeValueType.List
482+
assert attribute_value.l_value == attribute_value.list_value
483+
assert attribute_value.list_value == attribute_value.value
479484

480485

481486
def test_dynamo_attribute_value_map_value():
@@ -488,6 +493,8 @@ def test_dynamo_attribute_value_map_value():
488493
item = map_value["Name"]
489494
assert item.s_value == "Joe"
490495
assert attribute_value.get_type == AttributeValueType.Map
496+
assert attribute_value.m_value == attribute_value.map_value
497+
assert attribute_value.map_value == attribute_value.value
491498

492499

493500
def test_dynamo_attribute_value_n_value():
@@ -496,6 +503,7 @@ def test_dynamo_attribute_value_n_value():
496503
attribute_value = AttributeValue(example_attribute_value)
497504

498505
assert attribute_value.get_type == AttributeValueType.Number
506+
assert attribute_value.n_value == attribute_value.value
499507

500508

501509
def test_dynamo_attribute_value_ns_value():
@@ -504,6 +512,7 @@ def test_dynamo_attribute_value_ns_value():
504512
attribute_value = AttributeValue(example_attribute_value)
505513

506514
assert attribute_value.get_type == AttributeValueType.NumberSet
515+
assert attribute_value.ns_value == attribute_value.value
507516

508517

509518
def test_dynamo_attribute_value_null_value():
@@ -512,6 +521,7 @@ def test_dynamo_attribute_value_null_value():
512521
attribute_value = AttributeValue(example_attribute_value)
513522

514523
assert attribute_value.get_type == AttributeValueType.Null
524+
assert attribute_value.null_value == attribute_value.value
515525

516526

517527
def test_dynamo_attribute_value_s_value():
@@ -520,6 +530,7 @@ def test_dynamo_attribute_value_s_value():
520530
attribute_value = AttributeValue(example_attribute_value)
521531

522532
assert attribute_value.get_type == AttributeValueType.String
533+
assert attribute_value.s_value == attribute_value.value
523534

524535

525536
def test_dynamo_attribute_value_ss_value():
@@ -528,6 +539,7 @@ def test_dynamo_attribute_value_ss_value():
528539
attribute_value = AttributeValue(example_attribute_value)
529540

530541
assert attribute_value.get_type == AttributeValueType.StringSet
542+
assert attribute_value.ss_value == attribute_value.value
531543

532544

533545
def test_event_bridge_event():

0 commit comments

Comments
 (0)