-
Notifications
You must be signed in to change notification settings - Fork 119
Fix CSV.filter to preserve headers #174
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
Conversation
lib/csv.rb
Outdated
@@ -1058,9 +1058,11 @@ def filter(input=nil, output=nil, **options) | |||
end | |||
end | |||
# build input and output wrappers | |||
input = new(input || ARGF, **in_options) | |||
input = parse(input || ARGF, **in_options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not use parse
here because parse
parses all input at once. It may consume large resource.
|
||
if input.instance_of?(CSV::Table) | ||
output << input.headers | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds headers twice with the following case:
def test_filter_with_headers
input = <<-CSV
foo,0
bar,1
baz,2
CSV
output = ""
CSV.filter(input, output, headers: ["Name", "Value"], output_write_headers: true) do |row|
row
end
assert_equal(input, output)
end
baz,2 | ||
CSV | ||
output = "" | ||
CSV.filter(input, output, headers: true, write_headers: true) do |row| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use output_write_headers:
here. If we use write_headers:
, input
is also rewritten.
OK, thanks. I don't have a proper fix for this, but I do think it's a bug, and I think the test I've included is valid. Do you have a good fix for this? |
OK. I take over this. |
Done. |
No description provided.