Skip to content

Commit 349d049

Browse files
authored
Merge pull request #13 from llakenbr/feature/tuple-support
Added support for Tuple data type encoding
2 parents b9a5204 + bedbd12 commit 349d049

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

tests/test_encoder.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ def test_encode_primitive_array():
8888
result = encode(data)
8989
assert result == 'items: [hello,"world, test",foo]'
9090

91+
def test_encode_tuple_array():
92+
"""Test encoding of primitive arrays."""
93+
# Number tuple
94+
assert encode({'numbers': (1, 2, 3)}) == 'numbers: [1,2,3]'
95+
96+
# String tuple
97+
assert encode({'names': ('Alice', 'Bob')}) == 'names: [Alice,Bob]'
98+
99+
# Mixed primitive tuple
100+
assert encode({'mixed': (1, 'text', True, None)}) == 'mixed: [1,text,true,null]'
101+
91102

92103
def test_encode_array_delimiter():
93104
"""Test different array delimiters."""

toon/encoder.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ def _encode_value(value: Any, level: int, opts: EncoderOptions) -> str:
117117
return _encode_array(value, level, opts)
118118
elif isinstance(value, dict):
119119
return _encode_object(value, level, opts)
120+
elif isinstance(value, tuple):
121+
return _encode_tuple(value)
120122
else:
121-
# Handle other types as null
122-
return 'null'
123+
# Handle other types with NotImplementedError
124+
raise NotImplementedError(f'Encoding for type {type(value)} is not implemented.')
123125

124126

125127
def _encode_object(obj: dict, level: int, opts: EncoderOptions) -> str:
@@ -177,6 +179,16 @@ def _encode_array(arr: list, level: int, opts: EncoderOptions) -> str:
177179
# Mixed array (list format)
178180
return _encode_list_array(arr, level, opts, key=None)
179181

182+
def _encode_tuple(value: tuple) -> str:
183+
"""Encode a tuple."""
184+
if not value:
185+
return '[]'
186+
187+
tuple_data = ','.join(_encode_primitive_value(v) for v in value)
188+
tuple_string = f'[{tuple_data}]'
189+
190+
return tuple_string
191+
180192

181193
def _encode_array_with_key(key: str, arr: list, level: int, opts: EncoderOptions) -> str:
182194
"""Encode an array with its key prefix for object context."""

0 commit comments

Comments
 (0)