Skip to content
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

Fix issue with duplicate column name #136

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Binary file added .DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Bugfixes:

- Strip byte order mark from all input strings, not just when loading files (#128) -- @Diggory

## 0.8.2

Bugfixes:

- Throw an error when encountering duplicate column names in CSV headers (#136) -- @TomorrowMC

## 0.8.1

Bugfixes:
Expand Down
13 changes: 13 additions & 0 deletions SwiftCSV/Array+duplicates.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Array+duplicates.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 }
}
}
6 changes: 6 additions & 0 deletions SwiftCSV/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.duplicateColumns(columnNames: duplicateColumns)
}

// Start after the header
try enumerateAsArray(text: content, delimiter: delimiter, startAt: 1, rowLimit: rowLimit) { fields in
Expand Down
4 changes: 3 additions & 1 deletion SwiftCSV/ParsingState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
public enum CSVParseError: Error {
case generic(message: String)
case quotation(message: String)
case duplicateColumns(columnNames: [String])


}

/// State machine of parsing CSV contents character by character.
struct ParsingState {

Expand Down
64 changes: 64 additions & 0 deletions SwiftCSVTests/DuplicateColumnNameHandlingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// 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
switch error as? CSVParseError {
case .duplicateColumns(let columnNames):
XCTAssertEqual(["name"], columnNames)
default:
XCTFail("Expected CSVParseError.duplicateColumns")
}
}
}

func testNoDuplicateColumnNames() throws {
let csvString = """
id,name,age
1,John,23
2,Jane,25
"""

let csvError = try CSV<Named>(string: csvString)
let csvRandom = try CSV<Named>(string: csvString)

XCTAssertEqual(csvError.header, ["id", "name", "age"])
XCTAssertEqual(csvRandom.header, ["id", "name", "age"])

XCTAssertEqual(csvError.rows.count, 2)
XCTAssertEqual(csvRandom.rows.count, 2)

XCTAssertEqual(csvError.rows[0]["id"], "1")
XCTAssertEqual(csvError.rows[0]["name"], "John")
XCTAssertEqual(csvError.rows[0]["age"], "23")

XCTAssertEqual(csvRandom.rows[0]["id"], "1")
XCTAssertEqual(csvRandom.rows[0]["name"], "John")
XCTAssertEqual(csvRandom.rows[0]["age"], "23")

XCTAssertEqual(csvError.rows[1]["id"], "2")
XCTAssertEqual(csvError.rows[1]["name"], "Jane")
XCTAssertEqual(csvError.rows[1]["age"], "25")

XCTAssertEqual(csvRandom.rows[1]["id"], "2")
XCTAssertEqual(csvRandom.rows[1]["name"], "Jane")
XCTAssertEqual(csvRandom.rows[1]["age"], "25")
}

}