Skip to content

Commit 04942a2

Browse files
committed
Serialization/Encoding rename and extension of Encoder API surface
1 parent c010320 commit 04942a2

File tree

9 files changed

+231
-66
lines changed

9 files changed

+231
-66
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ You can choose to add the library through SPM or Cocoapods:
3737
let package = Package(
3838
/* Your package name, supported platforms, and generated products go here */
3939
dependencies: [
40-
.package(url: "https://github.com/dehesa/CodableCSV.git", .upToNextMinor(from: "0.5.0"))
40+
.package(url: "https://github.com/dehesa/CodableCSV.git", .upToNextMinor(from: "0.5.1"))
4141
],
4242
targets: [
4343
.target(name: /* Your target name here */, dependencies: ["CodableCSV"])
@@ -48,7 +48,7 @@ You can choose to add the library through SPM or Cocoapods:
4848
- [Cocoapods](https://cocoapods.org).
4949

5050
```
51-
pod 'CodableCSV', '~> 0.5.0'
51+
pod 'CodableCSV', '~> 0.5.1'
5252
```
5353

5454
</p></details>
@@ -175,17 +175,17 @@ let reader = CSVReader(input: ...) {
175175
176176
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 writen individually.
177177
178-
- Complete CSV rows serialization.
178+
- Complete CSV rows encoding.
179179
180180
```swift
181181
let input = [
182182
["numA", "numB", "name" ],
183183
["1" , "2" , "Marcos" ],
184184
["4" , "5" , "Marine-Anaïs"]
185185
]
186-
let data = try CSVWriter.serialize(rows: input)
187-
let string = try CSVWriter.serialize(rows: input, into: String.self)
188-
try CSVWriter.serialize(rows: input, into: URL("~/Desktop/Test.csv")!, append: false)
186+
let data = try CSVWriter.encode(rows: input)
187+
let string = try CSVWriter.encode(rows: input, into: String.self)
188+
try CSVWriter.encode(rows: input, into: URL("~/Desktop/Test.csv")!, append: false)
189189
```
190190
191191
- Row-by-row encoding.
@@ -254,7 +254,7 @@ A `CSVWriter` encodes CSV information into a specified target (i.e. a `String`,
254254

255255
- `bomStrategy` (default: `.convention`) indicates whether a Byte Order Marker will be included at the beginning of the CSV representation.
256256

257-
The OS convention is that BOMs are never writen, except when `.utf16`, `.utf32`, or `.unicode` string encodings are specified. You could however indicate that you always want the BOM writen (`.always`) or that is never writer (`.never`).
257+
The OS convention is that BOMs are never writen, except when `.utf16`, `.utf32`, or `.unicode` string encodings are specified. You could however indicate that you always want the BOM writen (`.always`) or that is never writen (`.never`).
258258

259259
The configuration values are set during initialization and can be passed to the `CSWriter` instance through a structure or with a convenience closure syntax:
260260

docs/SUPPORT.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Here is a helpful list of "where to get help" depending on the topic:
44

5-
- **How to use [CodableCSV](https://www.github.com/dehesa/CodableCSV)?**
5+
- **How to use CodableCSV?**
66

77
The first place to look is the [README file](https://github.com/dehesa/CodableCSV/blob/master/README.md). It is kept updated and there is a wealth of information under each right-pointing [caret](https://en.wikipedia.org/wiki/Caret). Information spans from basic usage to tips on how to squeeze the best performance.
88

@@ -20,11 +20,11 @@ Here is a helpful list of "where to get help" depending on the topic:
2020

2121
For complex queries, go to the [Swift forums](https://forums.swift.org), specifically the [_Using Swift_](https://forums.swift.org/c/swift-users/15) category, setting the search tag to [_codable_](https://forums.swift.org/tags/c/swift-users/15/codable).
2222

23-
- **How to contribute to [CodableCSV](https://www.github.com/dehesa/CodableCSV)?**
23+
- **How to contribute to CodableCSV?**
2424

25-
This project uses the standard [CONTRIBUTING.md](docs/CONTRIBUTING.md) file explaining the contribution guidelines. This same file also has a section on "where to start contributing", in case you want to contribute but you are unsure where to start.
25+
This project uses the standard [CONTRIBUTING.md](CONTRIBUTING.md) file explaining the contribution guidelines. This same file also has a section on "where to start contributing", in case you want to contribute but you are unsure where to start.
2626

27-
Also take a look at the Github's [projects page](https://github.com/dehesa/CodableCSV/projects) where you can see the project's Kanban board, or at a higher level, take a look at the [Roadmap graph](docs/Assets/Roadmap.svg).
27+
Also take a look at the Github's [projects page](https://github.com/dehesa/CodableCSV/projects) where you can see the project's Kanban board, or at a higher level, take a look at the [Roadmap graph](Assets/Roadmap.svg).
2828

2929
- **What is the Comma-separated values format?**
3030

sources/Active/Writer/WriterAPI.swift

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extension CSVWriter {
6666
/// - parameter configuration: Configuration values specifying how the CSV output should look like.
6767
/// - throws: `CSVError<CSVWriter>` exclusively.
6868
/// - returns: Data blob in a CSV format.
69-
@inlinable public static func serialize<S:Sequence,C:Collection>(rows: S, configuration: Configuration = .init()) throws -> Data where S.Element==C, C.Element==String {
69+
@inlinable public static func encode<S:Sequence,C:Collection>(rows: S, configuration: Configuration = .init()) throws -> Data where S.Element==C, C.Element==String {
7070
let writer = try CSVWriter(configuration: configuration)
7171
for row in rows {
7272
try writer.write(row: row)
@@ -82,8 +82,8 @@ extension CSVWriter {
8282
/// - parameter configuration: Configuration values specifying how the CSV output should look like.
8383
/// - throws: `CSVError<CSVWriter>` exclusively.
8484
/// - returns: Swift `String` containing the formatted CSV data.
85-
@inlinable public static func serialize<S:Sequence,C:Collection>(rows: S, into type: String.Type, configuration: Configuration = .init()) throws -> String where S.Element==C, C.Element==String {
86-
let data = try CSVWriter.serialize(rows: rows, configuration: configuration)
85+
@inlinable public static func encode<S:Sequence,C:Collection>(rows: S, into type: String.Type, configuration: Configuration = .init()) throws -> String where S.Element==C, C.Element==String {
86+
let data = try CSVWriter.encode(rows: rows, configuration: configuration)
8787
return String(data: data, encoding: configuration.encoding ?? .utf8)!
8888
}
8989

@@ -93,7 +93,7 @@ extension CSVWriter {
9393
/// - parameter append: In case an existing file is under the given URL, this Boolean indicates that the information will be appended to the file (`true`), or the file will be overwritten (`false`).
9494
/// - parameter configuration: Configuration values specifying how the CSV output should look like.
9595
/// - throws: `CSVError<CSVWriter>` exclusively.
96-
@inlinable public static func serialize<S:Sequence,C:Collection>(rows: S, into fileURL: URL, append: Bool, configuration: Configuration = .init()) throws where S.Element==C, C.Element==String {
96+
@inlinable public static func encode<S:Sequence,C:Collection>(rows: S, into fileURL: URL, append: Bool, configuration: Configuration = .init()) throws where S.Element==C, C.Element==String {
9797
let writer = try CSVWriter(fileURL: fileURL, append: append, configuration: configuration)
9898
for row in rows {
9999
try writer.write(row: row)
@@ -111,10 +111,10 @@ extension CSVWriter {
111111
/// - parameter configuration: Default configuration values for the `CSVWriter`.
112112
/// - throws: `CSVError<CSVWriter>` exclusively.
113113
/// - returns: Data blob in a CSV format.
114-
@inlinable public static func serialize<S:Sequence,C:Collection>(rows: S, setter: (_ configuration: inout Configuration) -> Void) throws -> Data where S.Element==C, C.Element==String {
114+
@inlinable public static func encode<S:Sequence,C:Collection>(rows: S, setter: (_ configuration: inout Configuration) -> Void) throws -> Data where S.Element==C, C.Element==String {
115115
var configuration = Configuration()
116116
setter(&configuration)
117-
return try CSVWriter.serialize(rows: rows, configuration: configuration)
117+
return try CSVWriter.encode(rows: rows, configuration: configuration)
118118
}
119119

120120

@@ -125,10 +125,10 @@ extension CSVWriter {
125125
/// - parameter configuration: Default configuration values for the `CSVWriter`.
126126
/// - throws: `CSVError<CSVWriter>` exclusively.
127127
/// - returns: Swift `String` containing the formatted CSV data.
128-
@inlinable public static func serialize<S:Sequence,C:Collection>(rows: S, into type: String.Type, setter: (_ configuration: inout Configuration) -> Void) throws -> String where S.Element==C, C.Element==String {
128+
@inlinable public static func encode<S:Sequence,C:Collection>(rows: S, into type: String.Type, setter: (_ configuration: inout Configuration) -> Void) throws -> String where S.Element==C, C.Element==String {
129129
var configuration = Configuration()
130130
setter(&configuration)
131-
return try CSVWriter.serialize(rows: rows, into: type, configuration: configuration)
131+
return try CSVWriter.encode(rows: rows, into: type, configuration: configuration)
132132
}
133133

134134
/// Creates a file with the CSV-encoded representation of the `rows` supplied.
@@ -138,10 +138,10 @@ extension CSVWriter {
138138
/// - parameter setter: Closure receiving the default configuration values and gives you the possibility to change them.
139139
/// - parameter configuration: Default configuration values for the `CSVWriter`.
140140
/// - throws: `CSVError<CSVWriter>` exclusively.
141-
@inlinable public static func serialize<S:Sequence,C:Collection>(row: S, into fileURL: URL, append: Bool, setter: (_ configuration: inout Configuration) -> Void) throws where S.Element==C, C.Element==String {
141+
@inlinable public static func encode<S:Sequence,C:Collection>(rows: S, into fileURL: URL, append: Bool, setter: (_ configuration: inout Configuration) -> Void) throws where S.Element==C, C.Element==String {
142142
var configuration = Configuration()
143143
setter(&configuration)
144-
try CSVWriter.serialize(rows: row, into: fileURL, append: append, configuration: configuration)
144+
try CSVWriter.encode(rows: rows, into: fileURL, append: append, configuration: configuration)
145145
}
146146
}
147147

@@ -163,3 +163,37 @@ fileprivate extension CSVWriter.Error {
163163
userInfo: ["Encoding": encoding])
164164
}
165165
}
166+
167+
// MARK: -
168+
169+
extension CSVWriter {
170+
@available(*, deprecated, renamed: "encode(rows:configuration:)")
171+
public static func serialize<S:Sequence,C:Collection>(rows: S, configuration: Configuration = .init()) throws -> Data where S.Element==C, C.Element==String {
172+
try self.encode(rows: rows, configuration: configuration)
173+
}
174+
175+
@available(*, deprecated, renamed: "encode(rows:into:configuration:)")
176+
@inlinable public static func serialize<S:Sequence,C:Collection>(rows: S, into type: String.Type, configuration: Configuration = .init()) throws -> String where S.Element==C, C.Element==String {
177+
try self.encode(rows: rows, into: type, configuration: configuration)
178+
}
179+
180+
@available(*, deprecated, renamed: "encode(rows:into:configuration:)")
181+
public static func serialize<S:Sequence,C:Collection>(rows: S, into fileURL: URL, append: Bool, configuration: Configuration = .init()) throws where S.Element==C, C.Element==String {
182+
try self.encode(rows: rows, into: fileURL, append: append, configuration: configuration)
183+
}
184+
185+
@available(*, deprecated, renamed: "encode(rows:setter:)")
186+
public static func serialize<S:Sequence,C:Collection>(rows: S, setter: (_ configuration: inout Configuration) -> Void) throws -> Data where S.Element==C, C.Element==String {
187+
try self.encode(rows: rows, setter: setter)
188+
}
189+
190+
@available(*, deprecated, renamed: "encode(rows:into:setter:)")
191+
public static func serialize<S:Sequence,C:Collection>(rows: S, into type: String.Type, setter: (_ configuration: inout Configuration) -> Void) throws -> String where S.Element==C, C.Element==String {
192+
try self.encode(rows: rows, into: type, setter: setter)
193+
}
194+
195+
@available(*, deprecated, renamed: "encode(rows:into:append:setter:)")
196+
public static func serialize<S:Sequence,C:Collection>(row: S, into fileURL: URL, append: Bool, setter: (_ configuration: inout Configuration) -> Void) throws where S.Element==C, C.Element==String {
197+
try self.encode(rows: row, into: fileURL, append: append, setter: setter)
198+
}
199+
}

sources/Codable/Decodable/Decoder.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ import Foundation
3131
}
3232

3333
extension CSVDecoder {
34-
/// Returns a value of the type you specify, decoded from a CSV file (given as a `String`).
34+
/// Returns a value of the type you specify, decoded from a CSV file (given as a `Data` blob).
3535
/// - parameter type: The type of the value to decode from the supplied file.
36-
/// - parameter string: A Swift string representing a CSV file.
36+
/// - parameter data: The data blob representing a CSV file.
3737
/// - throws: `DecodingError`, or `CSVError<CSVReader>`, or the error raised by your custom types.
38-
open func decode<T:Decodable>(_ type: T.Type, from string: String) throws -> T {
39-
let reader = try CSVReader(input: string, configuration: self.configuration.readerConfiguration)
38+
open func decode<T:Decodable>(_ type: T.Type, from data: Data) throws -> T {
39+
let reader = try CSVReader(input: data, configuration: self.configuration.readerConfiguration)
4040
let source = ShadowDecoder.Source(reader: reader, configuration: self.configuration, userInfo: self.userInfo)
4141
return try T(from: ShadowDecoder(source: source, codingPath: []))
4242
}
43-
44-
/// Returns a value of the type you specify, decoded from a CSV file (given as a `Data` blob).
43+
44+
/// Returns a value of the type you specify, decoded from a CSV file (given as a `String`).
4545
/// - parameter type: The type of the value to decode from the supplied file.
46-
/// - parameter data: The data blob representing a CSV file.
46+
/// - parameter string: A Swift string representing a CSV file.
4747
/// - throws: `DecodingError`, or `CSVError<CSVReader>`, or the error raised by your custom types.
48-
open func decode<T:Decodable>(_ type: T.Type, from data: Data) throws -> T {
49-
let reader = try CSVReader(input: data, configuration: self.configuration.readerConfiguration)
48+
open func decode<T:Decodable>(_ type: T.Type, from string: String) throws -> T {
49+
let reader = try CSVReader(input: string, configuration: self.configuration.readerConfiguration)
5050
let source = ShadowDecoder.Source(reader: reader, configuration: self.configuration, userInfo: self.userInfo)
5151
return try T(from: ShadowDecoder(source: source, codingPath: []))
5252
}

sources/Codable/Encodable/Containers/EncodingKeyed.swift

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,15 @@ extension ShadowEncoder.KeyedContainer {
3636
}
3737

3838
extension ShadowEncoder.KeyedContainer {
39-
mutating func encodeNil(forKey key: Key) throws {
40-
fatalError()
41-
}
42-
43-
mutating func encode(_ value: Bool, forKey key: Key) throws {
44-
fatalError()
45-
}
46-
4739
mutating func encode(_ value: String, forKey key: Key) throws {
4840
fatalError()
4941
}
5042

51-
mutating func encode(_ value: Double, forKey key: Key) throws {
43+
mutating func encodeNil(forKey key: Key) throws {
5244
fatalError()
5345
}
5446

55-
mutating func encode(_ value: Float, forKey key: Key) throws {
47+
mutating func encode(_ value: Bool, forKey key: Key) throws {
5648
fatalError()
5749
}
5850

@@ -96,7 +88,81 @@ extension ShadowEncoder.KeyedContainer {
9688
fatalError()
9789
}
9890

99-
mutating func encode<T>(_ value: T, forKey key: Key) throws where T : Encodable {
91+
mutating func encode(_ value: Float, forKey key: Key) throws {
92+
fatalError()
93+
}
94+
95+
mutating func encode(_ value: Double, forKey key: Key) throws {
96+
fatalError()
97+
}
98+
99+
mutating func encode<T>(_ value: T, forKey key: Key) throws where T:Encodable {
100+
fatalError()
101+
}
102+
103+
mutating func encodeConditional<T>(_ object: T, forKey key: Key) throws where T:AnyObject, T:Encodable {
104+
fatalError()
105+
}
106+
}
107+
108+
extension ShadowEncoder.KeyedContainer {
109+
mutating func encodeIfPresent(_ value: String?, forKey key: Key) throws {
110+
fatalError()
111+
}
112+
113+
mutating func encodeIfPresent(_ value: Bool?, forKey key: Key) throws {
114+
fatalError()
115+
}
116+
117+
mutating func encodeIfPresent(_ value: Double?, forKey key: Key) throws {
118+
fatalError()
119+
}
120+
121+
mutating func encodeIfPresent(_ value: Float?, forKey key: Key) throws {
122+
fatalError()
123+
}
124+
125+
mutating func encodeIfPresent(_ value: Int?, forKey key: Key) throws {
126+
fatalError()
127+
}
128+
129+
mutating func encodeIfPresent(_ value: Int8?, forKey key: Key) throws {
130+
fatalError()
131+
}
132+
133+
mutating func encodeIfPresent(_ value: Int16?, forKey key: Key) throws {
134+
fatalError()
135+
}
136+
137+
mutating func encodeIfPresent(_ value: Int32?, forKey key: Key) throws {
138+
fatalError()
139+
}
140+
141+
mutating func encodeIfPresent(_ value: Int64?, forKey key: Key) throws {
142+
fatalError()
143+
}
144+
145+
mutating func encodeIfPresent(_ value: UInt?, forKey key: Key) throws {
146+
fatalError()
147+
}
148+
149+
mutating func encodeIfPresent(_ value: UInt8?, forKey key: Key) throws {
150+
fatalError()
151+
}
152+
153+
mutating func encodeIfPresent(_ value: UInt16?, forKey key: Key) throws {
154+
fatalError()
155+
}
156+
157+
mutating func encodeIfPresent(_ value: UInt32?, forKey key: Key) throws {
158+
fatalError()
159+
}
160+
161+
mutating func encodeIfPresent(_ value: UInt64?, forKey key: Key) throws {
162+
fatalError()
163+
}
164+
165+
mutating func encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T:Encodable {
100166
fatalError()
101167
}
102168
}

0 commit comments

Comments
 (0)