Skip to content

fix: (CDK) (CsvParser) - Fix the \\ escaping when passing the delimiter from Builder's UI #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ class CsvParser(Parser):
encoding: Optional[str] = "utf-8"
delimiter: Optional[str] = ","

def _get_delimiter(self) -> Optional[str]:
"""
Get delimiter from the configuration. Check for the escape character and decode it.
"""
if self.delimiter is not None:
if self.delimiter.startswith("\\"):
self.delimiter = self.delimiter.encode("utf-8").decode("unicode_escape")

return self.delimiter

def parse(
self,
data: BufferedIOBase,
Expand All @@ -115,7 +125,7 @@ def parse(
Parse CSV data from decompressed bytes.
"""
text_data = TextIOWrapper(data, encoding=self.encoding) # type: ignore
reader = csv.DictReader(text_data, delimiter=self.delimiter or ",")
reader = csv.DictReader(text_data, delimiter=self._get_delimiter() or ",")
yield from reader


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def test_composite_raw_decoder_gzip_csv_parser(requests_mock, encoding: str):
)
response = requests.get("https://airbyte.io/", stream=True)

parser = GzipParser(inner_parser=CsvParser(encoding=encoding, delimiter="\t"))
# the delimiter is set to `\\t` intentionally to test the parsing logic here
parser = GzipParser(inner_parser=CsvParser(encoding=encoding, delimiter="\\t"))

composite_raw_decoder = CompositeRawDecoder(parser=parser)
counter = 0
for _ in composite_raw_decoder.decode(response):
Expand Down
Loading