Skip to content

Commit a2ce0ac

Browse files
committed
split docs example out
1 parent 0106f79 commit a2ce0ac

File tree

1 file changed

+1
-26
lines changed

1 file changed

+1
-26
lines changed

docs/utilities/parser.md

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -160,32 +160,7 @@ def my_function():
160160
You can parse multiple types using [`Union`](https://docs.pydantic.dev/latest/api/standard_library_types/#union), this gives you control over parsing different event types with the same handler based on an attribute, for example:
161161

162162
```python
163-
from aws_lambda_powertools.utilities.parser import event_parser
164-
from pydantic import BaseModel, Field
165-
from typing import Annotated, Any, Literal, Union
166-
167-
168-
class Cat(BaseModel):
169-
animal: Literal["cat"]
170-
name: str
171-
meow: int
172-
173-
class Dog(BaseModel):
174-
animal: Literal["dog"]
175-
name: str
176-
bark: int
177-
178-
Animal = Annotated[
179-
Union[Cat, Dog], Field(discriminator="animal")
180-
]
181-
182-
@event_parser(model=Animal)
183-
def lambda_handler(event: Animal, _: Any) -> str:
184-
if isinstance(event, CatCallback):
185-
# we have a cat!
186-
return f"🐈: {event.name}"
187-
188-
return f"🐶: {event.name}"
163+
--8<-- "examples/parser/src/multiple_model_parsing.py"
189164
```
190165

191166
### Built-in models

0 commit comments

Comments
 (0)