Skip to content

Commit 42b108f

Browse files
committed
Prepare 0.6.0 release
1 parent 5cfe55b commit 42b108f

File tree

8 files changed

+94
-187
lines changed

8 files changed

+94
-187
lines changed

CodableCSV.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'CodableCSV'
3-
s.version = '0.5.5'
3+
s.version = '0.6.0'
44
s.summary = "Read and write CSV files row-by-row or through Swift's Codable interface."
55

66
s.homepage = 'https://github.com/dehesa/CodableCSV'

README.md

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111

1212
[CodableCSV](https://github.com/dehesa/CodableCSV) provides:
1313

14-
- Imperative CSV reader/writer (row-by-row and/or field-by-field).
15-
- Declarative `Codable` encoder/decoder and lazy row decoder.
14+
- Imperative CSV reader/writer.
15+
- Declarative CSV encoder/decoder.
1616
- Support multiple inputs/outputs: `String`s, `Data` blobs, `URL`s, and `Stream`s (commonly used for `stdin`).
1717
- Support numerous string encodings and [Byte Order Markers](https://en.wikipedia.org/wiki/Byte_order_mark) (BOM).
1818
- Extensive configuration: delimiters, escaping scalar, trim strategy, codable strategies, presampling, etc.
1919
- [RFC4180](https://tools.ietf.org/html/rfc4180) compliant with default configuration and CRLF (`\r\n`) row delimiter.
20-
- Multiplatform support with no dependencies.
21-
22-
> The Swift Standard Library and Foundation are considered implicit requirements.
20+
- Multiplatform support with no dependencies (the Swift Standard Library and Foundation are implicit dependencies).
2321

2422
# Usage
2523

@@ -39,7 +37,7 @@ You can choose to add the library through SPM or Cocoapods:
3937
let package = Package(
4038
/* Your package name, supported platforms, and generated products go here */
4139
dependencies: [
42-
.package(url: "https://github.com/dehesa/CodableCSV.git", from: "0.5.5")
40+
.package(url: "https://github.com/dehesa/CodableCSV.git", from: "0.6.0")
4341
],
4442
targets: [
4543
.target(name: /* Your target name here */, dependencies: ["CodableCSV"])
@@ -50,7 +48,7 @@ You can choose to add the library through SPM or Cocoapods:
5048
- [Cocoapods](https://cocoapods.org).
5149

5250
```
53-
pod 'CodableCSV', '~> 0.5.5'
51+
pod 'CodableCSV', '~> 0.6.0'
5452
```
5553

5654
</p></details>
@@ -74,7 +72,7 @@ There are two ways to use this library:
7472
The following types provide imperative control on how to read/write CSV data.
7573

7674
<ul>
77-
<details><summary><code>CSVReader</code>.</summary><p>
75+
<details><summary><code>CSVReader</code></summary><p>
7876

7977
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.
8078

@@ -189,7 +187,7 @@ let reader = CSVReader(input: ...) {
189187

190188
</p></details>
191189

192-
<details><summary><code>CSVWriter</code>.</summary><p>
190+
<details><summary><code>CSVWriter</code></summary><p>
193191

194192
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.
195193

@@ -287,7 +285,7 @@ let writer = CSWriter(fileURL: ...) {
287285
288286
</p></details>
289287
290-
<details><summary><code>CSVError</code>.</summary><p>
288+
<details><summary><code>CSVError</code></summary><p>
291289
292290
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.
293291
@@ -302,16 +300,16 @@ do {
302300
}
303301
```
304302
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.
306304
307305
- `type`: The error group category.
308-
- `failureReason`: Explanation on what went wrong.
306+
- `failureReason`: Explanation of what went wrong.
309307
- `helpAnchor`: Advice on how to solve the problem.
310308
- `errorUserInfo`: Arguments associated with the operation that threw the error.
311309
- `underlyingError`: Optional underlying error, which provoked the operation to fail (most of the time is `nil`).
312310
- `localizedDescription`: Returns a human readable string with all the information contained in the error.
313311
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>`.
315313
316314
</p></details>
317315
</ul>
@@ -321,7 +319,7 @@ You can get all the information by simply printing the error or calling the `loc
321319
The encoders/decoders provided by this library let you use Swift's `Codable` declarative approach to encode/decode CSV data.
322320
323321
<ul>
324-
<details><summary><code>CSVDecoder</code>.</summary><p>
322+
<details><summary><code>CSVDecoder</code></summary><p>
325323
326324
`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.
327325
@@ -334,7 +332,7 @@ let result = try decoder.decode(CustomType.self, from: data)
334332
335333
```swift
336334
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"))
338336
```
339337
340338
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
357355
358356
- `bufferingStrategy` (default `.keepAll`) controls the behavior of `KeyedDecodingContainer`s.
359357
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).
361359
362360
The configuration values can be set during `CSVDecoder` initialization or at any point before the `decode` function is called.
363361
@@ -377,44 +375,44 @@ decoder.decimalStrategy = .custom { (decoder) in
377375
378376
</p></details>
379377
380-
<details><summary><code>CSVDecoder.LazyDecoder</code>.</summary><p>
378+
<details><summary><code>CSVDecoder.Lazy</code></summary><p>
381379
382-
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.
383381
384382
```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)
390386
```
391387
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.
393389
394390
```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) }
397393
```
398394
399395
A nice benefit of using the _lazy_ operation, is that it lets you switch how a row is decoded at any point. For example:
400396
```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) }
404402
```
405403
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.
407405
408406
```swift
409407
let decoder = CSVDecoder {
410408
$0.headerStrategy = .firstLine
411409
$0.bufferingStrategy = .sequential
412-
}
410+
}.lazy(from: fileURL)
413411
```
414412
415413
</p></details>
416414
417-
<details><summary><code>CSVEncoder</code>.</summary><p>
415+
<details><summary><code>CSVEncoder</code></summary><p>
418416
419417
`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.
420418
@@ -474,39 +472,39 @@ encoder.dataStrategy = .custom { (data, encoder) in
474472
475473
</p></details>
476474
477-
<details><summary><code>CSVEncoder.LazyEncoder</code>.</summary><p>
475+
<details><summary><code>CSVEncoder.Lazy</code></summary><p>
478476
479-
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.
480478
481479
```swift
482-
let lazyEncoder = CSVEncoder().lazy(into: Data.self)
480+
let encoder = CSVEncoder().lazy(into: Data.self)
483481
for student in students {
484-
try lazyEncoder.encode(student)
482+
try encoder.encodeRow(student)
485483
}
486-
let data = try lazyEncoder.endEncoding()
484+
let data = try encoder.endEncoding()
487485
```
488486
489487
Call `endEncoding()` once there is no more values to be encoded. The function will return the encoded CSV.
490488
```swift
491-
let lazyEncoder = CSVEncoder().lazy(into: String.self)
489+
let encoder = CSVEncoder().lazy(into: String.self)
492490
students.forEach {
493-
try lazyEncoder.encode($0)
491+
try encoder.encode($0)
494492
}
495-
let string = try lazyEncoder.endEncoding()
493+
let string = try encoder.endEncoding()
496494
```
497495
498496
A nice benefit of using the _lazy_ operation, is that it lets you switch how a row is encoded at any point. For example:
499497
```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()
504502
```
505503
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.
507505
508506
```swift
509-
let lazyEncoder = CSVEncoder {
507+
let encoder = CSVEncoder {
510508
$0.bufferingStrategy = .sequential
511509
}.lazy(into: String.self)
512510
```
@@ -685,7 +683,7 @@ struct Student: Codable {
685683
686684
<details><summary>Encoding/decoding strategies.</summary><p>
687685
688-
[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.
689687
690688
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:
691689

sources/Deprecated.swift

Lines changed: 0 additions & 95 deletions
This file was deleted.

sources/declarative/decodable/Decoder.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,28 @@ extension CSVDecoder {
7272
/// Returns a sequence for decoding row-by-row from a CSV file (given as a `Data` blob).
7373
/// - parameter data: The data blob representing a CSV file.
7474
/// - throws: `CSVError<CSVReader>` exclusively.
75-
open func lazy(from data: Data) throws -> LazyDecoder {
75+
open func lazy(from data: Data) throws -> Lazy {
7676
let reader = try CSVReader(input: data, configuration: self._configuration.readerConfiguration)
7777
let source = ShadowDecoder.Source(reader: reader, configuration: self._configuration, userInfo: self.userInfo)
78-
return LazyDecoder(source: source)
78+
return Lazy(source: source)
7979
}
8080

8181
/// Returns a sequence for decoding row-by-row from a CSV file (given as a `String`).
8282
/// - parameter string: A Swift string representing a CSV file.
8383
/// - throws: `CSVError<CSVReader>` exclusively.
84-
open func lazy(from string: String) throws -> LazyDecoder {
84+
open func lazy(from string: String) throws -> Lazy {
8585
let reader = try CSVReader(input: string, configuration: self._configuration.readerConfiguration)
8686
let source = ShadowDecoder.Source(reader: reader, configuration: self._configuration, userInfo: self.userInfo)
87-
return LazyDecoder(source: source)
87+
return Lazy(source: source)
8888
}
8989

9090
/// Returns a sequence for decoding row-by-row from a CSV file (being pointed by `url`).
9191
/// - parameter url: The URL pointing to the file to decode.
9292
/// - throws: `CSVError<CSVReader>` exclusively.
93-
open func lazy(from url: URL) throws -> LazyDecoder {
93+
open func lazy(from url: URL) throws -> Lazy {
9494
let reader = try CSVReader(input: url, configuration: self._configuration.readerConfiguration)
9595
let source = ShadowDecoder.Source(reader: reader, configuration: self._configuration, userInfo: self.userInfo)
96-
return LazyDecoder(source: source)
96+
return Lazy(source: source)
9797
}
9898
}
9999

0 commit comments

Comments
 (0)