Skip to content

Commit

Permalink
Handle unknown event types with fallback value (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
allenporter authored Oct 13, 2024
1 parent e948380 commit 56387b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
20 changes: 19 additions & 1 deletion gcal_sync/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ class EventTypeEnum(str, Enum):
FROM_GMAIL = "fromGmail"
"""An event from Gmail."""

BIRTHDAY = "birthday"
"""A special all-day event with an annual recurrence."""

UNKNOWN = "unknown"
"""An unknown event type."""


class VisibilityEnum(str, Enum):
"""Visibility of the event."""
Expand Down Expand Up @@ -579,7 +585,10 @@ class Event(CalendarBaseModel):
That is, most use cases should not need to involve checking the status.
"""

event_type: EventTypeEnum = Field(alias="eventType", default=EventTypeEnum.DEFAULT)
event_type: EventTypeEnum = Field(
alias="eventType",
default=EventTypeEnum.DEFAULT,
)
"""Specific type of the event."""

visibility: VisibilityEnum = VisibilityEnum.DEFAULT
Expand Down Expand Up @@ -734,6 +743,15 @@ def _adjust_recurrence_date(
return date_value.date()
return date_value

@root_validator(pre=True)
def _adjust_unknown_event_type(cls, values: dict[str, Any]) -> dict[str, Any]:
"""Validate the event type."""
if event_type := values.get("eventType"):
if event_type not in [member.value for member in EventTypeEnum]:
_LOGGER.debug("Unknown event type: %s", event_type)
values["eventType"] = EventTypeEnum.UNKNOWN
return values

@property
def timespan(self) -> Timespan:
"""Return a timespan representing the event start and end."""
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = gcal_sync
version = 6.1.5
version = 6.1.6
description = A python library for syncing Google Calendar to local storage
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
2 changes: 2 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ def test_required_fields() -> None:
("outOfOffice", EventTypeEnum.OUT_OF_OFFICE),
("workingLocation", EventTypeEnum.WORKING_LOCATION),
("fromGmail", EventTypeEnum.FROM_GMAIL),
("birthday", EventTypeEnum.BIRTHDAY),
("some-event-type", EventTypeEnum.UNKNOWN),
],
)
def test_event_type(api_event_type: str, event_type: EventTypeEnum) -> None:
Expand Down

0 comments on commit 56387b8

Please sign in to comment.