Skip to content

Commit 4b6ecbf

Browse files
committed
docs: add extending built-in models
1 parent 75dc529 commit 4b6ecbf

File tree

1 file changed

+74
-4
lines changed

1 file changed

+74
-4
lines changed

docs/content/utilities/parser.mdx

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,77 @@ These are simply Python classes that inherit from BaseModel. **Parser** enforces
5959

6060
### Extending built-in models
6161

62-
**TBW**
62+
Parser comes with the following built-in models:
63+
64+
Model name | Description
65+
------------------------------------------------- | ----------------------------------------------------------------------------------------------------------
66+
**DynamoDBStreamModel** | Lambda Event Source payload for Amazon DynamoDB Streams
67+
**EventBridgeModel** | Lambda Event Source payload for Amazon EventBridge
68+
**SqsModel** | Lambda Event Source payload for Amazon SQS
69+
70+
You can extend them to include your own models, and yet have all other known fields parsed along the way.
71+
72+
**EventBridge example**
73+
74+
```python:title=extending_builtin_models.py
75+
from aws_lambda_powertools.utilities.parser import parse, BaseModel
76+
from aws_lambda_powertools.utilities.parser.models import EventBridgeModel
77+
78+
from typing import List, Optional
79+
80+
class OrderItem(BaseModel):
81+
id: int
82+
quantity: int
83+
description: str
84+
85+
class Order(BaseModel):
86+
id: int
87+
description: str
88+
items: List[OrderItem]
89+
90+
# highlight-start
91+
class OrderEventModel(EventBridgeModel):
92+
detail: Order
93+
# highlight-end
94+
95+
payload = {
96+
"version": "0",
97+
"id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718",
98+
"detail-type": "OrderPurchased",
99+
"source": "OrderService",
100+
"account": "111122223333",
101+
"time": "2020-10-22T18:43:48Z",
102+
"region": "us-west-1",
103+
"resources": ["some_additional"],
104+
"detail": { # highlight-line
105+
"id": 10876546789,
106+
"description": "My order",
107+
"items": [
108+
{
109+
"id": 1015938732,
110+
"quantity": 1,
111+
"description": "item xpto"
112+
}
113+
]
114+
}
115+
}
116+
117+
ret = parse(model=OrderEventModel, event=payload) # highlight-line
118+
119+
assert ret.source == "OrderService"
120+
assert ret.detail.description == "My order"
121+
assert ret.detail_type == "OrderPurchased" # we rename it to snake_case
122+
123+
for order_item in ret.detail.items:
124+
...
125+
```
126+
127+
**What's going on here, you might ask**:
128+
129+
1. We imported our built-in model `EventBridgeModel` from the parser utility
130+
2. Defined how our `Order` should look like
131+
3. Defined how part of our EventBridge event should look like by overriding `detail` key within our `OrderEventModel`
132+
4. Parser parsed the original event against `OrderEventModel`
63133

64134
## Parsing events
65135

@@ -232,7 +302,7 @@ parse(model=UserModel, event=payload)
232302

233303
## Envelopes
234304

235-
Envelope parameter is useful when your actual payload is wrapped around a known structure, for example Lambda Event Sources like Amazon EventBridge.
305+
Envelope parameter is useful when your actual payload is wrapped around a known structure, for example Lambda Event Sources like EventBridge.
236306

237307
Example of parsing a model found in an event coming from EventBridge, where all you want is what's inside the `detail` key.
238308

@@ -262,15 +332,15 @@ payload = {
262332
# highlight-end
263333
}
264334

265-
ret = parse(model=UserModel, envelope=envelopes.EventBridgeModel, payload=payload) # highlight-line
335+
ret = parse(model=UserModel, envelope=envelopes.EventBridgeModel, event=payload) # highlight-line
266336

267337
# Parsed model only contains our actual model, not the entire EventBridge + Payload parsed
268338
assert ret.password1 == ret.password2
269339
```
270340

271341
**What's going on here, you might ask**:
272342

273-
1. We imported built-in `models` from the parser utility
343+
1. We imported built-in `envelopes` from the parser utility
274344
2. Used `envelopes.EventBridgeModel` as the envelope for our `UserModel` model
275345
3. Parser parsed the original event against the EventBridge model
276346
4 Parser then parsed the `detail` key using `UserModel`

0 commit comments

Comments
 (0)