Skip to content

Commit 4a5cd55

Browse files
committed
add some docs for unions
1 parent 5d50e7d commit 4a5cd55

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

docs/utilities/parser.md

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,13 @@ This utility provides data parsing and deep validation using [Pydantic](https://
1111
* Defines data in pure Python classes, then parse, validate and extract only what you want
1212
* Built-in envelopes to unwrap, extend, and validate popular event sources payloads
1313
* Enforces type hints at runtime with user-friendly errors
14-
* Support for Pydantic v1 and v2
14+
* Support for Pydantic v2
1515

1616
## Getting started
1717

1818
### Install
1919

20-
Powertools for AWS Lambda (Python) supports Pydantic v1 and v2. Each Pydantic version requires different dependencies before you can use Parser.
21-
22-
#### Using Pydantic v1
23-
24-
!!! info "This is not necessary if you're installing Powertools for AWS Lambda (Python) via [Lambda Layer/SAR](../index.md#lambda-layer){target="_blank"}"
25-
26-
Add `aws-lambda-powertools[parser]` as a dependency in your preferred tool: _e.g._, _requirements.txt_, _pyproject.toml_.
27-
28-
???+ warning
29-
This will increase the compressed package size by >10MB due to the Pydantic dependency.
30-
31-
To reduce the impact on the package size at the expense of 30%-50% of its performance [Pydantic can also be
32-
installed without binary files](https://pydantic-docs.helpmanual.io/install/#performance-vs-package-size-trade-off){target="_blank" rel="nofollow"}:
33-
34-
Pip example: `SKIP_CYTHON=1 pip install --no-binary pydantic aws-lambda-powertools[parser]`
20+
Powertools for AWS Lambda (Python) supports Pydantic v2. Each Pydantic version requires different dependencies before you can use Parser.
3521

3622
#### Using Pydantic v2
3723

@@ -169,6 +155,39 @@ def my_function():
169155
}
170156
```
171157

158+
#### Union parsing
159+
160+
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:
161+
162+
```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}"
189+
```
190+
172191
### Built-in models
173192

174193
Parser comes with the following built-in models:

0 commit comments

Comments
 (0)