Made Dictionary<String, Array<Data>> extension generic where Value is Collection<OptionalType>

This commit is contained in:
Caleb Kleveter 2018-05-08 16:34:10 -05:00
parent 703e7e7485
commit af2753a573
No known key found for this signature in database
GPG Key ID: 30A5A6F3A0ED7EDA
2 changed files with 6 additions and 5 deletions

View File

@ -11,7 +11,7 @@ let package = Package(
.package(url: "https://github.com/vapor/core.git", from: "3.0.0")
],
targets: [
.target(name: "CSV", dependencies: ["Bits", "Debugging", "Async"]),
.target(name: "CSV", dependencies: ["Bits", "Debugging", "Async", "Core"]),
.testTarget(name: "CSVTests", dependencies: ["CSV"]),
]
)

View File

@ -1,4 +1,5 @@
import Foundation
import Core
final class _CSVUnkeyedDecoder: UnkeyedDecodingContainer {
let codingPath: [CodingKey]
@ -80,15 +81,15 @@ final class _CSVUnkeyedDecoder: UnkeyedDecodingContainer {
}
}
extension Dictionary where Key == String, Value == Array<Data?> {
public func makeRows() -> () -> [String: Data]? {
extension Dictionary where Key == String, Value: Collection, Value.Element: OptionalType, Value.Index == Int {
public func makeRows() -> () -> [String: Value.Element.WrappedType]? {
guard let columnCount = self.first?.value.count else { return { return nil } }
var rowIndex = 0
func next() -> [String: Data]? {
func next() -> [String: Value.Element.WrappedType]? {
defer { rowIndex += 1 }
guard rowIndex < columnCount else { return nil }
return self.mapValues { $0[rowIndex] }.filter { $0.value != nil }.mapValues { $0! }
return self.mapValues { $0[rowIndex] }.filter { $0.value.wrapped != nil }.mapValues { $0.wrapped! }
}
return next