Skip to content

Commit 43645d6

Browse files
committed
Restored decodeHexIfPresent and conformed it to the new format.
1 parent c850ad8 commit 43645d6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Sources/web3swift/Convenience/Decodable+Extensions.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ extension KeyedDecodingContainer {
140140
guard let array = try? container.decodeHex(type) else { throw Web3Error.dataError }
141141
return array
142142
}
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+
143157
}
144158

145159
public extension UnkeyedDecodingContainer {

0 commit comments

Comments
 (0)