Skip to content

Commit 26b16eb

Browse files
committed
Make decoder's codingPath inmutable
1 parent f4741b2 commit 26b16eb

File tree

8 files changed

+58
-44
lines changed

8 files changed

+58
-44
lines changed

Assets/Roadmap.svg

Lines changed: 1 addition & 1 deletion
Loading

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ There are two ways to use [CodableCSV](https://github.com/dehesa/CodableCSV):
2929

3030
> `CodableCSV` can _encode to_ or _decode from_ `String`s, `Data` blobs, or CSV files (represented by `URL` addresses).
3131
32-
## Active Encoding/Decoding
32+
## Active Decoding/Encoding
3333

34-
The _active entities_ provide _imperative_ control on how to read or write CSV data.
34+
The _active entities_ provide imperative control on how to read or write CSV data.
3535

3636
<ul>
3737
<details><summary><code>CSVReader</code>.</summary><p>
3838

39-
A `CSVReadder` parses CSV data from an input and returns you each CSV row as an array of strings.
39+
A `CSVReadder` parses CSV data from an input and returns CSV row as an array of strings.
4040

4141
- Row-by-row parsing.
4242

@@ -47,6 +47,8 @@ A `CSVReadder` parses CSV data from an input and returns you each CSV row as an
4747
}
4848
```
4949

50+
Alternatively you can use the `parseRecord()` function which also returns the next CSV row, but it wraps the result in a convenience structure. This structure lets you access each field with the header name (as long as the `headerStrategy` is market with `.firstLine`).
51+
5052
- `Sequence` syntax parsing.
5153

5254
```swift
@@ -56,15 +58,17 @@ A `CSVReadder` parses CSV data from an input and returns you each CSV row as an
5658
}
5759
```
5860

59-
Please note the `Sequence` syntax (i.e. `IteratorProtocol`) doesn't throw errors; therefore if the CSV data is invalid, the previous code will crash your program. If you don't control the origin of the CSV data, use the `parseRow()` function instead.
61+
Please note the `Sequence` syntax (i.e. `IteratorProtocol`) doesn't throw errors; therefore if the CSV data is invalid, the previous code will crash. If you don't control the CSV data origin, use `parseRow()` instead.
6062

6163
- Whole input parsing.
6264

6365
```swift
6466
let file = try CSVReader.parse(string: ..., configuration: ...)
65-
// file is of type: (headers: [String], rows: [[String]])
67+
// file is of type: CSVReader.Output
6668
```
6769

70+
This type of parsing returns a simple structure containing the CSV headers and CSV rows. Additionally it lets you access each field through the header name or the field index.
71+
6872
### Reader Configuration
6973

7074
`CSVReader` accepts the following configuration properties:
@@ -89,7 +93,7 @@ A `CSVReadder` parses CSV data from an input and returns you each CSV row as an
8993

9094
Loading all data into memory may provide faster iteration for small to medium size files, since you get rid of the overhead of managing an `InputStream`.
9195

92-
The configuration values are only set during initialization and can be passed to the `CSVReader` instance through a structure or with a convenience closure syntax:
96+
The configuration values are set during initialization and can be passed to the `CSVReader` instance through a structure or with a convenience closure syntax:
9397

9498
```swift
9599
let reader = CSVReader(data: ...) {
@@ -109,7 +113,7 @@ let reader = CSVReader(data: ...) {
109113
</p></details>
110114
</ul>
111115

112-
## Swift's `Codable`
116+
## `Codable`'s Decoder/Encoder
113117

114118
The encoders/decoders provided by this library let you use Swift's `Codable` declarative approach to encode/decode CSV data.
115119

@@ -234,12 +238,14 @@ struct Student: Codable {
234238
}
235239
```
236240

241+
> Using integer coding keys has the added benefit of better encoder/decoder performance. By explicitly indicating the field index, you let the decoder skip the functionality of matching coding keys string values to headers.
242+
237243
</p></details>
238244
<details><summary>A CSV is a long list of records/rows.</summary><p>
239245

240246
CSV formatted data is commonly used with flat hierarchies (e.g. a list of students, a list of car models, etc.). Nested structures, such as the ones found in JSON files, are not supported by default in CSV implementations (e.g. a list of users, where each user has a list of services she uses, and each service has a list of the user's configuration values).
241247

242-
You can definitely support complex structures in CSV, but you would have to flatten the hierarchy in a single model or build a custom encoding/decoding process. This process would make sure there is always a maximum of two keyed/unkeyed containers.
248+
You can support complex structures in CSV, but you would have to flatten the hierarchy in a single model or build a custom encoding/decoding process. This process would make sure there is always a maximum of two keyed/unkeyed containers.
243249

244250
As an example, we can create a nested structure for a school with students who own pets.
245251

0 commit comments

Comments
 (0)