Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/BuzzCutNorman/sdk into 1046…
Browse files Browse the repository at this point in the history
…-feat-faster-json-dumps-with-msgspec
  • Loading branch information
BuzzCutNorman committed Dec 6, 2023
2 parents 21fbc60 + fe0ff85 commit 257f312
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
41 changes: 37 additions & 4 deletions singer_sdk/_singerlib/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from __future__ import annotations

import enum
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 @@ -23,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 @@ -198,3 +209,25 @@ class ActivateVersionMessage(Message):
def __post_init__(self) -> None:
"""Post-init processing."""
self.type = SingerMessageType.ACTIVATE_VERSION


def format_message(message: Message) -> str:
"""Format a message as a JSON string.
Args:
message: The message to format.
Returns:
The formatted message.
"""
return json.dumps(message.to_dict(), use_decimal=True, default=_default_encoding)


def write_message(message: Message) -> None:
"""Write a message to stdout.
Args:
message: The message to write.
"""
sys.stdout.write(format_message(message) + "\n")
sys.stdout.flush()
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 257f312

Please sign in to comment.