Skip to content

Commit

Permalink
Re-add SingerReader.deserialize_json
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Jul 12, 2024
1 parent 5cfa21e commit b9faa7f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 12 additions & 1 deletion singer_sdk/io_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ def _assert_line_requires(line_dict: dict, requires: set[str]) -> None:
msg = f"Line is missing required {', '.join(missing)} key(s): {line_dict}"
raise InvalidInputLine(msg)

def deserialize_json(self, line: str) -> dict: # noqa: PLR6301
"""Deserialize a line of json.
Args:
line: A single line of json.
Returns:
A dictionary of the deserialized json.
"""
return deserialize_json(line)

def _process_lines(self, file_input: t.IO[str]) -> t.Counter[str]:
"""Internal method to process jsonl lines from a Singer tap.
Expand All @@ -62,7 +73,7 @@ def _process_lines(self, file_input: t.IO[str]) -> t.Counter[str]:
"""
stats: dict[str, int] = defaultdict(int)
for line in file_input:
line_dict = deserialize_json(line)
line_dict = self.deserialize_json(line)
self._assert_line_requires(line_dict, requires={"type"})

record_type: SingerMessageType = line_dict["type"]
Expand Down
8 changes: 5 additions & 3 deletions tests/core/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import pytest

from singer_sdk._singerlib import RecordMessage
from singer_sdk._singerlib.serde import deserialize_json
from singer_sdk.io_base import SingerReader, SingerWriter


Expand Down Expand Up @@ -53,8 +52,9 @@ def _process_state_message(self, message_dict: dict) -> None:
],
)
def test_deserialize(line, expected, exception):
reader = DummyReader()
with exception:
assert deserialize_json(line) == expected
assert reader.deserialize_json(line) == expected


# Benchmark Tests
Expand Down Expand Up @@ -104,8 +104,10 @@ def test_bench_deserialize_json(benchmark, bench_encoded_record):
"""Run benchmark for Sink._validator method validate."""
number_of_runs = 1000

reader = DummyReader()

def run_deserialize_json():
for record in itertools.repeat(bench_encoded_record, number_of_runs):
deserialize_json(record)
reader.deserialize_json(record)

benchmark(run_deserialize_json)

0 comments on commit b9faa7f

Please sign in to comment.