Skip to content

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

Merged
merged 2 commits into from
Sep 11, 2020
Merged

Fix CSV.filter to preserve headers #174

merged 2 commits into from
Sep 11, 2020

Conversation

BurdetteLamar
Copy link
Member

No description provided.

@BurdetteLamar BurdetteLamar requested a review from kou September 11, 2020 19:41
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)
Copy link
Member

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
Copy link
Member

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|
Copy link
Member

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.

@BurdetteLamar
Copy link
Member Author

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?

@kou
Copy link
Member

kou commented Sep 11, 2020

OK. I take over this.

@kou kou merged commit 203c5e0 into ruby:master Sep 11, 2020
@kou
Copy link
Member

kou commented Sep 11, 2020

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants