-
Notifications
You must be signed in to change notification settings - Fork 119
Add quoted information to CSV::FieldInfo #252 #253
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
55d6e87
to
344894d
Compare
344894d
to
d5ea835
Compare
d5ea835
to
5d3273d
Compare
I rebased feat/issue-252 to the latest HEAD. |
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.
Thanks!
(If you want me to write code instead of commenting, please let me know.)
test/csv/parse/test_convert.rb
Outdated
field.to_sym | ||
end | ||
expected = [["serial", :value], ["109", "12"], ["10A", "13"]] | ||
row = CSV.parse(@str, headers: true, header_converters: quoted_header_converter) |
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.
Could you also add CSV.parse(..., headers: '"serial",value', ...)
(specifying headers as String
) case?
Thanks! I will check. |
I will add test case in:
Please hold approving. |
f2673da
to
969ce83
Compare
I added and fixed except 3rd one:
This one requires fix in Current CSV has a bug in converting when specified header has a Symbol. CSV.parse("1,2", headers: ["string", :symbol], header_converters: :symbol)
|
How about this? diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb
index c3c1eff..cd5a45f 100644
--- a/lib/csv/parser.rb
+++ b/lib/csv/parser.rb
@@ -758,9 +758,10 @@ class CSV
case headers
when Array
@raw_headers = headers
+ quoted_fields = [false] * @raw_headers.size
@use_headers = true
when String
- @raw_headers = parse_headers(headers)
+ @raw_headers, quoted_fields = parse_headers(headers)
@use_headers = true
when nil, false
@raw_headers = nil
@@ -770,20 +771,24 @@ class CSV
@use_headers = true
end
if @raw_headers
- # Set 'quoted?' info to false to avoid re-converting
- @headers = adjust_headers(@raw_headers, [true] * @raw_headers.size)
+ @headers = adjust_headers(@raw_headers, quoted_fields)
else
@headers = nil
end
end
def parse_headers(row)
- converters = @return_headers ? [] : @header_fields_converter.to_a
- CSV.parse_line(row,
- col_sep: @column_separator,
- row_sep: @row_separator,
- quote_char: @quote_character,
- converters: converters)
+ quoted_fields = []
+ converter = lambda do |field, info|
+ quoted_fields << info.quoted?
+ field
+ end
+ headers = CSV.parse_line(row,
+ col_sep: @column_separator,
+ row_sep: @row_separator,
+ quote_char: @quote_character,
+ converters: [converter])
+ [headers, quoted_fields]
end
def adjust_headers(headers, quoted_fields) |
c59cde2
to
42e0cfe
Compare
Thank you!! I reflected this code! |
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.
Thanks!
It looks almost good! Could you confirm my comments?
test/csv/parse/test_convert.rb
Outdated
|
||
sub_test_case("converter with #encode for Symbols") do | ||
def test_encode_symbol | ||
assert_nothing_raised { CSV.parse("1,2", headers: ["string", :symbol] , header_converters: :symbol) } |
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.
It seems that this is not related to this pull request.
Could you remove this from this pull request? If this is needed, please open another pull request.
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.
Thank you. I removed last one and create modified commit.
I will consider more for another pull request about converting when specified header has a Symbol.
1b6e3e8
to
f0248f0
Compare
Thanks! |
This is a proposal to fix the issue #252;
:quoted?
toFieldInfo
.Introduce an array of booleans@quoted_fields
in Parser to collect original column value was quoted?.parse_no_quote
,parse_quotable_loose
andparse_quotable_robust
.quoted_fields
to collect booleans in each method and pass it through#emit_row
.