@@ -35,6 +35,7 @@ extension KeyedDecodingContainer {
35
35
/// for the given key.
36
36
/// - throws: `DecodingError.valueNotFound` if `self` has a null entry for
37
37
/// the given key.
38
+ @available ( * , deprecated, message: " Use decodeHex insetad " )
38
39
public func decode( _ type: [ Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ Any ] {
39
40
var values = try nestedUnkeyedContainer ( forKey: key)
40
41
return try values. decode ( type)
@@ -52,6 +53,7 @@ extension KeyedDecodingContainer {
52
53
/// for the given key.
53
54
/// - throws: `DecodingError.valueNotFound` if `self` has a null entry for
54
55
/// the given key.
56
+ @available ( * , deprecated, message: " Use decodeHex() insetad " )
55
57
public func decode( _ type: [ String : Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ String : Any ] {
56
58
let values = try nestedContainer ( keyedBy: AnyCodingKey . self, forKey: key)
57
59
return try values. decode ( type)
@@ -70,6 +72,7 @@ extension KeyedDecodingContainer {
70
72
/// the value is a null value.
71
73
/// - throws: `DecodingError.typeMismatch` if the encountered encoded value
72
74
/// is not convertible to the requested type.
75
+ @available ( * , deprecated, message: " In next version Will be replaced by decodeHexIfPresent() insetad " )
73
76
public func decodeIfPresent( _ type: [ Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ Any ] ? {
74
77
guard contains ( key) ,
75
78
try decodeNil ( forKey: key) == false else { return nil }
@@ -89,26 +92,107 @@ extension KeyedDecodingContainer {
89
92
/// the value is a null value.
90
93
/// - throws: `DecodingError.typeMismatch` if the encountered encoded value
91
94
/// is not convertible to the requested type.
95
+ @available ( * , deprecated, message: " In next version Will be replaced by decodeHexIfPresent() insetad " )
92
96
public func decodeIfPresent( _ type: [ String : Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ String : Any ] ? {
93
97
guard contains ( key) ,
94
98
try decodeNil ( forKey: key) == false else { return nil }
95
99
return try decode ( type, forKey: key)
96
100
}
97
101
98
- /// Decodes a value of the given key from Hex to BigUInt
102
+ /// Decodes a value of the given key from Hex to `DecodableFromHex`
99
103
///
100
- /// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`
104
+ /// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`, `UInt.Type`
101
105
///
102
106
/// - Parameter type: Generic type `T` wich conforms to `DecodableFromHex` protocol
103
107
/// - Parameter key: The key that the decoded value is associated with.
104
- /// - Returns: A decoded value of type `BigUInt`
105
- /// - throws: `Web3Error.dataError` if value associated with key are unable
106
- /// to be initialized as `BigUInt`.
107
- public func decodeHex< T: DecodableFromHex > ( to type: T . Type , key: KeyedDecodingContainer < K > . Key ) throws -> T {
108
- let string = try self . decode ( String . self, forKey: key)
109
- guard let number = T ( fromHex: string) else { throw Web3Error . dataError }
108
+ /// - Returns: A decoded value of type `T`
109
+ /// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `DecodableFromHex`.
110
+ public func decodeHex< T: DecodableFromHex > ( _ type: T . Type , forKey: KeyedDecodingContainer < K > . Key ) throws -> T {
111
+ let hexString = try self . decode ( String . self, forKey: forKey)
112
+ guard let number = T ( fromHex: hexString) else { throw Web3Error . dataError }
110
113
return number
111
114
}
115
+
116
+ /// Decodes a value of the given key from Hex to `[DecodableFromHex]`
117
+ ///
118
+ /// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`, `UInt.Type`
119
+ ///
120
+ /// - Parameter type: Array of a generic type `T` wich conforms to `DecodableFromHex` protocol
121
+ /// - Parameter key: The key that the decoded value is associated with.
122
+ /// - Returns: A decoded value of type `T`
123
+ /// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `[[DecodableFromHex]]`.
124
+ public func decodeHex< T: DecodableFromHex > ( _ type: [ T ] . Type, forKey: KeyedDecodingContainer < K > . Key ) throws -> [ T ] {
125
+ var container = try nestedUnkeyedContainer ( forKey: forKey)
126
+ guard let array = try ? container. decodeHex ( type) else { throw Web3Error . dataError }
127
+ return array
128
+ }
129
+
130
+ /// Decodes a value of the given key from Hex to `[DecodableFromHex]`
131
+ ///
132
+ /// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`, `EthereumAddress`, `UInt.Type`
133
+ ///
134
+ /// - Parameter type: Array of a generic type `T` wich conforms to `DecodableFromHex` protocol
135
+ /// - Parameter key: The key that the decoded value is associated with.
136
+ /// - Returns: A decoded value of type `T`
137
+ /// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `[[DecodableFromHex]]`.
138
+ public func decodeHex< T: DecodableFromHex > ( _ type: [ [ T ] ] . Type, forKey: KeyedDecodingContainer < K > . Key ) throws -> [ [ T ] ] {
139
+ var container = try nestedUnkeyedContainer ( forKey: forKey)
140
+ guard let array = try ? container. decodeHex ( type) else { throw Web3Error . dataError }
141
+ return array
142
+ }
143
+
144
+ /// Decodes a value of the given key from Hex to `DecodableFromHex`
145
+ ///
146
+ /// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`, `UInt.Type`
147
+ ///
148
+ /// - Parameter type: Generic type `T` wich conforms to `DecodableFromHex` protocol
149
+ /// - Parameter key: The key that the decoded value is associated with.
150
+ /// - Returns: A decoded value of type `T`, or nil if key is not present
151
+ /// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `DecodableFromHex`.
152
+ public func decodeHexIfPresent< T: DecodableFromHex > ( _ type: T . Type , forKey: KeyedDecodingContainer < K > . Key ) throws -> T ? {
153
+ guard contains ( forKey) else { return nil }
154
+ return try decodeHex ( type, forKey: forKey)
155
+ }
156
+
157
+ }
158
+
159
+ public extension UnkeyedDecodingContainer {
160
+ /// Decodes a unkeyed value from hex to `[DecodableFromHex]`
161
+ ///
162
+ /// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`, `EthereumAddress`
163
+ ///
164
+ /// - Parameter type: Generic type `T` wich conforms to `DecodableFromHex` protocol
165
+ /// - Parameter key: The key that the decoded value is associated with.
166
+ /// - Returns: A decoded value of type `BigUInt`
167
+ /// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `[DecodableFromHex]`.
168
+ mutating func decodeHex< T: DecodableFromHex > ( _ type: [ T ] . Type) throws -> [ T ] {
169
+ var array : [ T ] = [ ]
170
+ while !isAtEnd {
171
+ let hexString = try decode ( String . self)
172
+ guard let item = T ( fromHex: hexString) else { continue }
173
+ array. append ( item)
174
+ }
175
+ return array
176
+ }
177
+
178
+
179
+ /// Decodes a unkeyed value from Hex to `DecodableFromHex`
180
+ ///
181
+ /// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`, `EthereumAddress`
182
+ ///
183
+ /// - Parameter type: Generic type `T` wich conforms to `DecodableFromHex` protocol
184
+ /// - Parameter key: The key that the decoded value is associated with.
185
+ /// - Returns: A decoded value of type `BigUInt`
186
+ /// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `[[DecodableFromHex]]`.
187
+ mutating func decodeHex< T: DecodableFromHex > ( _ type: [ [ T ] ] . Type) throws -> [ [ T ] ] {
188
+ var array : [ [ T ] ] = [ ]
189
+ while !isAtEnd {
190
+ var container = try nestedUnkeyedContainer ( )
191
+ let intArr = try container. decodeHex ( [ T ] . self)
192
+ array. append ( intArr)
193
+ }
194
+ return array
195
+ }
112
196
}
113
197
114
198
public protocol DecodableFromHex : Decodable {
@@ -123,6 +207,12 @@ extension Data: DecodableFromHex {
123
207
}
124
208
}
125
209
210
+ extension UInt : DecodableFromHex {
211
+ public init ? ( fromHex hexString: String ) {
212
+ self . init ( hexString. stripHexPrefix ( ) , radix: 16 )
213
+ }
214
+ }
215
+
126
216
extension BigUInt : DecodableFromHex {
127
217
public init ? ( fromHex hexString: String ) {
128
218
self . init ( hexString. stripHexPrefix ( ) , radix: 16 )
@@ -144,6 +234,7 @@ extension EthereumAddress: DecodableFromHex {
144
234
}
145
235
}
146
236
237
+ // deprecated, should be removed in 3.0.0
147
238
private extension KeyedDecodingContainer {
148
239
func decode( _ type: [ String : Any ] . Type) throws -> [ String : Any ] {
149
240
var dictionary : [ String : Any ] = [ : ]
@@ -168,6 +259,7 @@ private extension KeyedDecodingContainer {
168
259
}
169
260
}
170
261
262
+ // deprecated, should be removed in 3.0.0
171
263
private extension UnkeyedDecodingContainer {
172
264
mutating func decode( _ type: [ Any ] . Type) throws -> [ Any ] {
173
265
var elements : [ Any ] = [ ]
0 commit comments