Skip to content
Merged
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
Prev Previous commit
Next Next commit
Push Coding Path for context
  • Loading branch information
yoiang committed Sep 11, 2018
commit 15540460ab7285875fe1349ee2137609a79c4192
57 changes: 55 additions & 2 deletions Sources/CSV/CSVReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ extension CSVReader {
}

private class CSVRowDecoder: Decoder {
let codingPath: [CodingKey]
var codingPath: [CodingKey]

let valuesByColumn: [String: String]

Expand Down Expand Up @@ -480,6 +480,9 @@ extension CSVReader {
// TODO: support DecodingError.keyNotFound
// TODO: support DecodingError.valueNotFound
func decode(_ type: Bool.Type, forKey key: K) throws -> Bool {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ Bool($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -488,6 +491,9 @@ extension CSVReader {
}

func decode(_ type: String.Type, forKey key: K) throws -> String {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ String($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -496,6 +502,9 @@ extension CSVReader {
}

func decode(_ type: Double.Type, forKey key: K) throws -> Double {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ Double($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -504,6 +513,9 @@ extension CSVReader {
}

func decode(_ type: Float.Type, forKey key: K) throws -> Float {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ Float($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -512,6 +524,9 @@ extension CSVReader {
}

func decode(_ type: Int.Type, forKey key: K) throws -> Int {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ Int($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -520,6 +535,9 @@ extension CSVReader {
}

func decode(_ type: Int8.Type, forKey key: K) throws -> Int8 {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ Int8($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -528,6 +546,9 @@ extension CSVReader {
}

func decode(_ type: Int16.Type, forKey key: K) throws -> Int16 {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ Int16($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -537,6 +558,9 @@ extension CSVReader {
}

func decode(_ type: Int32.Type, forKey key: K) throws -> Int32 {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ Int32($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -546,6 +570,9 @@ extension CSVReader {
}

func decode(_ type: Int64.Type, forKey key: K) throws -> Int64 {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ Int64($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -555,6 +582,9 @@ extension CSVReader {
}

func decode(_ type: UInt.Type, forKey key: K) throws -> UInt {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ UInt($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -564,6 +594,9 @@ extension CSVReader {
}

func decode(_ type: UInt8.Type, forKey key: K) throws -> UInt8 {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ UInt8($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -573,6 +606,9 @@ extension CSVReader {
}

func decode(_ type: UInt16.Type, forKey key: K) throws -> UInt16 {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ UInt16($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -582,6 +618,9 @@ extension CSVReader {
}

func decode(_ type: UInt32.Type, forKey key: K) throws -> UInt32 {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ UInt32($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -591,6 +630,9 @@ extension CSVReader {
}

func decode(_ type: UInt64.Type, forKey key: K) throws -> UInt64 {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let result = self.valueFor(column: key).flatMap({ UInt64($0) }) else {
throw DecodingError.typeMismatch(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...) value '\(self.valueFor(column: key) ?? "nil")'"))
Expand All @@ -600,6 +642,9 @@ extension CSVReader {
}

func decode<T>(_ type: T.Type, forKey key: K) throws -> T where T : Decodable {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

guard let stringValue = self.valueFor(column: key) else {
throw DecodingError.valueNotFound(type,
DecodingError.Context(codingPath: codingPath, debugDescription: "decode(...)"))
Expand All @@ -619,13 +664,19 @@ extension CSVReader {
}

func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: K) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

throw DecodingError.dataCorrupted(
DecodingError.Context(codingPath: codingPath,
debugDescription: "nestedContainer(...) CSV does not support nested values")
)
}

func nestedUnkeyedContainer(forKey key: K) throws -> UnkeyedDecodingContainer {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

throw DecodingError.dataCorrupted(
DecodingError.Context(codingPath: codingPath,
debugDescription: "nestedUnkeyedContainer(...) CSV does not support nested values")
Expand All @@ -640,6 +691,9 @@ extension CSVReader {
}

func superDecoder(forKey key: K) throws -> Decoder {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

throw DecodingError.dataCorrupted(
DecodingError.Context(codingPath: codingPath,
debugDescription: "CSV does not support nested values")
Expand All @@ -649,7 +703,6 @@ extension CSVReader {

}


public func readRow<T>() throws -> T? where T: Decodable {
guard let headerRow = self.headerRow else {
throw DecodingError.typeMismatch(T.self,
Expand Down