Skip to content

Commit 7804966

Browse files
committed
split docs example out
1 parent a2ce0ac commit 7804966

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Annotated, Any, Literal, Union
2+
3+
from pydantic import BaseModel, Field
4+
5+
from aws_lambda_powertools.utilities.parser import event_parser
6+
7+
8+
class Cat(BaseModel):
9+
animal: Literal["cat"]
10+
name: str
11+
meow: int
12+
13+
14+
class Dog(BaseModel):
15+
animal: Literal["dog"]
16+
name: str
17+
bark: int
18+
19+
20+
Animal = Annotated[
21+
Union[Cat, Dog],
22+
Field(discriminator="animal"),
23+
]
24+
25+
26+
@event_parser(model=Animal)
27+
def lambda_handler(event: Animal, _: Any) -> str:
28+
if isinstance(event, Cat):
29+
# we have a cat!
30+
return f"🐈: {event.name}"
31+
32+
return f"🐶: {event.name}"

0 commit comments

Comments
 (0)