From 8724294013e3157de9478f90e7c1283a952de54f Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Tue, 8 Jun 2021 20:21:55 +0200 Subject: [PATCH] docs(data_classes): fix missing dynamodb stream get_type/value --- docs/utilities/data_classes.md | 51 ++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/docs/utilities/data_classes.md b/docs/utilities/data_classes.md index 5b0d0db8c0..3217c5364d 100644 --- a/docs/utilities/data_classes.md +++ b/docs/utilities/data_classes.md @@ -556,21 +556,42 @@ attributes values (`AttributeValue`), as well as enums for stream view type (`St === "app.py" -```python -from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import ( - DynamoDBStreamEvent, - DynamoDBRecordEventName -) - -def lambda_handler(event, context): - event: DynamoDBStreamEvent = DynamoDBStreamEvent(event) - - # Multiple records can be delivered in a single event - for record in event.records: - if record.event_name == DynamoDBRecordEventName.MODIFY: - do_something_with(record.dynamodb.new_image) - do_something_with(record.dynamodb.old_image) -``` + ```python + from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import ( + DynamoDBStreamEvent, + DynamoDBRecordEventName + ) + + def lambda_handler(event, context): + event: DynamoDBStreamEvent = DynamoDBStreamEvent(event) + + # Multiple records can be delivered in a single event + for record in event.records: + if record.event_name == DynamoDBRecordEventName.MODIFY: + do_something_with(record.dynamodb.new_image) + do_something_with(record.dynamodb.old_image) + ``` + +=== "multiple_records_types.py" + + ```python + 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: + # {"N": "123.45"} => "123.45" + 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) + ``` ### EventBridge