Skip to content

feat(data-classes): add AttributeValueType to DynamoDBStreamEvent #462

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged

Conversation

michaelbrewer
Copy link
Contributor

@michaelbrewer michaelbrewer commented Jun 6, 2021

Issue #, if available:

#461

Description of changes:

Changes:

  • Add new enum AttributeValueType for the type of AttributeValue
  • Add new property get_type to return said enum
  • Add new property get_value which returns converted value
  • Update docs with any revisited changes in the type conversion process

Example usage:

from aws_lambda_powertools.utilities.data_classes import event_source, DynamoDBStreamEvent
from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import AttributeValueType, AttributeValue
from aws_lambda_powertools.utilities.typing import LambdaContext


@event_source(data_class=DynamoDBStreamEvent)
def lambda_handler(event: DynamoDBStreamEvent, context: LambdaContext):
    for record in event.records:
        key: AttributeValue = record.dynamodb.keys["id"]
        if key == AttributeValueType.Number:
            assert key.get_value == key.n_value
            print(key.get_value)
        elif key == AttributeValueType.Map:
            assert key.get_value == key.map_value
            print(key.get_value)

Where AttributeValueType is:

class AttributeValueType(Enum):
    Binary = "B"
    BinarySet = "BS"
    Boolean = "BOOL"
    List = "L"
    Map = "M"
    Number = "N"
    NumberSet = "NS"
    Null = "NULL"
    String = "S"
    StringSet = "SS"

How AttributeValue does the raw values

Example Values:

  • b_value:
    {"B": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"}
  • bs_value
    {"BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]}
  • bool_value
    {"BOOL": True}
  • list_value/l_value
    {"L": [ {"S": "Cookies"} , {"S": "Coffee"}, {"N": "3.14159"}]}
  • map_value/m_value
    {"M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}}}
  • n_value
    {"N": "123.45"}
  • ns_value
    {"NS": ["42.2", "-19", "7.5", "3.14"]}
  • null_value
    {"NULL": True}
  • s_value
    {"S": "Hello"}
  • ss_value
    {"SS": ["Giraffe", "Hippo" ,"Zebra"]}

NOTE: All return types are Optional

Return types

Property Description Raw Return Type
b_value An attribute of type Base64-encoded binary data object str
bs_value An attribute of type Array of Base64-encoded binary data objects List[str]
bool_value An attribute of type Boolean bool
list_value l_value An attribute of type Array of AttributeValue objects List["AttributeValue"]
map_value m_value An attribute of type String to AttributeValue object map Dict[str, "AttributeValue"]
n_value An attribute of type Number str
ns_value An attribute of type Number Set List[str]
null_value An attribute of type Null bool
s_value An attribute of type String str
ss_value An attribute of type Array of strings List[str]

How Dynamodb Type does deserialization

DynamoDB Python
{'NULL': True} None
{'BOOL': True/False} True/False
{'N': str(value)} Decimal(str(value))
{'S': string} string
{'B': bytes} Binary(bytes)
{'NS': [str(value)]} set([Decimal(str(value))])
{'SS': [string]} set([string])
{'BS': [bytes]} set([bytes])
{'L': list} list
{'M': dict} dict

Checklist

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Changes:
Add new enum AttributeValueType for the type of AttributeValue
Add new method `get_type` to return said enum
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 6, 2021
@codecov-commenter
Copy link

codecov-commenter commented Jun 6, 2021

Codecov Report

Merging #462 (ca79f4a) into develop (675cc24) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #462   +/-   ##
========================================
  Coverage    99.90%   99.90%           
========================================
  Files          105      105           
  Lines         4202     4231   +29     
  Branches       205      205           
========================================
+ Hits          4198     4227   +29     
  Misses           1        1           
  Partials         3        3           
Impacted Files Coverage Δ
...s/utilities/data_classes/dynamo_db_stream_event.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b029b5c...ca79f4a. Read the comment docs.

@michaelbrewer
Copy link
Contributor Author

@gwlester what do you think?

@gwlester
Copy link
Contributor

gwlester commented Jun 6, 2021

Looks great!

Thanks for the fast work!

@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jun 7, 2021
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll release 1.17.0 tomorrow and I could get this included too, if we could have an example usage in the docs. Thanks a lot!

@gwlester
Copy link
Contributor

gwlester commented Jun 7, 2021

Will this be in the next Release?

@michaelbrewer michaelbrewer requested a review from heitorlessa June 8, 2021 03:51
@heitorlessa
Copy link
Contributor

Thanks @michaelbrewer for the changes!

I'm gonna look into the CI failures

@heitorlessa heitorlessa merged commit a65e55c into aws-powertools:develop Jun 8, 2021
@michaelbrewer michaelbrewer deleted the feat-dynamodb-stream-event-type branch June 8, 2021 04:16
@michaelbrewer
Copy link
Contributor Author

Thanks @michaelbrewer for the changes!

I'm gonna look into the CI failures

Failures seem to be intermittent getting the codecov token

@heitorlessa
Copy link
Contributor

heitorlessa commented Jun 8, 2021 via email

heitorlessa added a commit that referenced this pull request Jun 8, 2021
…tools-python into develop

* 'develop' of https://github.com/awslabs/aws-lambda-powertools-python:
  chore(deps): bump boto3 from 1.17.88 to 1.17.89 (#466)
  feat(data-classes): add AttributeValueType to DynamoDBStreamEvent (#462)
  chore(deps): bump boto3 from 1.17.87 to 1.17.88 (#463)
  chore(deps-dev): bump mkdocs-material from 7.1.6 to 7.1.7 (#464)
  feat(data-classes): decorator to instantiate data_classes and docs updates (#442)
  chore(deps): bump boto3 from 1.17.86 to 1.17.87 (#459)
@heitorlessa heitorlessa added the feature New feature or functionality label Jun 8, 2021
@gwlester
Copy link
Contributor

gwlester commented Jun 9, 2021

FYI, added it into the code (Dynamo Stream -> Lambda -> TimeStream -- for audit trail) and it works great!!!

Thanks again!

@heitorlessa
Copy link
Contributor

heitorlessa commented Jun 10, 2021 via email

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
feature New feature or functionality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants