Files
firebase-ios-sdk/FirebaseDatabaseSwift/Sources/Codable/DataSnapshot+ReadDecodable.swift
T
Morten Bek Ditlevsen 0175b86cb5 Swift extension for RTDB (#6568)
* Added pristine version of JSONEncoder.swift to the project

* Renamed JSONEncoder and JSONDecoder and namespaced them under Database. Skip JSONSerialization steps of encoding and decoding

* Added convenience decoding to DataSnapshot and encoding to DatabaseReference

* Convert to 2 space indentation

* Added ServerTimestamp - modelled on Firestore ditto

* Added a ServerIncrement enum/property wrapper. Not too happy about the property wrapper API though

* Fix ServerTimestamp Date decoding and encoding

* Fix ServerTimestamp Date decoding and encoding

* Added API for setting default decoder and encoder

* Fix typo and linter error

* Changed requirement for ServerIncrement generic parameter to be AdditiveArithmetic instead of Numeric. Added tests for server value coding.

* Add test for alternative ServerIncrement API and demonstrate that this version of the API also models actual optionality

* Test the default encoder and decoder

* Added more tests derived from corresponding Firestore coding tests

* Ran linter

* Delete EncoderTests.swift

Ooops, I added this file prematurely.

* Added modified version of tests from apple/swift

* Rename all but a few occurrences of 'json' and add a reference to the original file from apple/swift

* Made it possible to decode Optional types

* Updated copyright messages

* Removed CodableErrors and force cast ServerValue.timestamp() based on feedback on PR

* Changed completion handler to take a Result<Void, Error>

* Add escaping to closure parameter

* Fixed format

* Change completion handler type to accept and Optional Error rather than a Result<Void, Error>

* Remove ServerIncrement for now

* Fix function signature error

* Remove Swift 4 support. Remove default encoder/decoder configuration. Add FirebaseDatabaseSwift.podspec - I don't know if it's good though, as I don't use CocoaPods myself.

* Update podspec with fixes from review

* Run scripts/style.sh

* Add -Beta to FirebaseDatabaseSwift library
2021-03-13 10:42:14 -05:00

38 lines
1.4 KiB
Swift

/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
import FirebaseDatabase
extension DataSnapshot {
/// Retrieves the value of a snapshot and converts it to an instance of
/// caller-specified type.
/// Throws `DecodingError.valueNotFound`
/// if the document does not exist and `T` is not an `Optional`.
///
/// See `Database.Decoder` for more details about the decoding process.
///
/// - Parameters
/// - type: The type to convert the document fields to.
/// - decoder: The decoder to use to convert the document. Defaults to use
/// default decoder.
public func data<T: Decodable>(as type: T.Type,
decoder: Database.Decoder =
Database.Decoder()) throws -> T {
try decoder.decode(T.self, from: value ?? NSNull())
}
}