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

Implement transformation of header #45

Closed
wants to merge 3 commits into from
Closed
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
10 changes: 4 additions & 6 deletions Frameworks/CSVImporter/CSVImporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public class CSVImporter<T> {
/// - recordMapper: A closure to map the dictionary data interpreted from a line to your data structure.
/// - Returns: `self` to enable consecutive method calls (e.g. `importer.startImportingRecords {...}.onProgress {...}`).
public func startImportingRecords(
structure structureClosure: @escaping (_ headerValues: [String]) -> Void,
structure structureClosure: @escaping (_ headerValues: [String]) -> [String],
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be marked as @discardableResult so code with current behavior doesn't break.

recordMapper closure: @escaping (_ recordValues: [String: String]) -> T
) -> Self {
workDispatchQueue.async {
Expand All @@ -185,8 +185,7 @@ public class CSVImporter<T> {

let importedLinesWithSuccess = self.importLines { valuesInLine in
if recordStructure == nil {
recordStructure = valuesInLine
structureClosure(valuesInLine)
recordStructure = structureClosure(valuesInLine)
} else {
if let structuredValuesInLine = [String: String](keys: recordStructure!, values: valuesInLine) {
let newRecord = closure(structuredValuesInLine)
Expand Down Expand Up @@ -235,16 +234,15 @@ public class CSVImporter<T> {
/// - recordMapper: A closure to map the dictionary data interpreted from a line to your data structure.
/// - Returns: The imported records array.
public func importRecords(
structure structureClosure: @escaping (_ headerValues: [String]) -> Void,
structure structureClosure: @escaping (_ headerValues: [String]) -> [String],
Copy link
Member

Choose a reason for hiding this comment

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

See above.

recordMapper closure: @escaping (_ recordValues: [String: String]) -> T
) -> [T] {
var recordStructure: [String]?
var importedRecords = [T]()

_ = self.importLines { valuesInLine in
if recordStructure == nil {
recordStructure = valuesInLine
structureClosure(valuesInLine)
recordStructure = structureClosure(valuesInLine)
} else {
if let structuredValuesInLine = [String: String](keys: recordStructure!, values: valuesInLine) {
let newRecord = closure(structuredValuesInLine)
Expand Down