Skip to content

Commit 720ccff

Browse files
committed
Add decodeAs function
1 parent 9299038 commit 720ccff

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Sources/Lookup/Lookup.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,21 @@ public extension Lookup {
574574
}
575575
}
576576

577+
// MARK: Decode
578+
extension Lookup {
579+
580+
public enum DecodeError: Swift.Error {
581+
case invalidJSONData
582+
}
583+
584+
public func decode<D>(as decodable: D.Type, using decoder: JSONDecoder = JSONDecoder()) throws -> D where D: Decodable {
585+
guard let jsonData else {
586+
throw DecodeError.invalidJSONData
587+
}
588+
return try decoder.decode(D.self, from: jsonData)
589+
}
590+
}
591+
577592

578593
// MARK: - Operator
579594
public func + (lhs: Lookup, rhs: Lookup) -> Lookup {

0 commit comments

Comments
 (0)