Skip to content

Commit

Permalink
refactor: Explicitly use T iso date separator (#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon authored Dec 6, 2023
1 parent 4e82c65 commit fe0ff85
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions singer_sdk/_singerlib/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import sys
import typing as t
from dataclasses import asdict, dataclass, field
from datetime import timezone
from datetime import datetime, timezone

import simplejson as json
from dateutil.parser import parse

if t.TYPE_CHECKING:
from datetime import datetime


class SingerMessageType(str, enum.Enum):
"""Singer specification message types."""
Expand All @@ -25,6 +22,18 @@ class SingerMessageType(str, enum.Enum):
BATCH = "BATCH"


def _default_encoding(obj: t.Any) -> str: # noqa: ANN401
"""Default JSON encoder.
Args:
obj: The object to encode.
Returns:
The encoded object.
"""
return obj.isoformat(sep="T") if isinstance(obj, datetime) else str(obj)


def exclude_null_dict(pairs: list[tuple[str, t.Any]]) -> dict[str, t.Any]:
"""Exclude null values from a dictionary.
Expand Down Expand Up @@ -211,7 +220,7 @@ def format_message(message: Message) -> str:
Returns:
The formatted message.
"""
return json.dumps(message.to_dict(), use_decimal=True, default=str)
return json.dumps(message.to_dict(), use_decimal=True, default=_default_encoding)


def write_message(message: Message) -> None:
Expand Down
2 changes: 1 addition & 1 deletion singer_sdk/helpers/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def to_json_compatible(val: t.Any) -> t.Any: # noqa: ANN401
If given a naive datetime object, pendulum automatically makes it utc
"""
if isinstance(val, (datetime.datetime, pendulum.DateTime)):
return pendulum.instance(val).isoformat()
return pendulum.instance(val).isoformat("T")
return val


Expand Down

0 comments on commit fe0ff85

Please sign in to comment.