Skip to content

Commit 8d41f74

Browse files
[io] feat: add jsonl format to ConsoleWriter
1 parent 7e251f2 commit 8d41f74

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

libs/garf_io/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Currently it supports writing data to the following destination:
99

1010
| identifier | Writer | Options |
1111
|------------| ---------------- | -------- |
12-
| `console` | ConsoleWriter | `page-size=10`,`format=table\|json`|
12+
| `console` | ConsoleWriter | `page-size=10`,`format=table\|json\|jsonl`|
1313
| `csv` | CsvWriter | `destination-folder` |
1414
| `json` | JsonWriter | `destination-folder`,`format=json\|jsonl`|
1515
| `bq` | BigQueryWriter | `project`, `dataset`, `location`, `write-disposition` |

libs/garf_io/garf_io/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
__version__ = '0.0.6'
15+
__version__ = '0.0.7'

libs/garf_io/garf_io/writers/console_writer.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
from __future__ import annotations
1717

18+
from typing import Literal
19+
1820
import rich
1921
from rich import console, table
20-
from rich import json as rich_json
2122

2223
from garf_core import report as garf_report
2324
from garf_io.writers import abs_writer
@@ -32,13 +33,16 @@ class ConsoleWriter(abs_writer.AbsWriter):
3233
"""
3334

3435
def __init__(
35-
self, page_size: int = 10, format: str = 'table', **kwargs: str
36+
self,
37+
page_size: int = 10,
38+
format: Literal['table', 'json', 'jsonl'] = 'table',
39+
**kwargs: str,
3640
) -> None:
3741
"""Initializes ConsoleWriter.
3842
3943
Args:
4044
page_size: How many row of report should be written
41-
format: Type of output ('table', 'json').
45+
format: Type of output.
4246
kwargs: Optional parameter to initialize writer.
4347
"""
4448
super().__init__(**kwargs)
@@ -68,5 +72,7 @@ def write(self, report: garf_report.GarfReport, destination: str) -> None:
6872
if i < self.page_size:
6973
output_table.add_row(*[str(field) for field in row])
7074
elif self.format == 'json':
71-
output_table = rich_json.JSON(report.to_json())
75+
output_table = report.to_json()
76+
elif self.format == 'jsonl':
77+
output_table = report.to_jsonl()
7278
console.Console().print(output_table)

libs/garf_io/tests/unit/writers/test_console_writer.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,30 @@ def test_write_multi_column_report_with_arrays_returns_concatenated_strings(
9494
expected_result = json.loads(sample_data.to_json())
9595
res = json.loads(prepared_output.replace("'", '"'))
9696
assert res == expected_result
97+
98+
99+
class TestConsoleJsonlWriter:
100+
@pytest.fixture
101+
def console_writer(self):
102+
return console_writer.ConsoleWriter(format='jsonl')
103+
104+
def test_write_multi_column_report_returns_arrays(
105+
self, capsys, console_writer, sample_data
106+
):
107+
console_writer.array_handling = 'arrays'
108+
console_writer.write(sample_data, _TMP_NAME)
109+
output = capsys.readouterr().out.strip()
110+
for i, res in enumerate(output.split('\n')):
111+
expected_result = sample_data[i].to_dict()
112+
assert json.loads(res) == expected_result
113+
114+
def test_write_multi_column_report_with_arrays_returns_concatenated_strings(
115+
self, capsys, console_writer, sample_data
116+
):
117+
console_writer.array_handling = 'strings'
118+
console_writer.write(sample_data, _TMP_NAME)
119+
output = capsys.readouterr().out.strip()
120+
for i, res in enumerate(output.split('\n')):
121+
expected_result = sample_data[i].to_dict()
122+
expected_result['column_3'] = '3|4'
123+
assert json.loads(res) == expected_result

0 commit comments

Comments
 (0)