Skip to content

Commit c209116

Browse files
committed
small improvements
1 parent ca3160c commit c209116

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

Sources/swift-csv/CSVLineDecoder.swift

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99

1010
final class CSVLineDecoder: Decoder {
11-
var tempData: String?
11+
1212

1313
var codingPath: [any CodingKey] = []
1414

@@ -25,18 +25,19 @@ final class CSVLineDecoder: Decoder {
2525
}
2626

2727
func unkeyedContainer() throws -> any UnkeyedDecodingContainer {
28-
fatalError()
28+
fatalError("Not supported yet. Please file a bug report.")
2929
}
3030

3131
func singleValueContainer() throws -> any SingleValueDecodingContainer {
32-
SVD(codingPath: codingPath, value: tempData!, decoder: self)
32+
SVD(codingPath: codingPath, data: decoderData, decoder: self)
3333
}
3434
}
3535

3636
extension CSVLineDecoder {
3737
final class DecoderData {
3838
var headers: [String]?
3939
var data: [String] = []
40+
var tempData: [String] = []
4041
let booleanDecodingBehavior: BooleanDecodingBehavior
4142

4243
init(booleanDecodingBehavior: BooleanDecodingBehavior) {
@@ -49,9 +50,13 @@ extension CSVLineDecoder {
4950

5051
struct SVD: SingleValueDecodingContainer {
5152
var codingPath: [any CodingKey]
52-
let value: String
53+
let data: DecoderData
5354
let decoder: CSVLineDecoder
5455

56+
var value: String {
57+
data.tempData.last!
58+
}
59+
5560
func decodeLossless<T>(_ type: T.Type) throws -> T where T: LosslessStringConvertible {
5661
switch T(value) {
5762
case .some(let value):
@@ -66,11 +71,7 @@ extension CSVLineDecoder {
6671
}
6772

6873
func decode(_ type: Bool.Type) throws -> Bool {
69-
switch value {
70-
case "true": true
71-
case "false": false
72-
default: throw DecodingError.dataCorruptedError(in: self, debugDescription: #"Value is not "true" or "false""#)
73-
}
74+
try data.booleanDecodingBehavior.decode(value: value)
7475
}
7576

7677
func decode(_ type: String.Type) throws -> String {
@@ -259,21 +260,18 @@ extension CSVLineDecoder {
259260
}
260261

261262
func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T : Decodable {
262-
// let decoder = JSONDecoder()
263-
decoder.tempData = try getValue(for: key)
263+
decoder.decoderData.tempData.append(try getValue(for: key))
264264
let v = try T.init(from: decoder)
265-
decoder.tempData = nil
265+
decoder.decoderData.tempData.removeLast()
266266
return v
267-
// return try decoder.decode(Map<T>.self, from: "{ \"a\": \"\(data[key.intValue!])\" }".data(using: .ascii)!).a
268-
269267
}
270268

271269
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
272-
fatalError()
270+
KeyedDecodingContainer(KeyContainerDecoder<NestedKey>(decoder: decoder))
273271
}
274272

275273
func nestedUnkeyedContainer(forKey key: Key) throws -> any UnkeyedDecodingContainer {
276-
fatalError()
274+
fatalError("Not supported yet. Please file a bug report.")
277275
}
278276

279277
func superDecoder() throws -> any Decoder {
@@ -345,7 +343,7 @@ extension CSVLineDecoder {
345343
}
346344

347345
func decodeIfPresent<T>(_ type: T.Type, forKey key: Key) throws -> T? where T : Decodable {
348-
fatalError()
346+
try decode(type, forKey: key)
349347
}
350348
}
351349
}

0 commit comments

Comments
 (0)