Skip to content
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
14 changes: 9 additions & 5 deletions flow/record/tools/rdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,14 @@ def main(argv: list[str] | None = None) -> int:
root_logger.handlers.clear()
root_logger.addHandler(handler)

fields_to_exclude = args.exclude.split(",") if args.exclude else []
fields = args.fields.split(",") if args.fields else []
fields_to_exclude = list(filter(None, map(str.strip, args.exclude.split(",")))) if args.exclude else []
fields = list(filter(None, map(str.strip, args.fields.split(",")))) if args.fields else []

writer_options = {}
if fields:
writer_options["fields"] = fields
if fields_to_exclude:
writer_options["exclude"] = fields_to_exclude

if args.list_adapters:
list_adapters()
Expand All @@ -340,8 +346,6 @@ def main(argv: list[str] | None = None) -> int:
}
uri = mode_to_uri.get(args.mode, uri)
qparams = {
"fields": args.fields,
"exclude": args.exclude,
"format_spec": args.format,
}
query = urlencode({k: v for k, v in qparams.items() if v})
Expand Down Expand Up @@ -393,7 +397,7 @@ def main(argv: list[str] | None = None) -> int:
ret = 0

try:
with RecordWriter(uri) as record_writer:
with RecordWriter(uri, **writer_options) as record_writer:
for count, rec in enumerate(record_iterator, start=1): # noqa: B007
if args.record_source is not None:
rec._source = args.record_source
Expand Down
37 changes: 37 additions & 0 deletions tests/tools/test_rdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,40 @@ def test_rdump_print_error_notes(
rdump.main([str(path), "-vvv"])

capsys.readouterr()


def test_rdump_fields_with_spaces(tmp_path: Path, capsysbinary: pytest.CaptureFixture) -> None:
"""Test if rdump handles spaces in field names gracefully."""
TestRecord = RecordDescriptor(
"test/record",
[
("varint", "count"),
("string", "foo"),
("string", "bar"),
],
)

path = tmp_path / "test.records"
out_path = tmp_path / "out.records"
with RecordWriter(path) as writer:
writer.write(TestRecord(count=0, foo="bar", bar="baz"))

# test if fields works with spaces in the name
rdump.main([str(path), "--fields", "foo, count ", "-w", str(out_path)])
with RecordReader(out_path) as reader:
records = list(reader)
assert len(records) == 1
assert list(records[0]._desc.fields.keys()) == ["foo", "count"]

# test if exclude works with spaces in the field names
rdump.main([str(path), "--exclude", " foo, bar ", "-w", str(out_path)])
with RecordReader(out_path) as reader:
records = list(reader)
assert len(records) == 1
assert list(records[0]._desc.fields.keys()) == ["count"]

# also test an adapter
rdump.main([str(path), "--exclude", " foo, bar ", "--csv"])
captured = capsysbinary.readouterr()
assert captured.err == b""
assert b"count,_source,_classification,_generated,_version\r\n" in captured.out