-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parser): infer model from type hint (#3181)
* feat(parser): infer model from type hint if possible * chore(parser): remove unnecessary branch * Small changes --------- Co-authored-by: Leandro Damascena <[email protected]>
- Loading branch information
1 parent
9489ead
commit 36afcf7
Showing
4 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import json | ||
|
||
from pydantic import BaseModel, validator | ||
|
||
from aws_lambda_powertools.utilities.parser import event_parser | ||
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventV2Model | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
|
||
class CancelOrder(BaseModel): | ||
order_id: int | ||
reason: str | ||
|
||
|
||
class CancelOrderModel(APIGatewayProxyEventV2Model): | ||
body: CancelOrder # type: ignore[assignment] | ||
|
||
@validator("body", pre=True) | ||
def transform_body_to_dict(cls, value: str): | ||
return json.loads(value) | ||
|
||
|
||
@event_parser | ||
def handler(event: CancelOrderModel, context: LambdaContext): | ||
cancel_order: CancelOrder = event.body | ||
|
||
assert cancel_order.order_id is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters