Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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/Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Extension.swift
Copy link
Contributor

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

//
//
// 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.generic(message: "Duplicate column names found: \(duplicateColumns.joined(separator: ", "))")
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool. I think this warrants a new error case 👍

case duplicateColumns(columnNames: [String]) or something like it maybe.

}

// Start after the header
try enumerateAsArray(text: content, delimiter: delimiter, startAt: 1, rowLimit: rowLimit) { fields in
Expand Down
14 changes: 12 additions & 2 deletions SwiftCSV/ParsingState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@
// Copyright © 2016 Naoto Kaneko. All rights reserved.
//

public enum CSVParseError: Error {
public enum CSVParseError: Error, Equatable {
case generic(message: String)
case quotation(message: 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
default:
return false
}
}
}

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

Expand Down
59 changes: 59 additions & 0 deletions SwiftCSVTests/DuplicateColumnNameHandlingTests.swift
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"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you try to replace the equality check with a switch-case for this error?

Suggested change
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.

}
}

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

}