-
Notifications
You must be signed in to change notification settings - Fork 196
Fix issue with duplicate column name #136
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
135fbf1
67b115e
d0839bf
07aab19
4bd5436
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // | ||
| // Extension.swift | ||
| // | ||
| // | ||
| // Created by 胡逸飞 on 2024/4/26. | ||
| // | ||
|
|
||
| extension Array where Element: Hashable { | ||
| func duplicates() -> [Element] { | ||
| let counts = self.reduce(into: [:]) { counts, element in counts[element, default: 0] += 1 } | ||
| return counts.filter { $0.value > 1 }.map { $0.key } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,6 +126,12 @@ enum Parser { | |
| static func enumerateAsDict(header: [String], content: String, delimiter: CSVDelimiter, rowLimit: Int? = nil, block: @escaping ([String : String]) -> ()) throws { | ||
|
|
||
| let enumeratedHeader = header.enumerated() | ||
|
|
||
| // Check for duplicate column names | ||
| let duplicateColumns = header.duplicates() | ||
| if !duplicateColumns.isEmpty { | ||
| throw CSVParseError.generic(message: "Duplicate column names found: \(duplicateColumns.joined(separator: ", "))") | ||
|
||
| } | ||
|
|
||
| // Start after the header | ||
| try enumerateAsArray(text: content, delimiter: delimiter, startAt: 1, rowLimit: rowLimit) { fields in | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,59 @@ | ||||||||||||
| // | ||||||||||||
| // DuplicateColumnNameHandlingTests.swift | ||||||||||||
| // | ||||||||||||
| // | ||||||||||||
| // Created by 胡逸飞 on 2024/4/27. | ||||||||||||
| // | ||||||||||||
|
|
||||||||||||
| import Foundation | ||||||||||||
| import XCTest | ||||||||||||
| @testable import SwiftCSV | ||||||||||||
|
|
||||||||||||
| class DuplicateColumnNameHandlingTests: XCTestCase { | ||||||||||||
|
|
||||||||||||
| func testErrorOnDuplicateColumnNames() throws { | ||||||||||||
| let csvString = """ | ||||||||||||
| id,name,age,name | ||||||||||||
| 1,John,23,John Doe | ||||||||||||
| 2,Jane,25,Jane Doe | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
| XCTAssertThrowsError(try CSV<Named>(string: csvString)) { error in | ||||||||||||
| XCTAssertEqual(error as? CSVParseError, CSVParseError.generic(message: "Duplicate column names found: name")) | ||||||||||||
|
||||||||||||
| XCTAssertEqual(error as? CSVParseError, CSVParseError.generic(message: "Duplicate column names found: name")) | |
| switch error as? CSVParseError { | |
| case .generic(message: let message): XCTAssertEqual("Duplicate column names found: name", message) | |
| default: XCTFail("Expected CSVParseError.generic") | |
| } |
If that works, please revert the Equatable conformance of the error afterwards.
Motivation: there's just this one place in tests where equatability is used, and the conformance affects public SwiftCSV API. We can't undo that easily after we ship it, and there's no long-term motivation to make the errors equatable in apps. switch-case is the best bet there, too.
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.
Please rename the file to
Array+duplicates.swift