|
2 | 2 | // |
3 | 3 | // This source file is part of the Swift.org open source project |
4 | 4 | // |
5 | | -// Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 5 | +// Copyright (c) 2021-2023 Apple Inc. and the Swift project authors |
6 | 6 | // Licensed under Apache License v2.0 with Runtime Library Exception |
7 | 7 | // |
8 | 8 | // See https://swift.org/LICENSE.txt for license information |
@@ -93,256 +93,6 @@ public extension DecodableAttributedStringKey where Value : NSSecureCoding & NSO |
93 | 93 | } |
94 | 94 | #endif // FOUNDATION_FRAMEWORK |
95 | 95 |
|
96 | | -// MARK: Codable With Configuration |
97 | | - |
98 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
99 | | -public protocol EncodingConfigurationProviding { |
100 | | - associatedtype EncodingConfiguration |
101 | | - static var encodingConfiguration: EncodingConfiguration { get } |
102 | | -} |
103 | | - |
104 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
105 | | -public protocol EncodableWithConfiguration { |
106 | | - associatedtype EncodingConfiguration |
107 | | - func encode(to encoder: Encoder, configuration: EncodingConfiguration) throws |
108 | | -} |
109 | | - |
110 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
111 | | -public protocol DecodingConfigurationProviding { |
112 | | - associatedtype DecodingConfiguration |
113 | | - static var decodingConfiguration: DecodingConfiguration { get } |
114 | | -} |
115 | | - |
116 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
117 | | -public protocol DecodableWithConfiguration { |
118 | | - associatedtype DecodingConfiguration |
119 | | - init(from decoder: Decoder, configuration: DecodingConfiguration) throws |
120 | | -} |
121 | | - |
122 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
123 | | -public typealias CodableWithConfiguration = EncodableWithConfiguration & DecodableWithConfiguration |
124 | | - |
125 | | - |
126 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
127 | | -public extension KeyedEncodingContainer { |
128 | | - mutating func encode<T, C>(_ wrapper: CodableConfiguration<T?, C>, forKey key: Self.Key) throws { |
129 | | - switch wrapper.wrappedValue { |
130 | | - case .some(let val): |
131 | | - try val.encode(to: self.superEncoder(forKey: key), configuration: C.encodingConfiguration) |
132 | | - break |
133 | | - default: break |
134 | | - } |
135 | | - } |
136 | | -} |
137 | | - |
138 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
139 | | -public extension KeyedDecodingContainer { |
140 | | - func decode<T, C>(_: CodableConfiguration<T?, C>.Type, forKey key: Self.Key) throws -> CodableConfiguration<T?, C> { |
141 | | - if self.contains(key) { |
142 | | - let wrapper = try self.decode(CodableConfiguration<T, C>.self, forKey: key) |
143 | | - return CodableConfiguration<T?, C>(wrappedValue: wrapper.wrappedValue) |
144 | | - } else { |
145 | | - return CodableConfiguration<T?, C>(wrappedValue: nil) |
146 | | - } |
147 | | - } |
148 | | - |
149 | | -} |
150 | | - |
151 | | - |
152 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
153 | | -public extension KeyedEncodingContainer { |
154 | | - |
155 | | - mutating func encode<T: EncodableWithConfiguration, C: EncodingConfigurationProviding>(_ t: T, forKey key: Self.Key, configuration: C.Type) throws where T.EncodingConfiguration == C.EncodingConfiguration { |
156 | | - try t.encode(to: self.superEncoder(forKey: key), configuration: C.encodingConfiguration) |
157 | | - } |
158 | | - mutating func encodeIfPresent<T: EncodableWithConfiguration, C: EncodingConfigurationProviding>(_ t: T?, forKey key: Self.Key, configuration: C.Type) throws where T.EncodingConfiguration == C.EncodingConfiguration { |
159 | | - guard let value = t else { return } |
160 | | - try self.encode(value, forKey: key, configuration: configuration) |
161 | | - } |
162 | | - |
163 | | - mutating func encode<T: EncodableWithConfiguration>(_ t: T, forKey key: Self.Key, configuration: T.EncodingConfiguration) throws { |
164 | | - try t.encode(to: self.superEncoder(forKey: key), configuration: configuration) |
165 | | - } |
166 | | - mutating func encodeIfPresent<T: EncodableWithConfiguration>(_ t: T?, forKey key: Self.Key, configuration: T.EncodingConfiguration) throws { |
167 | | - guard let value = t else { return } |
168 | | - try self.encode(value, forKey: key, configuration: configuration) |
169 | | - } |
170 | | - |
171 | | -} |
172 | | - |
173 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
174 | | -public extension KeyedDecodingContainer { |
175 | | - |
176 | | - func decode<T: DecodableWithConfiguration, C: DecodingConfigurationProviding>( |
177 | | - _ : T.Type, |
178 | | - forKey key: Self.Key, |
179 | | - configuration: C.Type |
180 | | - ) throws -> T where T.DecodingConfiguration == C.DecodingConfiguration { |
181 | | - return try T(from: self.superDecoder(forKey: key), configuration: C.decodingConfiguration) |
182 | | - } |
183 | | - func decodeIfPresent<T: DecodableWithConfiguration, C: DecodingConfigurationProviding>( |
184 | | - _ : T.Type, |
185 | | - forKey key: Self.Key, |
186 | | - configuration: C.Type |
187 | | - ) throws -> T? where T.DecodingConfiguration == C.DecodingConfiguration { |
188 | | - if contains(key) { |
189 | | - return try self.decode(T.self, forKey: key, configuration: configuration) |
190 | | - } else { |
191 | | - return nil |
192 | | - } |
193 | | - } |
194 | | - |
195 | | - func decode<T: DecodableWithConfiguration>( |
196 | | - _ : T.Type, |
197 | | - forKey key: Self.Key, |
198 | | - configuration: T.DecodingConfiguration |
199 | | - ) throws -> T { |
200 | | - return try T(from: self.superDecoder(forKey: key), configuration: configuration) |
201 | | - } |
202 | | - func decodeIfPresent<T: DecodableWithConfiguration>( |
203 | | - _ : T.Type, |
204 | | - forKey key: Self.Key, |
205 | | - configuration: T.DecodingConfiguration |
206 | | - ) throws -> T? { |
207 | | - if contains(key) { |
208 | | - return try self.decode(T.self, forKey: key, configuration: configuration) |
209 | | - } else { |
210 | | - return nil |
211 | | - } |
212 | | - } |
213 | | - |
214 | | -} |
215 | | - |
216 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
217 | | -public extension UnkeyedEncodingContainer { |
218 | | - |
219 | | - mutating func encode<T: EncodableWithConfiguration, C: EncodingConfigurationProviding>(_ t: T, configuration: C.Type) throws where T.EncodingConfiguration == C.EncodingConfiguration { |
220 | | - try t.encode(to: self.superEncoder(), configuration: C.encodingConfiguration) |
221 | | - } |
222 | | - |
223 | | - mutating func encode<T: EncodableWithConfiguration>(_ t: T, configuration: T.EncodingConfiguration) throws { |
224 | | - try t.encode(to: self.superEncoder(), configuration: configuration) |
225 | | - } |
226 | | - |
227 | | -} |
228 | | - |
229 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
230 | | -public extension UnkeyedDecodingContainer { |
231 | | - |
232 | | - mutating func decode<T: DecodableWithConfiguration, C: DecodingConfigurationProviding>( |
233 | | - _ : T.Type, configuration: C.Type |
234 | | - ) throws -> T where T.DecodingConfiguration == C.DecodingConfiguration { |
235 | | - return try T(from: try self.superDecoder(), configuration: C.decodingConfiguration) |
236 | | - } |
237 | | - mutating func decodeIfPresent<T: DecodableWithConfiguration, C: DecodingConfigurationProviding>( |
238 | | - _ : T.Type, configuration: C.Type |
239 | | - ) throws -> T? where T.DecodingConfiguration == C.DecodingConfiguration { |
240 | | - if try self.decodeNil() { |
241 | | - return nil |
242 | | - } else { |
243 | | - return try self.decode(T.self, configuration: configuration) |
244 | | - } |
245 | | - } |
246 | | - |
247 | | - mutating func decode<T: DecodableWithConfiguration>( |
248 | | - _ : T.Type, configuration: T.DecodingConfiguration |
249 | | - ) throws -> T { |
250 | | - return try T(from: try self.superDecoder(), configuration: configuration) |
251 | | - } |
252 | | - mutating func decodeIfPresent<T: DecodableWithConfiguration>( |
253 | | - _ : T.Type, configuration: T.DecodingConfiguration |
254 | | - ) throws -> T? { |
255 | | - if try self.decodeNil() { |
256 | | - return nil |
257 | | - } else { |
258 | | - return try self.decode(T.self, configuration: configuration) |
259 | | - } |
260 | | - } |
261 | | - |
262 | | -} |
263 | | - |
264 | | -@propertyWrapper |
265 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
266 | | -public struct CodableConfiguration<T, ConfigurationProvider> : Codable |
267 | | -where T: CodableWithConfiguration, |
268 | | - ConfigurationProvider: EncodingConfigurationProviding & DecodingConfigurationProviding, |
269 | | - ConfigurationProvider.EncodingConfiguration == T.EncodingConfiguration, |
270 | | - ConfigurationProvider.DecodingConfiguration == T.DecodingConfiguration |
271 | | -{ |
272 | | - public var wrappedValue: T |
273 | | - |
274 | | - public init(wrappedValue: T) { |
275 | | - self.wrappedValue = wrappedValue |
276 | | - } |
277 | | - |
278 | | - public init(wrappedValue: T, from configurationProvider: ConfigurationProvider.Type) { |
279 | | - self.wrappedValue = wrappedValue |
280 | | - } |
281 | | - |
282 | | - public func encode(to encoder: Encoder) throws { |
283 | | - try wrappedValue.encode(to: encoder, configuration: ConfigurationProvider.encodingConfiguration) |
284 | | - } |
285 | | - |
286 | | - public init(from decoder: Decoder) throws { |
287 | | - wrappedValue = try T(from: decoder, configuration: ConfigurationProvider.decodingConfiguration) |
288 | | - } |
289 | | -} |
290 | | - |
291 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
292 | | -extension CodableConfiguration : Sendable where T : Sendable { } |
293 | | - |
294 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
295 | | -extension CodableConfiguration : Equatable where T : Equatable { } |
296 | | - |
297 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
298 | | -extension CodableConfiguration : Hashable where T : Hashable { } |
299 | | - |
300 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
301 | | -extension Optional : EncodableWithConfiguration where Wrapped : EncodableWithConfiguration { |
302 | | - public func encode(to encoder: Encoder, configuration: Wrapped.EncodingConfiguration) throws { |
303 | | - if let wrapped = self { |
304 | | - try wrapped.encode(to: encoder, configuration: configuration) |
305 | | - } else { |
306 | | - var c = encoder.singleValueContainer() |
307 | | - try c.encodeNil() |
308 | | - } |
309 | | - } |
310 | | -} |
311 | | - |
312 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
313 | | -extension Optional : DecodableWithConfiguration where Wrapped : DecodableWithConfiguration { |
314 | | - public init(from decoder: Decoder, configuration: Wrapped.DecodingConfiguration) throws { |
315 | | - let c = try decoder.singleValueContainer() |
316 | | - if c.decodeNil() { |
317 | | - self = nil |
318 | | - } else { |
319 | | - self = try Wrapped.init(from: decoder, configuration: configuration) |
320 | | - } |
321 | | - } |
322 | | -} |
323 | | - |
324 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
325 | | -extension Array : EncodableWithConfiguration where Element : EncodableWithConfiguration { |
326 | | - public func encode(to encoder: Encoder, configuration: Element.EncodingConfiguration) throws { |
327 | | - var c = encoder.unkeyedContainer() |
328 | | - for e in self { |
329 | | - try c.encode(e, configuration: configuration) |
330 | | - } |
331 | | - } |
332 | | -} |
333 | | - |
334 | | -@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
335 | | -extension Array : DecodableWithConfiguration where Element : DecodableWithConfiguration { |
336 | | - public init(from decoder: Decoder, configuration: Element.DecodingConfiguration) throws { |
337 | | - var result = [Element]() |
338 | | - var c = try decoder.unkeyedContainer() |
339 | | - while !c.isAtEnd { |
340 | | - try result.append(c.decode(Element.self, configuration: configuration)) |
341 | | - } |
342 | | - self = result |
343 | | - } |
344 | | -} |
345 | | - |
346 | 96 | // MARK: AttributedString CodableWithConfiguration Conformance |
347 | 97 |
|
348 | 98 | @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) |
|
0 commit comments