|
7 | 7 |
|
8 | 8 | import Foundation |
9 | 9 |
|
10 | | -{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class CodableHelper { |
11 | | - private static var customDateFormatter: DateFormatter? |
12 | | - private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter() |
13 | | -
|
14 | | - private static var customJSONDecoder: JSONDecoder? |
15 | | - private static var defaultJSONDecoder: JSONDecoder = { |
16 | | - let decoder = JSONDecoder() |
17 | | - decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter) |
18 | | - return decoder |
19 | | - }() |
20 | | - |
21 | | - private static var customJSONEncoder: JSONEncoder? |
22 | | - private static var defaultJSONEncoder: JSONEncoder = { |
23 | | - let encoder = JSONEncoder() |
24 | | - encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter) |
25 | | - encoder.outputFormatting = .prettyPrinted |
26 | | - return encoder |
27 | | - }() |
28 | | - |
29 | | - {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var dateFormatter: DateFormatter { |
30 | | - get { return customDateFormatter ?? defaultDateFormatter } |
31 | | - set { customDateFormatter = newValue } |
| 10 | +{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class CodableHelper: @unchecked Sendable { |
| 11 | +
|
| 12 | + // MARK: - Private state |
| 13 | +
|
| 14 | + private struct State { |
| 15 | + var customDateFormatter: DateFormatter? |
| 16 | + var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter() |
| 17 | +
|
| 18 | + var customJSONDecoder: JSONDecoder? |
| 19 | + var defaultJSONDecoder: JSONDecoder = JSONDecoder() |
| 20 | +
|
| 21 | + var customJSONEncoder: JSONEncoder? |
| 22 | + var defaultJSONEncoder: JSONEncoder = JSONEncoder() |
| 23 | +
|
| 24 | + init() { |
| 25 | + defaultJSONEncoder.outputFormatting = .prettyPrinted |
| 26 | + rebuildDefaultCoders() |
| 27 | + } |
| 28 | + |
| 29 | + mutating func rebuildDefaultCoders() { |
| 30 | + defaultJSONDecoder.dateDecodingStrategy = .formatted(customDateFormatter ?? defaultDateFormatter) |
| 31 | + defaultJSONEncoder.dateEncodingStrategy = .formatted(customDateFormatter ?? defaultDateFormatter) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private let _state = OpenAPIMutex(State()) |
| 36 | + |
| 37 | + // MARK: - Init |
| 38 | + |
| 39 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init() {} |
| 40 | + |
| 41 | + // MARK: - Public interface |
| 42 | + |
| 43 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var dateFormatter: DateFormatter { |
| 44 | + get { _state.withValue { $0.customDateFormatter ?? $0.defaultDateFormatter } } |
| 45 | + set { |
| 46 | + _state.withValue { state in |
| 47 | + state.customDateFormatter = newValue |
| 48 | + state.rebuildDefaultCoders() |
| 49 | + } |
| 50 | + } |
32 | 51 | } |
33 | | - {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var jsonDecoder: JSONDecoder { |
34 | | - get { return customJSONDecoder ?? defaultJSONDecoder } |
35 | | - set { customJSONDecoder = newValue } |
| 52 | + |
| 53 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var jsonDecoder: JSONDecoder { |
| 54 | + get { _state.withValue { $0.customJSONDecoder ?? $0.defaultJSONDecoder } } |
| 55 | + set { _state.withValue { $0.customJSONDecoder = newValue } } |
36 | 56 | } |
37 | | - {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var jsonEncoder: JSONEncoder { |
38 | | - get { return customJSONEncoder ?? defaultJSONEncoder } |
39 | | - set { customJSONEncoder = newValue } |
| 57 | + |
| 58 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var jsonEncoder: JSONEncoder { |
| 59 | + get { _state.withValue { $0.customJSONEncoder ?? $0.defaultJSONEncoder } } |
| 60 | + set { _state.withValue { $0.customJSONEncoder = newValue } } |
40 | 61 | } |
41 | 62 |
|
42 | | - {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable { |
| 63 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable { |
43 | 64 | return Swift.Result { try jsonDecoder.decode(type, from: data) } |
44 | 65 | } |
45 | 66 |
|
46 | | - {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable { |
| 67 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable { |
47 | 68 | return Swift.Result { try jsonEncoder.encode(value) } |
48 | 69 | } |
49 | 70 | } |
0 commit comments