Skip to content

Make enumerateAsDict() Accept a Throwing Function #139

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

Open
wants to merge 1 commit 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
22 changes: 15 additions & 7 deletions SwiftCSV/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension CSV {
try Parser.enumerateAsArray(text: self.text, delimiter: self.delimiter, startAt: startAt, rowLimit: rowLimit, rowCallback: rowCallback)
}

public func enumerateAsDict(_ block: @escaping ([String : String]) -> ()) throws {
public func enumerateAsDict(_ block: @escaping ([String : String]) throws -> ()) throws {

try Parser.enumerateAsDict(header: self.header, content: self.text, delimiter: self.delimiter, block: block)
}
Expand Down Expand Up @@ -60,7 +60,7 @@ enum Parser {
delimiter: CSVDelimiter,
startAt offset: Int = 0,
rowLimit: Int? = nil,
rowCallback: @escaping ([String]) -> ()) throws {
rowCallback: @escaping ([String]) throws -> ()) throws {
let maxRowIndex = rowLimit.flatMap { $0 < 0 ? nil : offset + $0 }

var currentIndex = text.startIndex
Expand All @@ -72,7 +72,7 @@ enum Parser {

var rowIndex = 0

func finishRow() {
func finishRow() throws {
defer {
rowIndex += 1
fields = []
Expand All @@ -81,7 +81,11 @@ enum Parser {

guard rowIndex >= offset else { return }
fields.append(String(field))
rowCallback(fields)
do {
try rowCallback(fields)
} catch {
throw CSVParseError.generic(message: "Error thrown by block for row \(rowIndex)")
Copy link
Contributor

Choose a reason for hiding this comment

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

You're right, this doesn't change usage because the function is throwing anyway.

But could you avoid catching, wrapping, and rethrowing the error? User code throwing a MyVerySpecificError on failure in this callback will likely want to work with and maybe even catch MyVerySpecificError, not CSVParseError.generic, losing the original error's information completely here.

For context: I'm also not a fan of wrapping-and-rethrowing, because that adds another step to the debugging flow when you need to inspect the stack.

Just let rowCallback's error propagate will do The Right Thing in more cases 👍

}
}

var state: ParsingState = ParsingState(
Expand Down Expand Up @@ -118,12 +122,16 @@ enum Parser {
}

if !fields.isEmpty {
rowCallback(fields)
do {
try rowCallback(fields)
} catch {
throw CSVParseError.generic(message: "Error thrown by block for row \(rowIndex)")
}
Comment on lines +125 to +129
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above here, please:

Suggested change
do {
try rowCallback(fields)
} catch {
throw CSVParseError.generic(message: "Error thrown by block for row \(rowIndex)")
}
try rowCallback(fields)

}
}
}

static func enumerateAsDict(header: [String], content: String, delimiter: CSVDelimiter, rowLimit: Int? = nil, block: @escaping ([String : String]) -> ()) throws {
static func enumerateAsDict(header: [String], content: String, delimiter: CSVDelimiter, rowLimit: Int? = nil, block: @escaping ([String : String]) throws -> ()) throws {

let enumeratedHeader = header.enumerated()

Expand All @@ -133,7 +141,7 @@ enum Parser {
for (index, head) in enumeratedHeader {
dict[head] = index < fields.count ? fields[index] : ""
}
block(dict)
try block(dict)
}
}
}
10 changes: 5 additions & 5 deletions SwiftCSV/ParsingState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ struct ParsingState {
private(set) var innerQuotes = false

let delimiter: Character
let finishRow: () -> Void
let finishRow: () throws -> Void
let appendChar: (Character) -> Void
let finishField: () -> Void

init(delimiter: Character,
finishRow: @escaping () -> Void,
finishRow: @escaping () throws -> Void,
appendChar: @escaping (Character) -> Void,
finishField: @escaping () -> Void) {

Expand All @@ -44,7 +44,7 @@ struct ParsingState {
} else if char == delimiter {
finishField()
} else if char.isNewline {
finishRow()
try finishRow()
} else if char.isWhitespace {
// ignore whitespaces between fields
} else {
Expand Down Expand Up @@ -72,7 +72,7 @@ struct ParsingState {
atStart = true
parsingField = false
innerQuotes = false
finishRow()
try finishRow()
} else {
appendChar(char)
}
Expand All @@ -91,7 +91,7 @@ struct ParsingState {
atStart = true
parsingQuotes = false
innerQuotes = false
finishRow()
try finishRow()
} else if char.isWhitespace {
// ignore whitespaces between fields
} else {
Expand Down