Last active
July 26, 2018 12:16
-
-
Save mortenbekditlevsen/917415497ba8873e20bfd8cd1638f8cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import FirebaseDatabase | |
import Result | |
public typealias DecodeResult<T> = Result<T, DecodeError> | |
public enum DecodeError: Error { | |
case noValuePresent | |
case conversionError(Error) | |
} | |
extension DataSnapshot { | |
func decoded<T>() -> DecodeResult<T> where T: Decodable { | |
guard exists(), let value = value else { | |
return Result.failure(DecodeError.noValuePresent) | |
} | |
let decoder = StructureDecoder() | |
do { | |
let d = try decoder.decode(T.self, from: value) | |
return Result.success(d) | |
} catch { | |
return Result.failure(DecodeError.conversionError(error)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment