Skip to content

Commit 4e0e097

Browse files
refactor(parser): address PR review feedback for DynamoDB models
1 parent 4bee949 commit 4e0e097

File tree

1 file changed

+53
-75
lines changed
  • aws_lambda_powertools/utilities/parser/models

1 file changed

+53
-75
lines changed

aws_lambda_powertools/utilities/parser/models/dynamodb.py

Lines changed: 53 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,132 +3,110 @@
33
from typing import Any, Dict, List, Literal, Optional, Type, Union
44

55
from pydantic import BaseModel, Field, field_validator
6+
67
from aws_lambda_powertools.shared.dynamodb_deserializer import TypeDeserializer
78

89
_DESERIALIZER = TypeDeserializer()
910

1011

1112
class DynamoDBStreamChangedRecordModel(BaseModel):
12-
ApproximateCreationDateTime: Optional[float] = Field( # AWS sends this as Unix epoch float
13+
ApproximateCreationDateTime: Optional[datetime] = Field( # AWS sends this as Unix epoch float
1314
default=None,
1415
description="The approximate date and time when the stream record was created (Unix epoch time).",
15-
examples=[1693997155.0]
16-
)
17-
Keys: Dict[str, Any] = Field(
18-
description="Primary key attributes for the item.",
19-
examples=[{"Id": {"N": "101"}}]
16+
examples=[1693997155.0],
2017
)
18+
Keys: Dict[str, Any] = Field(description="Primary key attributes for the item.", examples=[{"Id": {"N": "101"}}])
2119
NewImage: Optional[Union[Dict[str, Any], Type[BaseModel], BaseModel]] = Field(
2220
default=None,
2321
description="The item after modifications, in DynamoDB attribute-value format.",
24-
examples=[{
25-
"Message": {"S": "New item!"},
26-
"Id": {"N": "101"}
27-
}]
22+
examples=[{"Message": {"S": "New item!"}, "Id": {"N": "101"}}],
2823
)
2924
OldImage: Optional[Union[Dict[str, Any], Type[BaseModel], BaseModel]] = Field(
3025
default=None,
3126
description="The item before modifications, in DynamoDB attribute-value format.",
32-
examples=[{
33-
"Message": {"S": "Old item!"},
34-
"Id": {"N": "100"}
35-
}]
36-
)
37-
SequenceNumber: str = Field(
38-
description="A unique identifier for the stream record.",
39-
examples=["222"]
40-
)
41-
SizeBytes: int = Field(
42-
description="The size of the stream record, in bytes.",
43-
examples=[26]
27+
examples=[{"Message": {"S": "Old item!"}, "Id": {"N": "100"}}],
4428
)
29+
SequenceNumber: str = Field(description="A unique identifier for the stream record.", examples=["222"])
30+
SizeBytes: int = Field(description="The size of the stream record, in bytes.", examples=[26])
4531
StreamViewType: Literal["NEW_AND_OLD_IMAGES", "KEYS_ONLY", "NEW_IMAGE", "OLD_IMAGE"] = Field(
46-
description="The type of data included in the stream record.",
47-
examples=["NEW_AND_OLD_IMAGES"]
32+
description="The type of data included in the stream record.", examples=["NEW_AND_OLD_IMAGES"]
4833
)
4934

5035
@field_validator("Keys", "NewImage", "OldImage", mode="before")
5136
def deserialize_field(cls, value):
52-
return {k: _DESERIALIZER.deserialize(v) for k, v in value.items()} if value else value
37+
return {k: _DESERIALIZER.deserialize(v) for k, v in value.items()}
5338

5439

5540
class UserIdentity(BaseModel):
5641
type: Literal["Service"] = Field(
5742
default="Service",
5843
description="The type of identity that made the request, which is always 'Service' for DynamoDB streams.",
59-
examples=["Service"]
44+
examples=["Service"],
6045
)
61-
principalId: str = Field(
46+
principalId: Literal["dynamodb.amazonaws.com"] = Field(
6247
description="The unique identifier for the principal that made the request.",
63-
examples=["dynamodb.amazonaws.com"]
48+
examples=["dynamodb.amazonaws.com"],
6449
)
6550

6651

6752
class DynamoDBStreamRecordModel(BaseModel):
68-
eventID: str = Field(
69-
description="A unique identifier for the event.",
70-
examples=["1"]
71-
)
53+
eventID: str = Field(description="A unique identifier for the event.", examples=["1"])
7254
eventName: Literal["INSERT", "MODIFY", "REMOVE"] = Field(
73-
description="The type of operation that was performed on the item.",
74-
examples=["INSERT"]
55+
description="The type of operation that was performed on the item.", examples=["INSERT"]
7556
)
76-
eventVersion: str = Field(
77-
default="1.0",
78-
description="The version of the stream record format.",
79-
examples=["1.0"]
80-
)
81-
eventSource: str = Field(
57+
eventVersion: float = Field(default="1.0", description="The version of the stream record format.", examples=["1.0"])
58+
eventSource: Literal["aws:dynamodb"] = Field(
8259
default="aws:dynamodb",
8360
description="The source of the event, which is always 'aws:dynamodb' for DynamoDB streams.",
84-
examples=["aws:dynamodb"]
85-
)
86-
awsRegion: str = Field(
87-
description="The AWS region where the stream record was generated.",
88-
examples=["us-west-2"]
61+
examples=["aws:dynamodb"],
8962
)
63+
awsRegion: str = Field(description="The AWS region where the stream record was generated.", examples=["us-west-2"])
9064
eventSourceARN: str = Field(
9165
description="The Amazon Resource Name (ARN) of the DynamoDB stream.",
92-
examples=["arn:aws:dynamodb:us-west-2:123456789012:table/ExampleTable/stream/2021-01-01T00:00:00.000"]
66+
examples=["arn:aws:dynamodb:us-west-2:123456789012:table/ExampleTable/stream/2021-01-01T00:00:00.000"],
9367
)
9468
dynamodb: DynamoDBStreamChangedRecordModel = Field(
9569
description="Contains the details of the DynamoDB stream record.",
96-
examples=[{
97-
"ApproximateCreationDateTime": 1693997155.0,
98-
"Keys": {"Id": {"N": "101"}},
99-
"NewImage": {"Message": {"S": "New item!"}, "Id": {"N": "101"}},
100-
"OldImage": {"Message": {"S": "Old item!"}, "Id": {"N": "100"}},
101-
"SequenceNumber": "222",
102-
"SizeBytes": 26,
103-
"StreamViewType": "NEW_AND_OLD_IMAGES"
104-
}]
70+
examples=[
71+
{
72+
"ApproximateCreationDateTime": 1693997155.0,
73+
"Keys": {"Id": {"N": "101"}},
74+
"NewImage": {"Message": {"S": "New item!"}, "Id": {"N": "101"}},
75+
"OldImage": {"Message": {"S": "Old item!"}, "Id": {"N": "100"}},
76+
"SequenceNumber": "222",
77+
"SizeBytes": 26,
78+
"StreamViewType": "NEW_AND_OLD_IMAGES",
79+
}
80+
],
10581
)
10682
userIdentity: Optional[UserIdentity] = Field(
10783
default=None,
10884
description="Information about the identity that made the request.",
109-
examples=[{"type": "Service", "principalId": "dynamodb.amazonaws.com"}]
85+
examples=[{"type": "Service", "principalId": "dynamodb.amazonaws.com"}],
11086
)
11187

11288

11389
class DynamoDBStreamModel(BaseModel):
11490
Records: List[DynamoDBStreamRecordModel] = Field(
11591
description="A list of records that contain the details of the DynamoDB stream events.",
116-
examples=[{
117-
"eventID": "1",
118-
"eventName": "INSERT",
119-
"eventVersion": "1.0",
120-
"eventSource": "aws:dynamodb",
121-
"awsRegion": "us-west-2",
122-
"eventSourceARN": "arn:aws:dynamodb:us-west-2:123456789012:table/ExampleTable/stream/2021-01-01T00:00:00.000",
123-
"dynamodb": {
124-
"ApproximateCreationDateTime": 1693997155.0,
125-
"Keys": {"Id": {"N": "101"}},
126-
"NewImage": {"Message": {"S": "New item!"}, "Id": {"N": "101"}},
127-
"OldImage": {"Message": {"S": "Old item!"}, "Id": {"N": "100"}},
128-
"SequenceNumber": "222",
129-
"SizeBytes": 26,
130-
"StreamViewType": "NEW_AND_OLD_IMAGES"
131-
},
132-
"userIdentity": {"type": "Service", "principalId": "dynamodb.amazonaws.com"}
133-
}]
134-
)
92+
examples=[
93+
{
94+
"eventID": "1",
95+
"eventName": "INSERT",
96+
"eventVersion": "1.0",
97+
"eventSource": "aws:dynamodb",
98+
"awsRegion": "us-west-2",
99+
"eventSourceARN": "arn:aws:dynamodb:us-west-2:123456789012:table/ExampleTable/stream/2021-01-01T00:00:00.000",
100+
"dynamodb": {
101+
"ApproximateCreationDateTime": 1693997155.0,
102+
"Keys": {"Id": {"N": "101"}},
103+
"NewImage": {"Message": {"S": "New item!"}, "Id": {"N": "101"}},
104+
"OldImage": {"Message": {"S": "Old item!"}, "Id": {"N": "100"}},
105+
"SequenceNumber": "222",
106+
"SizeBytes": 26,
107+
"StreamViewType": "NEW_AND_OLD_IMAGES",
108+
},
109+
"userIdentity": {"type": "Service", "principalId": "dynamodb.amazonaws.com"},
110+
}
111+
],
112+
)

0 commit comments

Comments
 (0)