-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURLQueryDecoder.swift
More file actions
92 lines (74 loc) · 3.24 KB
/
URLQueryDecoder.swift
File metadata and controls
92 lines (74 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Foundation
public final class URLQueryDecoder: Sendable {
public static let `default` = URLQueryDecoder()
private let optionsMutex: Mutex<URLQueryDecodingOptions>
private let userInfoMutex: Mutex<[CodingUserInfoKey: Sendable]>
public var dateDecodingStrategy: URLQueryDateDecodingStrategy {
get { optionsMutex.withLock { $0.dateDecodingStrategy } }
set { optionsMutex.withLock { $0.dateDecodingStrategy = newValue } }
}
public var dataDecodingStrategy: URLQueryDataDecodingStrategy {
get { optionsMutex.withLock { $0.dataDecodingStrategy } }
set { optionsMutex.withLock { $0.dataDecodingStrategy = newValue } }
}
public var nonConformingFloatDecodingStrategy: URLQueryNonConformingFloatDecodingStrategy {
get { optionsMutex.withLock { $0.nonConformingFloatDecodingStrategy } }
set { optionsMutex.withLock { $0.nonConformingFloatDecodingStrategy = newValue } }
}
public var keyDecodingStrategy: URLQueryKeyDecodingStrategy {
get { optionsMutex.withLock { $0.keyDecodingStrategy } }
set { optionsMutex.withLock { $0.keyDecodingStrategy = newValue } }
}
public var userInfo: [CodingUserInfoKey: Sendable] {
get { userInfoMutex.withLock { $0 } }
set { userInfoMutex.withLock { $0 = newValue } }
}
public init(
dateDecodingStrategy: URLQueryDateDecodingStrategy = .deferredToDate,
dataDecodingStrategy: URLQueryDataDecodingStrategy = .base64,
nonConformingFloatDecodingStrategy: URLQueryNonConformingFloatDecodingStrategy = .throw,
keyDecodingStrategy: URLQueryKeyDecodingStrategy = .useDefaultKeys,
userInfo: [CodingUserInfoKey: Sendable] = [:]
) {
let options = URLQueryDecodingOptions(
dateDecodingStrategy: dateDecodingStrategy,
dataDecodingStrategy: dataDecodingStrategy,
nonConformingFloatDecodingStrategy: nonConformingFloatDecodingStrategy,
keyDecodingStrategy: keyDecodingStrategy
)
self.optionsMutex = Mutex(value: options)
self.userInfoMutex = Mutex(value: userInfo)
}
public func decode<T: Decodable>(
_ type: T.Type = T.self,
from query: String
) throws -> T {
let options = optionsMutex.withLock { $0 }
let deserializer = URLQueryDeserializer()
let value = try deserializer.deserialize(query)
let decoder = URLQuerySingleValueDecodingContainer(
value: value,
options: options,
userInfo: userInfo,
codingPath: []
)
return try T(from: decoder)
}
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
public func decode<T: DecodableWithConfiguration>(
_ type: T.Type = T.self,
from query: String,
configuration: T.DecodingConfiguration
) throws -> T {
let options = optionsMutex.withLock { $0 }
let deserializer = URLQueryDeserializer()
let value = try deserializer.deserialize(query)
let decoder = URLQuerySingleValueDecodingContainer(
value: value,
options: options,
userInfo: userInfo,
codingPath: []
)
return try T(from: decoder, configuration: configuration)
}
}