@@ -35,6 +35,7 @@ extension KeyedDecodingContainer {
3535 /// for the given key.
3636 /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for
3737 /// the given key.
38+ @available ( * , deprecated, message: " Use decodeHex insetad " )
3839 public func decode( _ type: [ Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ Any ] {
3940 var values = try nestedUnkeyedContainer ( forKey: key)
4041 return try values. decode ( type)
@@ -52,6 +53,7 @@ extension KeyedDecodingContainer {
5253 /// for the given key.
5354 /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for
5455 /// the given key.
56+ @available ( * , deprecated, message: " Use decodeHex() insetad " )
5557 public func decode( _ type: [ String : Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ String : Any ] {
5658 let values = try nestedContainer ( keyedBy: AnyCodingKey . self, forKey: key)
5759 return try values. decode ( type)
@@ -70,6 +72,7 @@ extension KeyedDecodingContainer {
7072 /// the value is a null value.
7173 /// - throws: `DecodingError.typeMismatch` if the encountered encoded value
7274 /// is not convertible to the requested type.
75+ @available ( * , deprecated, message: " In next version Will be replaced by decodeHexIfPresent() insetad " )
7376 public func decodeIfPresent( _ type: [ Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ Any ] ? {
7477 guard contains ( key) ,
7578 try decodeNil ( forKey: key) == false else { return nil }
@@ -89,26 +92,93 @@ extension KeyedDecodingContainer {
8992 /// the value is a null value.
9093 /// - throws: `DecodingError.typeMismatch` if the encountered encoded value
9194 /// is not convertible to the requested type.
95+ @available ( * , deprecated, message: " In next version Will be replaced by decodeHexIfPresent() insetad " )
9296 public func decodeIfPresent( _ type: [ String : Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ String : Any ] ? {
9397 guard contains ( key) ,
9498 try decodeNil ( forKey: key) == false else { return nil }
9599 return try decode ( type, forKey: key)
96100 }
97101
98- /// Decodes a value of the given key from Hex to BigUInt
102+ /// Decodes a value of the given key from Hex to `DecodableFromHex`
99103 ///
100104 /// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`
101105 ///
102106 /// - Parameter type: Generic type `T` wich conforms to `DecodableFromHex` protocol
103107 /// - Parameter key: The key that the decoded value is associated with.
104108 /// - 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 }
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 }
110113 return number
111114 }
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`
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 `BigUInt`
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`
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 `BigUInt`
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+
145+ public extension UnkeyedDecodingContainer {
146+ /// Decodes a unkeyed value from hex to `[DecodableFromHex]`
147+ ///
148+ /// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`, `EthereumAddress`
149+ ///
150+ /// - Parameter type: Generic type `T` wich conforms to `DecodableFromHex` protocol
151+ /// - Parameter key: The key that the decoded value is associated with.
152+ /// - Returns: A decoded value of type `BigUInt`
153+ /// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `[DecodableFromHex]`.
154+ mutating func decodeHex< T: DecodableFromHex > ( _ type: [ T ] . Type) throws -> [ T ] {
155+ var array : [ T ] = [ ]
156+ while !isAtEnd {
157+ let hexString = try decode ( String . self)
158+ guard let item = T ( fromHex: hexString) else { continue }
159+ array. append ( item)
160+ }
161+ return array
162+ }
163+
164+
165+ /// Decodes a unkeyed value from Hex to `DecodableFromHex`
166+ ///
167+ /// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`, `EthereumAddress`
168+ ///
169+ /// - Parameter type: Generic type `T` wich conforms to `DecodableFromHex` protocol
170+ /// - Parameter key: The key that the decoded value is associated with.
171+ /// - Returns: A decoded value of type `BigUInt`
172+ /// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `[[DecodableFromHex]]`.
173+ mutating func decodeHex< T: DecodableFromHex > ( _ type: [ [ T ] ] . Type) throws -> [ [ T ] ] {
174+ var array : [ [ T ] ] = [ ]
175+ while !isAtEnd {
176+ var container = try nestedUnkeyedContainer ( )
177+ let intArr = try container. decodeHex ( [ T ] . self)
178+ array. append ( intArr)
179+ }
180+ return array
181+ }
112182}
113183
114184public protocol DecodableFromHex : Decodable {
@@ -144,6 +214,7 @@ extension EthereumAddress: DecodableFromHex {
144214 }
145215}
146216
217+ // deprecated, should be removed in 3.0.0
147218private extension KeyedDecodingContainer {
148219 func decode( _ type: [ String : Any ] . Type) throws -> [ String : Any ] {
149220 var dictionary : [ String : Any ] = [ : ]
@@ -168,6 +239,7 @@ private extension KeyedDecodingContainer {
168239 }
169240}
170241
242+ // deprecated, should be removed in 3.0.0
171243private extension UnkeyedDecodingContainer {
172244 mutating func decode( _ type: [ Any ] . Type) throws -> [ Any ] {
173245 var elements : [ Any ] = [ ]
0 commit comments