Skip to content

Commit de53605

Browse files
committed
docs: reorder extending models as parse fn wasn't introduced
Signed-off-by: heitorlessa <lessa@amazon.co.uk>
1 parent 33fec71 commit de53605

File tree

1 file changed

+75
-75
lines changed

1 file changed

+75
-75
lines changed

docs/content/utilities/parser.mdx

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -57,80 +57,6 @@ These are simply Python classes that inherit from BaseModel. **Parser** enforces
5757
Use Koudai Aono's <a href="https://github.com/koxudaxi/datamodel-code-generator">data model code generation tool for Pydantic</a>
5858
</Note><br/>
5959

60-
### Extending built-in models
61-
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 since detail-type is an invalid name
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`
133-
13460
## Parsing events
13561

13662
You can parse inbound events using **event_parser** decorator, or the standalone `parse` function. Both are also able to parse either dictionary or JSON string as an input.
@@ -141,7 +67,7 @@ Use the decorator for fail fast scenarios where you want your Lambda function to
14167

14268
`event_parser` decorator will throw a `ValidationError` if your event cannot be parsed according to the model.
14369

144-
```python=:title=event_parser_decorator.py
70+
```python:title=event_parser_decorator.py
14571
from aws_lambda_powertools.utilities.parser import event_parser, BaseModel, ValidationError
14672
from aws_lambda_powertools.utilities.typing import LambdaContext
14773
import json
@@ -321,6 +247,80 @@ parse(model=UserModel, event=payload)
321247
You can read more about validating list items, reusing validators, validating raw inputs, and a lot more in <a href="https://pydantic-docs.helpmanual.io/usage/validators/">Pydantic's documentation</a>.
322248
</Note><br/>
323249

250+
## Extending built-in models
251+
252+
Parser comes with the following built-in models:
253+
254+
Model name | Description
255+
------------------------------------------------- | ----------------------------------------------------------------------------------------------------------
256+
**DynamoDBStreamModel** | Lambda Event Source payload for Amazon DynamoDB Streams
257+
**EventBridgeModel** | Lambda Event Source payload for Amazon EventBridge
258+
**SqsModel** | Lambda Event Source payload for Amazon SQS
259+
260+
You can extend them to include your own models, and yet have all other known fields parsed along the way.
261+
262+
**EventBridge example**
263+
264+
```python:title=extending_builtin_models.py
265+
from aws_lambda_powertools.utilities.parser import parse, BaseModel
266+
from aws_lambda_powertools.utilities.parser.models import EventBridgeModel
267+
268+
from typing import List, Optional
269+
270+
class OrderItem(BaseModel):
271+
id: int
272+
quantity: int
273+
description: str
274+
275+
class Order(BaseModel):
276+
id: int
277+
description: str
278+
items: List[OrderItem]
279+
280+
# highlight-start
281+
class OrderEventModel(EventBridgeModel):
282+
detail: Order
283+
# highlight-end
284+
285+
payload = {
286+
"version": "0",
287+
"id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718",
288+
"detail-type": "OrderPurchased",
289+
"source": "OrderService",
290+
"account": "111122223333",
291+
"time": "2020-10-22T18:43:48Z",
292+
"region": "us-west-1",
293+
"resources": ["some_additional"],
294+
"detail": { # highlight-line
295+
"id": 10876546789,
296+
"description": "My order",
297+
"items": [
298+
{
299+
"id": 1015938732,
300+
"quantity": 1,
301+
"description": "item xpto"
302+
}
303+
]
304+
}
305+
}
306+
307+
ret = parse(model=OrderEventModel, event=payload) # highlight-line
308+
309+
assert ret.source == "OrderService"
310+
assert ret.detail.description == "My order"
311+
assert ret.detail_type == "OrderPurchased" # we rename it to snake_case since detail-type is an invalid name
312+
313+
for order_item in ret.detail.items:
314+
...
315+
```
316+
317+
**What's going on here, you might ask**:
318+
319+
1. We imported our built-in model `EventBridgeModel` from the parser utility
320+
2. Defined how our `Order` should look like
321+
3. Defined how part of our EventBridge event should look like by overriding `detail` key within our `OrderEventModel`
322+
4. Parser parsed the original event against `OrderEventModel`
323+
324324

325325
## Envelopes
326326

0 commit comments

Comments
 (0)