Skip to content

docs: new parser utility #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4d9b2a9
docs: initial sketch of parser docs
heitorlessa Oct 14, 2020
a6477cf
Merge branch 'develop' into docs/parser
heitorlessa Oct 14, 2020
190703b
docs: initial structure for parser docs
heitorlessa Oct 14, 2020
f0a8a18
docs: add 101 parsing events content
heitorlessa Oct 20, 2020
bf294ee
fix: parse high level import
heitorlessa Oct 20, 2020
96fc444
chore: typo on code generation tool
heitorlessa Oct 20, 2020
ba7cd29
docs: use non-hello world model to better exemplify parsing
heitorlessa Oct 20, 2020
cb8d6a0
fix: ensures parser can take json strings as input
heitorlessa Oct 20, 2020
0ed746b
docs: add data model validation section
heitorlessa Oct 20, 2020
75dc529
docs: add envelope section
heitorlessa Oct 22, 2020
e1eac2d
chore: typo in list
heitorlessa Oct 23, 2020
4b6ecbf
docs: add extending built-in models
heitorlessa Oct 23, 2020
d434f48
docs: ensure examples can be copied/pasted as-is
heitorlessa Oct 23, 2020
ab1f0d2
Merge branch 'docs/parser' of https://github.com/heitorlessa/aws-lamb…
heitorlessa Oct 23, 2020
4e738e9
improv: export Pydantic ValidationError instead of our own
heitorlessa Oct 23, 2020
6c359d5
fix: remove malformed 3.1. sentence
heitorlessa Oct 23, 2020
9b9f4f1
fix: debug logging in envelopes before each parsing
heitorlessa Oct 23, 2020
2732760
chore: spacing
heitorlessa Oct 23, 2020
5f1ad0a
fix: generic type to match ABC bound class
heitorlessa Oct 23, 2020
d2148f2
docs: add a FAQ section
heitorlessa Oct 23, 2020
234afd9
docs: add cold start data
heitorlessa Oct 23, 2020
9e42863
Merge branch 'develop' into docs/parser
heitorlessa Oct 23, 2020
3750d55
improv: address Koudai's PR feedback
heitorlessa Oct 24, 2020
ffbde1d
fix: _parse return type
heitorlessa Oct 24, 2020
33fec71
improv: address Koudai's PR feedback on mypy
heitorlessa Oct 25, 2020
de53605
docs: reorder extending models as parse fn wasn't introduced
heitorlessa Oct 25, 2020
57681a2
docs: reorder data validation; improve envelopes section
heitorlessa Oct 25, 2020
eeabc0f
docs: address Ran's feedback
heitorlessa Oct 25, 2020
592cd56
docs: add a note that decorator will replace the event
heitorlessa Oct 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions aws_lambda_powertools/utilities/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
from . import envelopes
from .envelopes import BaseEnvelope
from .exceptions import ModelValidationError
from .parser import event_parser
from .pydantic import BaseModel, root_validator, validator
from .parser import event_parser, parse
from .pydantic import BaseModel, Field, root_validator, validator

__all__ = [
"event_parser",
"parse",
"envelopes",
"BaseEnvelope",
"BaseModel",
"Field",
"validator",
"root_validator",
"ModelValidationError",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .base import BaseEnvelope
from .dynamodb import DynamoDBEnvelope
from .dynamodb import DynamoDBStreamEnvelope
from .event_bridge import EventBridgeEnvelope
from .sqs import SqsEnvelope

__all__ = ["DynamoDBEnvelope", "EventBridgeEnvelope", "SqsEnvelope", "BaseEnvelope"]
__all__ = ["DynamoDBStreamEnvelope", "EventBridgeEnvelope", "SqsEnvelope", "BaseEnvelope"]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
logger = logging.getLogger(__name__)


class DynamoDBEnvelope(BaseEnvelope):
class DynamoDBStreamEnvelope(BaseEnvelope):
""" DynamoDB Stream Envelope to extract data within NewImage/OldImage

Note: Values are the parsed models. Images' values can also be None, and
Expand All @@ -31,6 +31,8 @@ def parse(self, data: Dict[str, Any], model: BaseModel) -> List[Dict[Literal["Ne
-------
List
List of records parsed with model provided


"""
parsed_envelope = DynamoDBStreamModel(**data)
output = []
Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_powertools/utilities/parser/envelopes/sqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SqsEnvelope(BaseEnvelope):
all items in the list will be parsed as str and npt as JSON (and vice versa)
"""

def parse(self, data: Dict[str, Any], model: Union[BaseModel, str]) -> List[Union[BaseModel, str]]:
def parse(self, data: Dict[str, Any], model: Union[BaseModel, str]) -> List[BaseModel]:
"""Parses records found with model provided

Parameters
Expand Down
3 changes: 3 additions & 0 deletions aws_lambda_powertools/utilities/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def handler(event: Order, context: LambdaContext):

try:
logger.debug("Parsing and validating event model; no envelope used")
if isinstance(event, str):
return model.parse_raw(event)

return model.parse_obj(event)
except (ValidationError, TypeError) as e:
raise ModelValidationError("Input event does not conform with model") from e
Expand Down
Loading