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 3 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.

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
16 changes: 15 additions & 1 deletion SwiftCSV/ParsingState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,22 @@
public enum CSVParseError: Error {
case generic(message: String)
case quotation(message: String)
case duplicateColumns(columnNames: [String])


public static func == (lhs: CSVParseError, rhs: CSVParseError) -> Bool {
switch (lhs, rhs) {
case (.generic(let message1), .generic(let message2)):
return message1 == message2
case (.quotation(let message1), .quotation(let message2)):
return message1 == message2
case (.duplicateColumns(let columns1), .duplicateColumns(let columns2)):
return columns1 == columns2
default:
return false
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I believe this is dead code a.t.m. and we can remove this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, has fixed these problems

}

/// 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")
}

}
Loading