Skip to content

Commit 16ed7e9

Browse files
committed
Namespace lazy sequence under CSVDecoder.LazySequence
1 parent 164a8a6 commit 16ed7e9

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

sources/declarative/decodable/Decoder.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,18 @@ extension CSVDecoder {
6464
/// Returns a sequence for decoding each row from a CSV file (given as a `Data` blob).
6565
/// - parameter data: The data blob representing a CSV file.
6666
/// - throws: `CSVError<CSVReader>` exclusively.
67-
open func lazy(from data: Data) throws -> CSVDecoderSequence {
67+
open func lazy(from data: Data) throws -> LazySequence {
6868
let reader = try CSVReader(input: data, configuration: self.configuration.readerConfiguration)
6969
let source = ShadowDecoder.Source(reader: reader, configuration: self.configuration, userInfo: self.userInfo)
70-
return CSVDecoderSequence(source: source)
70+
return LazySequence(source: source)
7171
}
7272

7373
/// Returns a sequence for decoding each row from a CSV file (being pointed by `url`).
7474
/// - parameter url: The URL pointing to the file to decode.
7575
/// - throws: `CSVError<CSVReader>` exclusively.
76-
open func lazy(from url: URL) throws -> CSVDecoderSequence {
76+
open func lazy(from url: URL) throws -> LazySequence {
7777
let reader = try CSVReader(input: url, configuration: self.configuration.readerConfiguration)
7878
let source = ShadowDecoder.Source(reader: reader, configuration: self.configuration, userInfo: self.userInfo)
79-
return CSVDecoderSequence(source: source)
79+
return LazySequence(source: source)
8080
}
8181
}
Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
1-
open class CSVDecoderSequence: IteratorProtocol, Sequence {
2-
private let source: ShadowDecoder.Source
3-
private var currentIndex: Int = 0
1+
extension CSVDecoder {
2+
public struct LazySequence: IteratorProtocol, Sequence {
3+
private let source: ShadowDecoder.Source
4+
private var currentIndex: Int = 0
45

5-
init(source: ShadowDecoder.Source) {
6-
self.source = source
7-
}
6+
init(source: ShadowDecoder.Source) {
7+
self.source = source
8+
}
9+
10+
/// Advances to the next row and returns a `CSVRowDecoder`, or `nil` if no next row exists.
11+
public mutating func next() -> Row? {
12+
guard !self.source.isRowAtEnd(index: self.currentIndex) else {
13+
return nil
14+
}
815

9-
/// Advances to the next row and returns a `CSVRowDecoder`, or `nil` if no next row exists.
10-
public func next() -> CSVRowDecoder? {
11-
guard !self.source.isRowAtEnd(index: self.currentIndex) else {
12-
return nil
16+
defer { self.currentIndex += 1 }
17+
let decoder = ShadowDecoder(source: source, codingPath: [IndexKey(self.currentIndex)])
18+
return Row(decoder: decoder)
1319
}
1420

15-
defer { self.currentIndex += 1 }
16-
let decoder = ShadowDecoder(source: source, codingPath: [IndexKey(self.currentIndex)])
17-
return CSVRowDecoder(decoder: decoder)
18-
}
19-
}
21+
public struct Row {
22+
/// The representation of the decoding process point-in-time.
23+
private let decoder: ShadowDecoder
2024

21-
public struct CSVRowDecoder {
22-
/// The representation of the decoding process point-in-time.
23-
let decoder: ShadowDecoder
25+
fileprivate init(decoder: ShadowDecoder) {
26+
self.decoder = decoder
27+
}
2428

25-
/// Returns a value of the type you specify, decoded from CSV row.
26-
/// - parameter type: The type of the value to decode from the supplied file.
27-
/// - throws: `DecodingError`, or `CSVError<CSVReader>`, or the error raised by your custom types.
28-
public func decode<T:Decodable>(_ type: T.Type) throws -> T {
29-
return try T(from: decoder)
29+
/// Returns a value of the type you specify, decoded from CSV row.
30+
/// - parameter type: The type of the value to decode from the supplied file.
31+
/// - throws: `DecodingError`, or `CSVError<CSVReader>`, or the error raised by your custom types.
32+
public func decode<T:Decodable>(_ type: T.Type) throws -> T {
33+
return try T(from: decoder)
34+
}
35+
}
3036
}
3137
}

0 commit comments

Comments
 (0)