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
Copy file name to clipboardExpand all lines: sources/declarative/decodable/DecoderConfiguration.swift
+53-9Lines changed: 53 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -49,6 +49,16 @@ extension Strategy {
49
49
/// An empty string can be both the absence of characters between field delimiters and an empty escaped field (e.g. `""`).
50
50
case empty
51
51
/// Decodes the `nil` as a custom value decoded by the given closure.
52
+
///
53
+
/// Custom `nil` decoding adheres to the same behavior as a custom `Decodable` type. For example:
54
+
///
55
+
/// let decoder = CSVDecoder()
56
+
/// decoder.nilStrategy = .custom({
57
+
/// let container = try $0.singleValueContainer()
58
+
/// let string = try container.decode(String.self)
59
+
/// return string == "-"
60
+
/// })
61
+
///
52
62
/// - parameter decoding: Function receiving the CSV decoder used to parse a custom `nil` value.
53
63
/// - parameter decoder: The decoder on which to fetch a single value container to obtain the underlying `String` value.
54
64
/// - returns: Boolean indicating whether the encountered value was a `nil` representation. If the value is not supported, return `false`.
@@ -65,11 +75,22 @@ extension Strategy {
65
75
///
66
76
/// The value: `True`, `TRUE`, `TruE` or `YES`are accepted.
67
77
case insensitive
68
-
/// Decodes the `Bool` from an underlying `0`/`1`
78
+
/// Decodes the `Bool` from an underlying `0` or `1`
69
79
case numeric
70
-
/// Decodes the `Bool` as a custom value decoded by the given closure.
80
+
/// Decodes the `Bool` as a custom value decoded by the given closure. If the closure fails to decode a value from the given decoder, the error will be bubled up.
81
+
///
82
+
/// Custom `Bool` decoding adheres to the same behavior as a custom `Decodable` type. For example:
83
+
///
84
+
/// let decoder = CSVDecoder()
85
+
/// decoder.boolStrategy = .custom({
86
+
/// let container = try $0.singleValueContainer()
87
+
/// switch try container.decode(String.self) {
88
+
/// case "si": return true
89
+
/// case "no": return false
90
+
/// default: throw CSVError<CSVDecoder>(...)
91
+
/// }
92
+
/// })
71
93
///
72
-
/// If the closure fails to decode a value from the given decoder, the error will be bubled up.
73
94
/// - parameter decoding: Function receiving the CSV decoder used to parse a custom `Bool` value.
74
95
/// - parameter decoder: The decoder on which to fetch a single value container to obtain the underlying `String` value.
75
96
/// - returns: Boolean value decoded from the underlying storage.
@@ -80,9 +101,16 @@ extension Strategy {
80
101
publicenumDecimalDecoding{
81
102
/// The locale used to interpret the number (specifically `decimalSeparator`).
82
103
case locale(Locale?=nil)
83
-
/// Decode the `Decimal` as a custom value decoded by the given closure.
104
+
/// Decode the `Decimal` as a custom value decoded by the given closure. If the closure fails to decode a value from the given decoder, the error will be bubled up.
105
+
///
106
+
/// Custom `Decimal` decoding adheres to the same behavior as a custom `Decodable` type. For example:
107
+
///
108
+
/// let decoder = CSVDecoder()
109
+
/// decoder.decimalStrategy = .custom({
110
+
/// let value = try Float(from: decoder)
111
+
/// return Decimal(value)
112
+
/// })
84
113
///
85
-
/// If the closure fails to decode a value from the given decoder, the error will be bubled up.
86
114
/// - parameter decoding: Function receiving the CSV decoder used to parse a custom `Decimal` value.
87
115
/// - parameter decoder: The decoder on which to fetch a single value container to obtain the underlying `String` value.
88
116
/// - returns: `Decimal` value decoded from the underlying storage.
@@ -101,9 +129,17 @@ extension Strategy {
101
129
case iso8601
102
130
/// Decode the `Date` as a string parsed by the given formatter.
103
131
case formatted(DateFormatter)
104
-
/// Decode the `Date` as a custom value decoded by the given closure.
132
+
/// Decode the `Date` as a custom value decoded by the given closure. If the closure fails to decode a value from the given decoder, the error will be bubled up.
133
+
///
134
+
/// Custom `Date` decoding adheres to the same behavior as a custom `Decodable` type. For example:
135
+
///
136
+
/// let decoder = CSVDecoder()
137
+
/// decoder.dateStrategy = .custom({
138
+
/// let container = try $0.singleValueContainer()
139
+
/// let string = try container.decode(String.self)
140
+
/// // Now returns the date represented by the custom string or throw an error if the string cannot be converted to a date.
141
+
/// })
105
142
///
106
-
/// If the closure fails to decode a value from the given decoder, the error will be bubled up.
107
143
/// - parameter decoding: Function receiving the CSV decoder used to parse a custom `Date` value.
108
144
/// - parameter decoder: The decoder on which to fetch a single value container to obtain the underlying `String` value.
109
145
/// - returns: `Date` value decoded from the underlying storage.
@@ -116,9 +152,17 @@ extension Strategy {
116
152
case deferredToData
117
153
/// Decode the `Data` from a Base64-encoded string.
118
154
case base64
119
-
/// Decode the `Data` as a custom value decoded by the given closure.
155
+
/// Decode the `Data` as a custom value decoded by the given closure. If the closure fails to decode a value from the given decoder, the error will be bubled up.
156
+
///
157
+
/// Custom `Data` decoding adheres to the same behavior as a custom `Decodable` type. For example:
158
+
///
159
+
/// let decoder = CSVDecoder()
160
+
/// decoder.dataStrategy = .custom({
161
+
/// let container = try $0.singleValueContainer()
162
+
/// let string = try container.decode(String.self)
163
+
/// // Now returns the data represented by the custom string or throw an error if the string cannot be converted to a data.
164
+
/// })
120
165
///
121
-
/// If the closure fails to decode a value from the given decoder, the error will be bubled up.
122
166
/// - parameter decoding: Function receiving the CSV decoder used to parse a custom `Data` value.
123
167
/// - parameter decoder: The decoder on which to fetch a single value container to obtain the underlying `String` value.
124
168
/// - returns: `Data` value decoded from the underlying storage.
Copy file name to clipboardExpand all lines: sources/declarative/encodable/EncoderConfiguration.swift
+47-10Lines changed: 47 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -46,9 +46,16 @@ extension Strategy {
46
46
publicenumNilEncoding{
47
47
/// `nil` is encoded as an empty string.
48
48
case empty
49
-
/// Encode `nil` as a custom value encoded by the given closure.
49
+
/// Encode `nil` as a custom value encoded by the given closure. If the closure fails to encode a value into the given encoder, the error will be bubled up.
50
+
///
51
+
/// Custom `nil` encoding adheres to the same behavior as a custom `Encodable` type. For example:
52
+
///
53
+
/// let encoder = CSVEncoder()
54
+
/// encoder.nilStrategy = .custom({
55
+
/// var container = try $0.singleValueContainer()
56
+
/// try container.encode("-")
57
+
/// })
50
58
///
51
-
/// If the closure fails to encode a value into the given encoder, the error will be bubled up.
52
59
/// - parameter encoding: Function receiving the encoder instance to encode `nil`.
53
60
/// - parameter encoder: The encoder on which to encode a custom `nil` representation.
54
61
case custom(_ encoding:(_ encoder:Encoder)throws->Void)
@@ -60,9 +67,16 @@ extension Strategy {
60
67
case deferredToString
61
68
/// Encode the `Bool` as `0` or `1`
62
69
case numeric
63
-
/// Encode the `Bool` as a custom value encoded by the given closure.
70
+
/// Encode the `Bool` as a custom value encoded by the given closure. If the closure fails to encode a value into the given encoder, the error will be bubled up.
71
+
///
72
+
/// Custom `Bool` encoding adheres to the same behavior as a custom `Encodable` type. For example:
73
+
///
74
+
/// let encoder = CSVEncoder()
75
+
/// encoder.boolStrategy = .custom({
76
+
/// var container = try $1.singleValueContainer()
77
+
/// try container.encode($0 ? "si" : "no")
78
+
/// })
64
79
///
65
-
/// If the closure fails to encode a value into the given encoder, the error will be bubled up.
66
80
/// - parameter encoding: Function receiving the necessary instances to encode a custom `Decimal` value.
67
81
/// - parameter value: The value to be encoded.
68
82
/// - parameter encoder: The encoder on which to generate a single value container.
@@ -74,9 +88,16 @@ extension Strategy {
74
88
/// The locale used to write the number (specifically the `decimalSeparator` property).
75
89
/// - parameter locale: The locale used to encode a `Decimal` value into a `String` value. If `nil`, the current user's locale will be used.
76
90
case locale(_ locale:Locale?=nil)
77
-
/// Encode the `Decimal` as a custom value encoded by the given closure.
91
+
/// Encode the `Decimal` as a custom value encoded by the given closure. If the closure fails to encode a value into the given encoder, the error will be bubled up.
92
+
///
93
+
/// Custom `Decimal` encoding adheres to the same behavior as a custom `Encodable` type. For example:
94
+
///
95
+
/// let encoder = CSVEncoder()
96
+
/// encoder.decimalStrategy = .custom({
97
+
/// var container = try $1.singleValueContainer()
98
+
/// try container.encode($0.description)
99
+
/// })
78
100
///
79
-
/// If the closure fails to encode a value into the given encoder, the error will be bubled up.
80
101
/// - parameter encoding: Function receiving the necessary instances to encode a custom `Decimal` value.
81
102
/// - parameter value: The value to be encoded.
82
103
/// - parameter encoder: The encoder on which to generate a single value container.
@@ -96,9 +117,17 @@ extension Strategy {
96
117
/// Encode the `Date` as a string formatted by the given formatter.
97
118
/// - parameter formatter: The date formatter used to encode a `Date` value into a `String`.
98
119
case formatted(_ formatter:DateFormatter)
99
-
/// Formats dates by calling a user-defined function.
120
+
/// Formats dates by calling a user-defined function. If the closure fails to encode a value into the given encoder, the error will be bubled up.
121
+
///
122
+
/// Custom `Date` encoding adheres to the same behavior as a custom `Encodable` type. For example:
123
+
///
124
+
/// let encoder = CSVEncoder()
125
+
/// encoder.dateStrategy = .custom({
126
+
/// var container = try $1.singleValueContainer()
127
+
/// let customRepresentation: String = // transform Date $0 into a String or throw if the date cannot be converted.
128
+
/// try container.encode(customRepresentation)
129
+
/// })
100
130
///
101
-
/// If the closure fails to encode a value into the given encoder, the error will be bubled up.
102
131
/// - parameter encoding: Function receiving the necessary instances to encode a custom `Date` value.
103
132
/// - parameter value: The value to be encoded.
104
133
/// - parameter encoder: The encoder on which to generate a single value container.
@@ -111,9 +140,17 @@ extension Strategy {
111
140
case deferredToData
112
141
/// Encoded the `Data` as a Base64-encoded string.
113
142
case base64
114
-
/// Formats data blobs by calling a user defined function.
143
+
/// Formats data blobs by calling a user defined function. If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
144
+
///
145
+
/// Custom `Data` encoding adheres to the same behavior as a custom `Encodable` type. For example:
146
+
///
147
+
/// let encoder = CSVEncoder()
148
+
/// encoder.dataStrategy = .custom({
149
+
/// var container = try $1.singleValueContainer()
150
+
/// let customRepresentation: String = // transform Data $0 into a String or throw if the data cannot be converted.
151
+
/// try container.encode(customRepresentation)
152
+
/// })
115
153
///
116
-
/// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
117
154
/// - parameter encoding: Function receiving the necessary instances to encode a custom `Data` value.
118
155
/// - parameter value: The value to be encoded.
119
156
/// - parameter encoder: The encoder on which to generate a single value container.
0 commit comments