Conversation
…anager for multiple with blocks
vladbagmet
left a comment
There was a problem hiding this comment.
praise: Awesome description for the pull request! 👍
question (blocking): Can validation be done inside base_handler?
| func: Callable, | ||
| raw_event_type: Type[RawBaseEvent], | ||
| handler: Optional[logging.Handler], | ||
| app_decorator_type: str, |
There was a problem hiding this comment.
question (blocking): Can we use raw_event_type to identify type?
There was a problem hiding this comment.
Ohh, yeah, great idea! Thanks for catching it! 🙌
| from corva.models.stream.raw import RawStreamEvent | ||
| from corva.models.task import RawTaskEvent | ||
|
|
||
| BASE_EVENT_CLS_TO_APP_TYPE_MAPPING: Dict[str, Type[RawBaseEvent]] = { |
There was a problem hiding this comment.
question (bloacking): Do we really need to have this mapping? RawTaskEvent, RawStreamEvent and RawScheduledEvent have the same parent class.
There was a problem hiding this comment.
After applying changes proposed here I've removed this BASE_EVENT_CLS_TO_APP_TYPE_MAPPING at all
Thanks!
|
|
||
| def validate_manifested_type(manifest: Dict[str, Any], app_decorator_type: str) -> None: | ||
| manifest_app_type = manifest.get("application", {}).get("type") | ||
| if manifest_app_type and manifest_app_type != app_decorator_type: |
There was a problem hiding this comment.
question (blocking): What if we have manifest.json without type. Should this validation pass in this case?
There was a problem hiding this comment.
Also updated the logic for handle such cases, I believe if app type missed at manifest.json we anyway should try to validate app call basing on event payload.
But at the same time I've also added check for app_type, so if manifest has wrong app_type code will throw an error, please take a look over this test 🙏
| if merge_events: | ||
| aws_event = _merge_events(aws_event, data_transformation_type) | ||
|
|
||
| validate_app_type_context(aws_event, raw_event_type) |
There was a problem hiding this comment.
question(blocking): Should validation happen before calling anything else?
There was a problem hiding this comment.
@vladbagmet I've refactored a bit base_handler body 🙏
But anyway as I see, more convenient way to place validate_app_type_context func right after _merge_events(...) coz in this case merging validation logic called first + it's easier to guess_event_type(aws_event) if the event already merged 🤔 wdyt?
| ) | ||
|
|
||
|
|
||
| @functools.lru_cache(maxsize=None) |
There was a problem hiding this comment.
question (non-blocking): Is this cache going to be used between lamba function calls? If, so why don't we limit number of cached instances?
There was a problem hiding this comment.
you're right, limited to 1, thanks 🙌
| import pydantic | ||
|
|
||
|
|
||
| class AppType(str, Enum): |
There was a problem hiding this comment.
note: I like that we do not have mapping dict now. Also, is there a way to use already existing models for event to identify the app type?
There was a problem hiding this comment.
I'm not sure that I've got your point, could you please explain a bit wider 🙏
There was a problem hiding this comment.
neatpick: Seems like AppType is type of app defined in manifest.json.
| raw_event["event_type"] = "unknown_event_type" | ||
| with pytest.raises(ValidationError) as e: | ||
|
|
||
| with pytest.raises(RuntimeError) as e: |
There was a problem hiding this comment.
question (non-blocking): If app type is correct, but schema of this event is corrupted, should ValidationError still be raised?
There was a problem hiding this comment.
Seems you're right, I've updated a bit app logic and have reverted the test logic accordingly, thanks for reviewing 🙌
| if merge_events: | ||
| aws_event = _merge_events(aws_event, data_transformation_type) | ||
|
|
||
| if is_direct_app_call: |
There was a problem hiding this comment.
question (blocking): Should more specific check goes first?
| pass | ||
|
|
||
| @classmethod | ||
| def get_app_type(cls) -> AppType: |
There was a problem hiding this comment.
issue(blocking): Event payload types and type defined in manifest.json are not the same. What do you think about removing this method from the interface and do types comparison in "validate_app_type_context" method.
| return leaf_classes | ||
|
|
||
|
|
||
| def guess_event_type(aws_event: Any) -> Optional[Type[RawBaseEvent]]: |
There was a problem hiding this comment.
question(non-blocking): Can we rename it to get_event_type?
| ) | ||
|
|
||
|
|
||
| @functools.lru_cache(maxsize=1) |
There was a problem hiding this comment.
though(non-blocking): Please be advised that this approach can not work between lamba calls.
There was a problem hiding this comment.
But anyway, I think we can keep such logic here to be prepared for provisioned_concurrency in the future after changes on packager side are rolled out 🙏
vladbagmet
left a comment
There was a problem hiding this comment.
Blocking things are:
- Revert changes (def get_app_type(cls) -> AppType:)
- Change order of checks.
@vladbagmet proposed changes are integrated, thanks! |
Rationale
We have to add changes to be aligned with node-sdk changes
node-sdk Jira task
node-sdk PR
Changes
Mainly, changes are related to two validations:
app_typedeclared atmanifest.jsonfile and compare it withraw_event_typepassed at specific app decorator tobase_handlerand raise an error if a mismatch happensapp_typebased on the event payload passed tolambda_handlerand app decorator itself, the logic here is the same if they aren't matched - raise the errorSo thus, changes were encapsulated into function
validate_app_type_contextwhich can handle both1and2corva app use cases.Under the hood
validate_app_type_contexthas:validate_manifested_type: will be called only ifget_manifested_type()could extract correctapp_typename frommanifest.jsonfilevalidate_event_payload: will be called otherwise. Under the hoodget_event_typefunc will trying to find the appropriate child cls inherited fromRawBaseEventand then - validate matching.The idea is the following: each existing (and future added) specific event type eventually should be inherited from one of 3 raw
Raw{APP_TYPE}Eventclasses, so we can just check this "belonging"JIRA ticket
TODO