You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A `CSVReader` parses CSV data from a given input (`String`, `Data`, `URL`, or `InputStream`) and returns CSV rows as a `String`s array. `CSVReader` can be used at a _high-level_, in which case it parses an input completely; or at a _low-level_, in which each row is decoded when requested.
80
78
@@ -189,7 +187,7 @@ let reader = CSVReader(input: ...) {
A `CSVWriter` encodes CSV information into a specified target (i.e. a `String`, or `Data`, or a file). It can be used at a _high-level_, by encoding completely a prepared set of information; or at a _low-level_, in which case rows or fields can be written individually.
195
193
@@ -287,7 +285,7 @@ let writer = CSWriter(fileURL: ...) {
Many of `CodableCSV`'s imperative functions may throw errors due to invalid configuration values, invalid CSV input, file stream failures, etc. All these throwing operations exclusively throw `CSVError`s that can be easily caught with `do`-`catch` clause.
293
291
@@ -302,16 +300,16 @@ do {
302
300
}
303
301
```
304
302
305
-
`CSVError` adopts [Swift Evolution's SE-112](https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror-bridging.md) protocols (`LocalizedError` and `CustomNSError`) and `CustomDebugStringConvertible`. The error's properties provide rich commentary explaining what went wrong and indicate how to fix the problem.
303
+
`CSVError` adopts Swift Evolution's [SE-112 protocols](https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror-bridging.md) and `CustomDebugStringConvertible`. The error's properties provide rich commentary explaining what went wrong and indicate how to fix the problem.
306
304
307
305
- `type`: The error group category.
308
-
- `failureReason`: Explanation on what went wrong.
306
+
- `failureReason`: Explanation of what went wrong.
309
307
- `helpAnchor`: Advice on how to solve the problem.
310
308
- `errorUserInfo`: Arguments associated with the operation that threw the error.
311
309
- `underlyingError`: Optional underlying error, which provoked the operation to fail (most of the time is `nil`).
312
310
- `localizedDescription`: Returns a human readable string with all the information contained in the error.
313
311
314
-
You can get all the information by simply printing the error or calling the `localizedDescription` property on a properly casted `CSVError<CSVReader>` or `CSVError<CSVWriter>`.
312
+
<br>You can get all the information by simply printing the error or calling the `localizedDescription` property on a properly casted `CSVError<CSVReader>` or `CSVError<CSVWriter>`.
315
313
316
314
</p></details>
317
315
</ul>
@@ -321,7 +319,7 @@ You can get all the information by simply printing the error or calling the `loc
321
319
The encoders/decoders provided by this library let you use Swift's `Codable` declarative approach to encode/decode CSV data.
`CSVDecoder` transforms CSV data into a Swift type conforming to `Decodable`. The decoding process is very simple and it only requires creating a decoding instance and call its `decode` function passing the `Decodable` type and the input data.
327
325
@@ -334,7 +332,7 @@ let result = try decoder.decode(CustomType.self, from: data)
334
332
335
333
```swift
336
334
let decoder = CSVDecoder { $0.bufferingStrategy = .sequential }
337
-
let content: [Student] = try decoder.decode([Student].self, from: URL("~/Desktop/Student.csv"))
335
+
let content = try decoder.decode([Student].self, from: URL("~/Desktop/Student.csv"))
338
336
```
339
337
340
338
If you are dealing with a big CSV file, it is preferred to used direct file decoding, a `.sequential` or `.unrequested` buffering strategy, and set _presampling_ to false; since then memory usage is drastically reduced.
@@ -357,7 +355,7 @@ The decoding process can be tweaked by specifying configuration values at initia
357
355
358
356
- `bufferingStrategy` (default `.keepAll`) controls the behavior of `KeyedDecodingContainer`s.
359
357
360
-
Selecting a buffering strategy affects the decoding performance and the amount of memory used during the process. For more information check this README's [Tips using `Codable`](#Tips-using-codable) section and the [`Strategy.DecodingBuffer` definition](sources/declarative/decodable/DecoderConfiguration.swift).
358
+
Selecting a buffering strategy affects the decoding performance and the amount of memory used during the decoding process. For more information check the README's [Tips using `Codable`](#Tips-using-codable) section and the [`Strategy.DecodingBuffer` definition](sources/declarative/decodable/DecoderConfiguration.swift).
361
359
362
360
The configuration values can be set during `CSVDecoder` initialization or at any point before the `decode` function is called.
363
361
@@ -377,44 +375,44 @@ decoder.decimalStrategy = .custom { (decoder) in
A CSV input can be decoded _on demand_ with the decoder's `lazy(from:)` function.
380
+
A CSV input can be decoded _on demand_ (i.e. row-by-row) with the decoder's `lazy(from:)` function.
383
381
384
382
```swift
385
-
let lazyDecoder = CSVDecoder().lazy(from: fileURL)
386
-
while let row = sequence.next() {
387
-
let student = try row.decode(Student.self)
388
-
// Do something here
389
-
}
383
+
let decoder = CSVDecoder(configuration: config).lazy(from: fileURL)
384
+
let student1 = try decoder.decodeRow(Student.self)
385
+
let student2 = try decoder.decodeRow(Student.self)
390
386
```
391
387
392
-
`LazyDecoder` conforms to Swift's [`Sequence` protocol](https://developer.apple.com/documentation/swift/sequence), letting you use functionality such as `map()`, `allSatisfy()`, etc. Please note, `LazyDecoder` cannot be used for repeated access. It _consumes_ the input CSV.
388
+
`CSVDecoder.Lazy` conforms to Swift's [`Sequence` protocol](https://developer.apple.com/documentation/swift/sequence), letting you use functionality such as `map()`, `allSatisfy()`, etc. Please note, `CSVDecoder.Lazy` cannot be used for repeated access; It _consumes_ the input CSV.
393
389
394
390
```swift
395
-
let lazyDecoder = CSVDecoder(configuration: config).lazy(from: fileData)
396
-
let students = try lazyDecoder.map { try $0.decode(Student.self) }
391
+
let decoder = CSVDecoder().lazy(from: fileData)
392
+
let students = try decoder.map { try $0.decode(Student.self) }
397
393
```
398
394
399
395
A nice benefit of using the _lazy_ operation, is that it lets you switch how a row is decoded at any point. For example:
400
396
```swift
401
-
let lazyDecoder = decoder.lazy(from: fileString)
402
-
let students = ( 0..<100).map { _ in try lazyDecoder.decode(Student.self) }
403
-
let teachers = (100..<110).map { _ in try lazyDecoder.decode(Teacher.self) }
397
+
let decoder = CSVDecoder().lazy(from: fileString)
398
+
// The first 100 rows are students.
399
+
let students = ( 0..<100).map { _ in try decoder.decode(Student.self) }
400
+
// The second 100 rows are teachers.
401
+
let teachers = (100..<110).map { _ in try decoder.decode(Teacher.self) }
404
402
```
405
403
406
-
Since `LazyDecoder` exclusively provides sequential access; setting the buffering strategy to `.sequential` will reduce the decoder's memory usage.
404
+
Since `CSVDecoder.Lazy` exclusively provides sequential access; setting the buffering strategy to `.sequential` will reduce the decoder's memory usage.
`CSVEncoder` transforms Swift types conforming to `Encodable` into CSV data. The encoding process is very simple and it only requires creating an encoding instance and call its `encode` function passing the `Encodable` value.
A series of codable types can be encoded _on demand_ with the encoder's `lazy(into:)` function.
477
+
A series of codable types (representing CSV rows) can be encoded _on demand_ with the encoder's `lazy(into:)` function.
480
478
481
479
```swift
482
-
let lazyEncoder = CSVEncoder().lazy(into: Data.self)
480
+
let encoder = CSVEncoder().lazy(into: Data.self)
483
481
for student in students {
484
-
try lazyEncoder.encode(student)
482
+
try encoder.encodeRow(student)
485
483
}
486
-
let data = try lazyEncoder.endEncoding()
484
+
let data = try encoder.endEncoding()
487
485
```
488
486
489
487
Call `endEncoding()` once there is no more values to be encoded. The function will return the encoded CSV.
490
488
```swift
491
-
let lazyEncoder = CSVEncoder().lazy(into: String.self)
489
+
let encoder = CSVEncoder().lazy(into: String.self)
492
490
students.forEach {
493
-
try lazyEncoder.encode($0)
491
+
try encoder.encode($0)
494
492
}
495
-
let string = try lazyEncoder.endEncoding()
493
+
let string = try encoder.endEncoding()
496
494
```
497
495
498
496
A nice benefit of using the _lazy_ operation, is that it lets you switch how a row is encoded at any point. For example:
499
497
```swift
500
-
let lazyEncoder = CSVEncoder(configuration: config).lazy(into: fileURL)
501
-
students.forEach { try lazyEncoder.encode($0) }
502
-
teachers.forEach { try lazyEncoder.encode($0) }
503
-
try lazyEncoder.endEncoding()
498
+
let encoder = CSVEncoder(configuration: config).lazy(into: fileURL)
499
+
students.forEach { try encoder.encode($0) }
500
+
teachers.forEach { try encoder.encode($0) }
501
+
try encoder.endEncoding()
504
502
```
505
503
506
-
Since `LazyEncoder` exclusively provides sequential encoding; setting the buffering strategy to `.sequential` will reduce the encoder's memory usage.
504
+
Since `CSVEncoder.Lazy` exclusively provides sequential encoding; setting the buffering strategy to `.sequential` will reduce the encoder's memory usage.
[SE167](https://github.com/apple/swift-evolution/blob/master/proposals/0167-swift-encoders.md) proposal introduced to Foundation a new JSON and PLIST encoder/decoder. This proposal also featured encoding/decoding strategies as a new way to configure the encoding/decoding process. `CodableCSV` continues this _tradition_ and mirrors such strategies including some new ones specific to the CSV file format.
686
+
[SE167](https://github.com/apple/swift-evolution/blob/master/proposals/0167-swift-encoders.md) proposal introduced to Foundation JSON and PLIST encoders/decoders. This proposal also featured encoding/decoding strategies as a new way to configure the encoding/decoding process. `CodableCSV` continues this _tradition_ and mirrors such strategies including some new ones specific to the CSV file format.
689
687
690
688
To configure the encoding/decoding process, you need to set the configuration values of the `CSVEncoder`/`CSVDecoder` before calling the `encode()`/`decode()` functions. There are two ways to set configuration values:
0 commit comments