create
This commit is contained in:
24
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/CodableErrors.swift
generated
Normal file
24
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/CodableErrors.swift
generated
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2019 Google
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
public enum FirestoreDecodingError: Error {
|
||||
case decodingIsNotSupported(String)
|
||||
case fieldNameConflict(String)
|
||||
}
|
||||
|
||||
public enum FirestoreEncodingError: Error {
|
||||
case encodingIsNotSupported(String)
|
||||
}
|
||||
34
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/CodablePassThroughTypes.swift
generated
Normal file
34
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/CodablePassThroughTypes.swift
generated
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2019 Google
|
||||
*
|
||||
* 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 FirebaseSharedSwift
|
||||
import Foundation
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
struct FirestorePassthroughTypes: StructureCodingPassthroughTypeResolver {
|
||||
static func isPassthroughType<T>(_ t: T) -> Bool {
|
||||
return
|
||||
t is GeoPoint ||
|
||||
t is Timestamp ||
|
||||
t is FieldValue ||
|
||||
t is DocumentReference
|
||||
}
|
||||
}
|
||||
47
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift
generated
Normal file
47
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift
generated
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2019 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
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
public extension CollectionReference {
|
||||
/// Encodes an instance of `Encodable` and adds a new document to this collection
|
||||
/// with the encoded data, assigning it a document ID automatically.
|
||||
///
|
||||
/// See `Firestore.Encoder` for more details about the encoding process.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: An instance of `Encodable` to be encoded to a document.
|
||||
/// - encoder: An encoder instance to use to run the encoding.
|
||||
/// - completion: A block to execute once the document has been successfully
|
||||
/// written to the server. This block will not be called while
|
||||
/// the client is offline, though local changes will be visible
|
||||
/// immediately.
|
||||
/// - Returns: A `DocumentReference` pointing to the newly created document.
|
||||
@discardableResult
|
||||
func addDocument<T: Encodable>(from value: T,
|
||||
encoder: Firestore.Encoder = Firestore.Encoder(),
|
||||
completion: ((Error?) -> Void)? = nil) throws
|
||||
-> DocumentReference {
|
||||
let encoded = try encoder.encode(value)
|
||||
return addDocument(data: encoded, completion: completion)
|
||||
}
|
||||
}
|
||||
192
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/DocumentID.swift
generated
Normal file
192
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/DocumentID.swift
generated
Normal file
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
* Copyright 2019 Google
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
@_implementationOnly import FirebaseCoreExtension
|
||||
import FirebaseSharedSwift
|
||||
|
||||
extension CodingUserInfoKey {
|
||||
static let documentRefUserInfoKey =
|
||||
CodingUserInfoKey(rawValue: "DocumentRefUserInfoKey")!
|
||||
}
|
||||
|
||||
/// A type that can initialize itself from a Firestore `DocumentReference`,
|
||||
/// which makes it suitable for use with the `@DocumentID` property wrapper.
|
||||
///
|
||||
/// Firestore includes extensions that make `String` and `DocumentReference`
|
||||
/// conform to `DocumentIDWrappable`.
|
||||
///
|
||||
/// Note that Firestore ignores fields annotated with `@DocumentID` when writing
|
||||
/// so there is no requirement to convert from the wrapped type back to a
|
||||
/// `DocumentReference`.
|
||||
public protocol DocumentIDWrappable {
|
||||
/// Creates a new instance by converting from the given `DocumentReference`.
|
||||
static func wrap(_ documentReference: DocumentReference) throws -> Self
|
||||
}
|
||||
|
||||
extension String: DocumentIDWrappable {
|
||||
public static func wrap(_ documentReference: DocumentReference) throws -> Self {
|
||||
return documentReference.documentID
|
||||
}
|
||||
}
|
||||
|
||||
extension DocumentReference: DocumentIDWrappable {
|
||||
public static func wrap(_ documentReference: DocumentReference) throws -> Self {
|
||||
// Swift complains that values of type DocumentReference cannot be returned
|
||||
// as Self which is nonsensical. The cast forces this to work.
|
||||
return documentReference as! Self
|
||||
}
|
||||
}
|
||||
|
||||
/// An internal protocol that allows Firestore.Decoder to test if a type is a
|
||||
/// DocumentID of some kind without knowing the specific generic parameter that
|
||||
/// the user actually used.
|
||||
///
|
||||
/// This is required because Swift does not define an existential type for all
|
||||
/// instances of a generic class--that is, it has no wildcard or raw type that
|
||||
/// matches a generic without any specific parameter. Swift does define an
|
||||
/// existential type for protocols though, so this protocol (to which DocumentID
|
||||
/// conforms) indirectly makes it possible to test for and act on any
|
||||
/// `DocumentID<Value>`.
|
||||
protocol DocumentIDProtocol {
|
||||
/// Initializes the DocumentID from a DocumentReference.
|
||||
init(from documentReference: DocumentReference?) throws
|
||||
}
|
||||
|
||||
/// A property wrapper type that marks a `DocumentReference?` or `String?` field to
|
||||
/// be populated with a document identifier when it is read.
|
||||
///
|
||||
/// Apply the `@DocumentID` annotation to a `DocumentReference?` or `String?`
|
||||
/// property in a `Codable` object to have it populated with the document
|
||||
/// identifier when it is read and decoded from Firestore.
|
||||
///
|
||||
/// - Important: The name of the property annotated with `@DocumentID` must not
|
||||
/// match the name of any fields in the Firestore document being read or else
|
||||
/// an error will be thrown. For example, if the `Codable` object has a
|
||||
/// property named `firstName` annotated with `@DocumentID`, and the Firestore
|
||||
/// document contains a field named `firstName`, an error will be thrown when
|
||||
/// attempting to decode the document.
|
||||
///
|
||||
/// - Example Read:
|
||||
/// ````
|
||||
/// struct Player: Codable {
|
||||
/// @DocumentID var playerID: String?
|
||||
/// var health: Int64
|
||||
/// }
|
||||
///
|
||||
/// let p = try! await Firestore.firestore()
|
||||
/// .collection("players")
|
||||
/// .document("player-1")
|
||||
/// .getDocument(as: Player.self)
|
||||
/// print("\(p.playerID!) Health: \(p.health)")
|
||||
///
|
||||
/// // Prints: "Player: player-1, Health: 95"
|
||||
/// ````
|
||||
///
|
||||
/// - Important: Trying to encode/decode this type using encoders/decoders other than
|
||||
/// Firestore.Encoder throws an error.
|
||||
///
|
||||
/// - Important: When writing a Codable object containing an `@DocumentID` annotated field,
|
||||
/// its value is ignored. This allows you to read a document from one path and
|
||||
/// write it into another without adjusting the value here.
|
||||
@propertyWrapper
|
||||
public struct DocumentID<Value: DocumentIDWrappable & Codable>:
|
||||
StructureCodingUncodedUnkeyed {
|
||||
private var value: Value? = nil
|
||||
|
||||
public init(wrappedValue value: Value?) {
|
||||
if let value = value {
|
||||
logIgnoredValueWarning(value: value)
|
||||
}
|
||||
self.value = value
|
||||
}
|
||||
|
||||
public var wrappedValue: Value? {
|
||||
get { value }
|
||||
set {
|
||||
if let someNewValue = newValue {
|
||||
logIgnoredValueWarning(value: someNewValue)
|
||||
}
|
||||
value = newValue
|
||||
}
|
||||
}
|
||||
|
||||
private func logIgnoredValueWarning(value: Value) {
|
||||
FirebaseLogger.log(
|
||||
level: .warning,
|
||||
service: "[FirebaseFirestoreSwift]",
|
||||
code: "I-FST000002",
|
||||
message: """
|
||||
Attempting to initialize or set a @DocumentID property with a non-nil \
|
||||
value: "\(value)". The document ID is managed by Firestore and any \
|
||||
initialized or set value will be ignored. The ID is automatically set \
|
||||
when reading from Firestore.
|
||||
"""
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension DocumentID: DocumentIDProtocol {
|
||||
init(from documentReference: DocumentReference?) throws {
|
||||
if let documentReference = documentReference {
|
||||
value = try Value.wrap(documentReference)
|
||||
} else {
|
||||
value = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension DocumentID: Codable {
|
||||
/// A `Codable` object containing an `@DocumentID` annotated field should
|
||||
/// only be decoded with `Firestore.Decoder`; this initializer throws if an
|
||||
/// unsupported decoder is used.
|
||||
///
|
||||
/// - Parameter decoder: A decoder.
|
||||
/// - Throws: ``FirestoreDecodingError``
|
||||
public init(from decoder: Decoder) throws {
|
||||
guard let reference = decoder
|
||||
.userInfo[CodingUserInfoKey.documentRefUserInfoKey] as? DocumentReference else {
|
||||
throw FirestoreDecodingError.decodingIsNotSupported(
|
||||
"""
|
||||
Could not find DocumentReference for user info key: \(CodingUserInfoKey
|
||||
.documentRefUserInfoKey).
|
||||
DocumentID values can only be decoded with Firestore.Decoder
|
||||
"""
|
||||
)
|
||||
}
|
||||
try self.init(from: reference)
|
||||
}
|
||||
|
||||
/// A `Codable` object containing an `@DocumentID` annotated field can only
|
||||
/// be encoded with `Firestore.Encoder`; this initializer always throws.
|
||||
///
|
||||
/// - Parameter encoder: An invalid encoder.
|
||||
/// - Throws: ``FirestoreEncodingError``
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
throw FirestoreEncodingError.encodingIsNotSupported(
|
||||
"DocumentID values can only be encoded with Firestore.Encoder"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension DocumentID: Equatable where Value: Equatable {}
|
||||
|
||||
extension DocumentID: Hashable where Value: Hashable {}
|
||||
53
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/DocumentReference+Codable.swift
generated
Normal file
53
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/DocumentReference+Codable.swift
generated
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2019 Google
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
/** Mark DocumentReference to conform to Codable. */
|
||||
|
||||
/**
|
||||
* A protocol describing the encodable properties of a DocumentReference.
|
||||
*
|
||||
* Note: this protocol exists as a workaround for the Swift compiler: if the DocumentReference class
|
||||
* was extended directly to conform to Codable, the methods implementing the protocol would be need
|
||||
* to be marked required but that can't be done in an extension. Declaring the extension on the
|
||||
* protocol sidesteps this issue.
|
||||
*/
|
||||
private protocol CodableDocumentReference: Codable {}
|
||||
|
||||
/**
|
||||
* DocumentReference's codable implmentation will just throw for most
|
||||
* encoder/decoder however. It is only meant to be encoded by Firestore.Encoder/Firestore.Decoder.
|
||||
*/
|
||||
extension CodableDocumentReference {
|
||||
public init(from decoder: Decoder) throws {
|
||||
throw FirestoreDecodingError.decodingIsNotSupported(
|
||||
"DocumentReference values can only be decoded with Firestore.Decoder"
|
||||
)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
throw FirestoreEncodingError.encodingIsNotSupported(
|
||||
"DocumentReference values can only be encoded with Firestore.Encoder"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension DocumentReference: CodableDocumentReference {}
|
||||
123
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
generated
Normal file
123
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
generated
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2021 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
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
public extension DocumentReference {
|
||||
/// Fetches and decodes the document referenced by this `DocumentReference`.
|
||||
///
|
||||
/// This allows users to retrieve a Firestore document and have it decoded to
|
||||
/// an instance of caller-specified type as follows:
|
||||
/// ```swift
|
||||
/// ref.getDocument(as: Book.self) { result in
|
||||
/// do {
|
||||
/// let book = try result.get()
|
||||
/// } catch {
|
||||
/// // Handle error
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// This method attempts to provide up-to-date data when possible by waiting
|
||||
/// for data from the server, but it may return cached data or fail if you are
|
||||
/// offline and the server cannot be reached. If `T` denotes an optional
|
||||
/// type, the method returns a successful status with a value of `nil` for
|
||||
/// non-existing documents.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - as: A `Decodable` type to convert the document fields to.
|
||||
/// - serverTimestampBehavior: Configures how server timestamps that have
|
||||
/// not yet been set to their final value are returned from the snapshot.
|
||||
/// - decoder: The decoder to use to convert the document. Defaults to use
|
||||
/// the default decoder.
|
||||
/// - source: Indicates whether the results should be fetched from the cache only
|
||||
/// (`Source.cache`), the server only (`Source.server`), or to attempt the
|
||||
/// server and fall back to the cache (`Source.default`).
|
||||
/// - completion: The closure to call when the document snapshot has been
|
||||
/// fetched and decoded.
|
||||
func getDocument<T: Decodable>(as type: T.Type,
|
||||
with serverTimestampBehavior: ServerTimestampBehavior =
|
||||
.none,
|
||||
decoder: Firestore.Decoder = .init(),
|
||||
source: FirestoreSource = .default,
|
||||
completion: @escaping (Result<T, Error>) -> Void) {
|
||||
getDocument(source: source) { snapshot, error in
|
||||
guard let snapshot = snapshot else {
|
||||
/**
|
||||
* Force unwrapping here is fine since this logic corresponds to the auto-synthesized
|
||||
* async/await wrappers for Objective-C functions with callbacks taking an object and an error
|
||||
* parameter. The API should (and does) guarantee that either object or error is set, but never both.
|
||||
* For more details see:
|
||||
* https://github.com/firebase/firebase-ios-sdk/pull/9101#discussion_r809117034
|
||||
*/
|
||||
completion(.failure(error!))
|
||||
return
|
||||
}
|
||||
let result = Result {
|
||||
try snapshot.data(as: T.self,
|
||||
with: serverTimestampBehavior,
|
||||
decoder: decoder)
|
||||
}
|
||||
completion(result)
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetches and decodes the document referenced by this `DocumentReference`.
|
||||
///
|
||||
/// This allows users to retrieve a Firestore document and have it decoded
|
||||
/// to an instance of caller-specified type as follows:
|
||||
/// ```swift
|
||||
/// do {
|
||||
/// let book = try await ref.getDocument(as: Book.self)
|
||||
/// } catch {
|
||||
/// // Handle error
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// This method attempts to provide up-to-date data when possible by waiting
|
||||
/// for data from the server, but it may return cached data or fail if you
|
||||
/// are offline and the server cannot be reached. If `T` denotes
|
||||
/// an optional type, the method returns a successful status with a value
|
||||
/// of `nil` for non-existing documents.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - as: A `Decodable` type to convert the document fields to.
|
||||
/// - serverTimestampBehavior: Configures how server timestamps that have
|
||||
/// not yet been set to their final value are returned from the
|
||||
/// snapshot.
|
||||
/// - decoder: The decoder to use to convert the document. Defaults to use
|
||||
/// the default decoder.
|
||||
/// - source: Indicates whether the results should be fetched from the cache only
|
||||
/// (`Source.cache`), the server only (`Source.server`), or to attempt the
|
||||
/// server and fall back to the cache (`Source.default`).
|
||||
/// - Returns: This instance of the supplied `Decodable` type `T`.
|
||||
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
|
||||
func getDocument<T: Decodable>(as type: T.Type,
|
||||
with serverTimestampBehavior: ServerTimestampBehavior =
|
||||
.none,
|
||||
decoder: Firestore.Decoder = .init(),
|
||||
source: FirestoreSource = .default) async throws -> T {
|
||||
let snapshot = try await getDocument(source: source)
|
||||
return try snapshot.data(as: T.self,
|
||||
with: serverTimestampBehavior,
|
||||
decoder: decoder)
|
||||
}
|
||||
}
|
||||
96
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/DocumentReference+WriteEncodable.swift
generated
Normal file
96
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/DocumentReference+WriteEncodable.swift
generated
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2019 Google
|
||||
*
|
||||
* 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
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
public extension DocumentReference {
|
||||
/// Encodes an instance of `Encodable` and overwrites the encoded data
|
||||
/// to the document referred by this `DocumentReference`. If no document exists,
|
||||
/// it is created. If a document already exists, it is overwritten.
|
||||
///
|
||||
/// See `Firestore.Encoder` for more details about the encoding process.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: An instance of `Encodable` to be encoded to a document.
|
||||
/// - encoder: An encoder instance to use to run the encoding.
|
||||
/// - completion: A closure to execute once the document has been successfully
|
||||
/// written to the server. This closure will not be called while
|
||||
/// the client is offline, though local changes will be visible
|
||||
/// immediately.
|
||||
func setData<T: Encodable>(from value: T,
|
||||
encoder: Firestore.Encoder = Firestore.Encoder(),
|
||||
completion: ((Error?) -> Void)? = nil) throws {
|
||||
let encoded = try encoder.encode(value)
|
||||
setData(encoded, completion: completion)
|
||||
}
|
||||
|
||||
/// Encodes an instance of `Encodable` and overwrites the encoded data
|
||||
/// to the document referred by this `DocumentReference`. If no document exists,
|
||||
/// it is created. If a document already exists, it is overwritten. If you pass
|
||||
/// merge:true, the provided `Encodable` will be merged into any existing document.
|
||||
///
|
||||
/// See `Firestore.Encoder` for more details about the encoding process.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: An instance of `Encodable` to be encoded to a document.
|
||||
/// - merge: Whether to merge the provided `Encodable` into any existing
|
||||
/// document.
|
||||
/// - encoder: An encoder instance to use to run the encoding.
|
||||
/// - completion: A closure to execute once the document has been successfully
|
||||
/// written to the server. This closure will not be called while
|
||||
/// the client is offline, though local changes will be visible
|
||||
/// immediately.
|
||||
func setData<T: Encodable>(from value: T,
|
||||
merge: Bool,
|
||||
encoder: Firestore.Encoder = Firestore.Encoder(),
|
||||
completion: ((Error?) -> Void)? = nil) throws {
|
||||
let encoded = try encoder.encode(value)
|
||||
setData(encoded, merge: merge, completion: completion)
|
||||
}
|
||||
|
||||
/// Encodes an instance of `Encodable` and writes the encoded data to the document referred
|
||||
/// by this `DocumentReference` by only replacing the fields specified under `mergeFields`.
|
||||
/// Any field that is not specified in mergeFields is ignored and remains untouched. If the
|
||||
/// document doesn’t yet exist, this method creates it and then sets the data.
|
||||
///
|
||||
/// It is an error to include a field in `mergeFields` that does not have a corresponding
|
||||
/// field in the `Encodable`.
|
||||
///
|
||||
/// See `Firestore.Encoder` for more details about the encoding process.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: An instance of `Encodable` to be encoded to a document.
|
||||
/// - mergeFields: Array of `String` or `FieldPath` elements specifying which fields to
|
||||
/// merge. Fields can contain dots to reference nested fields within the
|
||||
/// document.
|
||||
/// - encoder: An encoder instance to use to run the encoding.
|
||||
/// - completion: A closure to execute once the document has been successfully
|
||||
/// written to the server. This closure will not be called while
|
||||
/// the client is offline, though local changes will be visible
|
||||
/// immediately.
|
||||
func setData<T: Encodable>(from value: T,
|
||||
mergeFields: [Any],
|
||||
encoder: Firestore.Encoder = Firestore.Encoder(),
|
||||
completion: ((Error?) -> Void)? = nil) throws {
|
||||
let encoded = try encoder.encode(value)
|
||||
setData(encoded, mergeFields: mergeFields, completion: completion)
|
||||
}
|
||||
}
|
||||
46
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/DocumentSnapshot+ReadDecodable.swift
generated
Normal file
46
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/DocumentSnapshot+ReadDecodable.swift
generated
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2019 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
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
public extension DocumentSnapshot {
|
||||
/// Retrieves all fields in a document and converts them to an instance of
|
||||
/// caller-specified type.
|
||||
///
|
||||
/// By default, server-provided timestamps that have not yet been set to their
|
||||
/// final value will be returned as `NSNull`. Pass `serverTimestampBehavior`
|
||||
/// to configure this behavior.
|
||||
///
|
||||
/// See `Firestore.Decoder` for more details about the decoding process.
|
||||
///
|
||||
/// - Parameters
|
||||
/// - type: The type to convert the document fields to.
|
||||
/// - serverTimestampBehavior: Configures how server timestamps that have
|
||||
/// not yet been set to their final value are returned from the snapshot.
|
||||
/// - decoder: The decoder to use to convert the document. Defaults to use
|
||||
/// the default decoder.
|
||||
func data<T: Decodable>(as type: T.Type,
|
||||
with serverTimestampBehavior: ServerTimestampBehavior = .none,
|
||||
decoder: Firestore.Decoder = .init()) throws -> T {
|
||||
let d: Any = data(with: serverTimestampBehavior) ?? NSNull()
|
||||
return try decoder.decode(T.self, from: d, in: reference)
|
||||
}
|
||||
}
|
||||
104
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/EncoderDecoder.swift
generated
Normal file
104
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/EncoderDecoder.swift
generated
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2022 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.
|
||||
*/
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
import FirebaseSharedSwift
|
||||
import Foundation
|
||||
|
||||
public extension Firestore {
|
||||
class Encoder {
|
||||
/// The strategy to use in encoding dates. Defaults to `.timestamp`.
|
||||
public var dateEncodingStrategy: FirebaseDataEncoder.DateEncodingStrategy = .timestamp
|
||||
|
||||
/// Firestore encodes Data as `NSData` blobs versus the default .base64 strings.
|
||||
public var dataEncodingStrategy: FirebaseDataEncoder.DataEncodingStrategy = .blob
|
||||
|
||||
/// The strategy to use in encoding non-conforming numbers. Defaults to `.throw`.
|
||||
public var nonConformingFloatEncodingStrategy: FirebaseDataEncoder
|
||||
.NonConformingFloatEncodingStrategy = .throw
|
||||
|
||||
/// The strategy to use for encoding keys. Defaults to `.useDefaultKeys`.
|
||||
public var keyEncodingStrategy: FirebaseDataEncoder.KeyEncodingStrategy = .useDefaultKeys
|
||||
|
||||
/// Contextual user-provided information for use during encoding.
|
||||
public var userInfo: [CodingUserInfoKey: Any] = [:]
|
||||
|
||||
public func encode<T: Encodable>(_ value: T) throws -> [String: Any] {
|
||||
let encoder = FirebaseDataEncoder()
|
||||
encoder.dateEncodingStrategy = dateEncodingStrategy
|
||||
encoder.dataEncodingStrategy = dataEncodingStrategy
|
||||
encoder.nonConformingFloatEncodingStrategy = nonConformingFloatEncodingStrategy
|
||||
encoder.keyEncodingStrategy = keyEncodingStrategy
|
||||
encoder.passthroughTypeResolver = FirestorePassthroughTypes.self
|
||||
encoder.userInfo = userInfo
|
||||
let encoded = try encoder.encode(value)
|
||||
guard let dictionaryValue = encoded as? [String: Any] else {
|
||||
throw EncodingError
|
||||
.invalidValue(value,
|
||||
EncodingError
|
||||
.Context(codingPath: [],
|
||||
debugDescription: "Top-level \(T.self) is not allowed."))
|
||||
}
|
||||
return dictionaryValue
|
||||
}
|
||||
|
||||
public init() {}
|
||||
}
|
||||
|
||||
class Decoder {
|
||||
/// The strategy to use in decoding dates. Defaults to `.timestamp`.
|
||||
public var dateDecodingStrategy: FirebaseDataDecoder.DateDecodingStrategy = .timestamp
|
||||
|
||||
/// Firestore decodes Data from `NSData` blobs versus the default .base64 strings.
|
||||
public var dataDecodingStrategy: FirebaseDataDecoder.DataDecodingStrategy = .blob
|
||||
|
||||
/// The strategy to use in decoding non-conforming numbers. Defaults to `.throw`.
|
||||
public var nonConformingFloatDecodingStrategy: FirebaseDataDecoder
|
||||
.NonConformingFloatDecodingStrategy = .throw
|
||||
|
||||
/// The strategy to use for decoding keys. Defaults to `.useDefaultKeys`.
|
||||
public var keyDecodingStrategy: FirebaseDataDecoder.KeyDecodingStrategy = .useDefaultKeys
|
||||
|
||||
/// Contextual user-provided information for use during decoding.
|
||||
public var userInfo: [CodingUserInfoKey: Any] = [:]
|
||||
|
||||
public func decode<T: Decodable>(_ t: T.Type, from data: Any) throws -> T {
|
||||
let decoder = FirebaseDataDecoder()
|
||||
decoder.dateDecodingStrategy = dateDecodingStrategy
|
||||
decoder.dataDecodingStrategy = dataDecodingStrategy
|
||||
decoder.nonConformingFloatDecodingStrategy = nonConformingFloatDecodingStrategy
|
||||
decoder.keyDecodingStrategy = keyDecodingStrategy
|
||||
decoder.passthroughTypeResolver = FirestorePassthroughTypes.self
|
||||
decoder.userInfo = userInfo
|
||||
// configure for firestore
|
||||
return try decoder.decode(t, from: data)
|
||||
}
|
||||
|
||||
public func decode<T: Decodable>(_ t: T.Type, from data: Any,
|
||||
in reference: DocumentReference?) throws -> T {
|
||||
if let reference = reference {
|
||||
userInfo[CodingUserInfoKey.documentRefUserInfoKey] = reference
|
||||
}
|
||||
return try decode(T.self, from: data)
|
||||
}
|
||||
|
||||
public init() {}
|
||||
}
|
||||
}
|
||||
67
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/ExplicitNull.swift
generated
Normal file
67
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/ExplicitNull.swift
generated
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2019 Google
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
/// Wraps an `Optional` field in a `Codable` object such that when the field
|
||||
/// has a `nil` value it will encode to a null value in Firestore. Normally,
|
||||
/// optional fields are omitted from the encoded document.
|
||||
///
|
||||
/// This is useful for ensuring a field is present in a Firestore document,
|
||||
/// even when there is no associated value.
|
||||
@propertyWrapper
|
||||
public struct ExplicitNull<Value> {
|
||||
var value: Value?
|
||||
|
||||
public init(wrappedValue value: Value?) {
|
||||
self.value = value
|
||||
}
|
||||
|
||||
public var wrappedValue: Value? {
|
||||
get { value }
|
||||
set { value = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
extension ExplicitNull: Equatable where Value: Equatable {}
|
||||
|
||||
extension ExplicitNull: Hashable where Value: Hashable {}
|
||||
|
||||
extension ExplicitNull: Encodable where Value: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
if let value = value {
|
||||
try container.encode(value)
|
||||
} else {
|
||||
try container.encodeNil()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ExplicitNull: Decodable where Value: Decodable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if container.decodeNil() {
|
||||
value = nil
|
||||
} else {
|
||||
value = try container.decode(Value.self)
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/FieldValue+Encodable.swift
generated
Normal file
32
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/FieldValue+Encodable.swift
generated
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2019 Google
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
/** Extends FieldValue to conform to Encodable. */
|
||||
extension FieldValue: Encodable {
|
||||
/// Encoding a FieldValue will throw by default unless the encoder implementation
|
||||
/// explicitly handles it, which is what Firestore.Encoder does.
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
throw FirestoreEncodingError.encodingIsNotSupported(
|
||||
"FieldValue values can only be encoded with Firestore.Encoder"
|
||||
)
|
||||
}
|
||||
}
|
||||
66
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/GeoPoint+Codable.swift
generated
Normal file
66
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/GeoPoint+Codable.swift
generated
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2018 Google
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
/**
|
||||
* A protocol describing the encodable properties of a GeoPoint.
|
||||
*
|
||||
* Note: this protocol exists as a workaround for the Swift compiler: if the GeoPoint class
|
||||
* was extended directly to conform to Codable, the methods implementing the protocol would be need
|
||||
* to be marked required but that can't be done in an extension. Declaring the extension on the
|
||||
* protocol sidesteps this issue.
|
||||
*/
|
||||
private protocol CodableGeoPoint: Codable {
|
||||
var latitude: Double { get }
|
||||
var longitude: Double { get }
|
||||
|
||||
init(latitude: Double, longitude: Double)
|
||||
}
|
||||
|
||||
/** The keys in a GeoPoint. Must match the properties of CodableGeoPoint. */
|
||||
private enum GeoPointKeys: String, CodingKey {
|
||||
case latitude
|
||||
case longitude
|
||||
}
|
||||
|
||||
/**
|
||||
* An extension of GeoPoint that implements the behavior of the Codable protocol.
|
||||
*
|
||||
* Note: this is implemented manually here because the Swift compiler can't synthesize these methods
|
||||
* when declaring an extension to conform to Codable.
|
||||
*/
|
||||
extension CodableGeoPoint {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: GeoPointKeys.self)
|
||||
let latitude = try container.decode(Double.self, forKey: .latitude)
|
||||
let longitude = try container.decode(Double.self, forKey: .longitude)
|
||||
self.init(latitude: latitude, longitude: longitude)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: GeoPointKeys.self)
|
||||
try container.encode(latitude, forKey: .latitude)
|
||||
try container.encode(longitude, forKey: .longitude)
|
||||
}
|
||||
}
|
||||
|
||||
/** Extends GeoPoint to conform to Codable. */
|
||||
extension GeoPoint: CodableGeoPoint {}
|
||||
111
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/ServerTimestamp.swift
generated
Normal file
111
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/ServerTimestamp.swift
generated
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright 2019 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.
|
||||
*/
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
/// A type that can initialize itself from a Firestore Timestamp, which makes
|
||||
/// it suitable for use with the `@ServerTimestamp` property wrapper.
|
||||
///
|
||||
/// Firestore includes extensions that make `Timestamp` and `Date` conform to
|
||||
/// `ServerTimestampWrappable`.
|
||||
public protocol ServerTimestampWrappable {
|
||||
/// Creates a new instance by converting from the given `Timestamp`.
|
||||
///
|
||||
/// - Parameter timestamp: The timestamp from which to convert.
|
||||
static func wrap(_ timestamp: Timestamp) throws -> Self
|
||||
|
||||
/// Converts this value into a Firestore `Timestamp`.
|
||||
///
|
||||
/// - Returns: A `Timestamp` representation of this value.
|
||||
static func unwrap(_ value: Self) throws -> Timestamp
|
||||
}
|
||||
|
||||
extension Date: ServerTimestampWrappable {
|
||||
public static func wrap(_ timestamp: Timestamp) throws -> Self {
|
||||
return timestamp.dateValue()
|
||||
}
|
||||
|
||||
public static func unwrap(_ value: Self) throws -> Timestamp {
|
||||
return Timestamp(date: value)
|
||||
}
|
||||
}
|
||||
|
||||
extension Timestamp: ServerTimestampWrappable {
|
||||
public static func wrap(_ timestamp: Timestamp) throws -> Self {
|
||||
return timestamp as! Self
|
||||
}
|
||||
|
||||
public static func unwrap(_ value: Timestamp) throws -> Timestamp {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
/// A property wrapper that marks an `Optional<Timestamp>` field to be
|
||||
/// populated with a server timestamp. If a `Codable` object being written
|
||||
/// contains a `nil` for an `@ServerTimestamp`-annotated field, it will be
|
||||
/// replaced with `FieldValue.serverTimestamp()` as it is sent.
|
||||
///
|
||||
/// Example:
|
||||
/// ```
|
||||
/// struct CustomModel {
|
||||
/// @ServerTimestamp var ts: Timestamp?
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Then writing `CustomModel(ts: nil)` will tell server to fill `ts` with
|
||||
/// current timestamp.
|
||||
@propertyWrapper
|
||||
public struct ServerTimestamp<Value>: Codable
|
||||
where Value: ServerTimestampWrappable & Codable {
|
||||
var value: Value?
|
||||
|
||||
public init(wrappedValue value: Value?) {
|
||||
self.value = value
|
||||
}
|
||||
|
||||
public var wrappedValue: Value? {
|
||||
get { value }
|
||||
set { value = newValue }
|
||||
}
|
||||
|
||||
// MARK: Codable
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if container.decodeNil() {
|
||||
value = nil
|
||||
} else {
|
||||
value = try Value.wrap(container.decode(Timestamp.self))
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
if let value = value {
|
||||
try container.encode(Value.unwrap(value))
|
||||
} else {
|
||||
try container.encode(FieldValue.serverTimestamp())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ServerTimestamp: Equatable where Value: Equatable {}
|
||||
|
||||
extension ServerTimestamp: Hashable where Value: Hashable {}
|
||||
66
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/Timestamp+Codable.swift
generated
Normal file
66
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/Timestamp+Codable.swift
generated
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2019 Google
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
/**
|
||||
* A protocol describing the encodable properties of a Timestamp.
|
||||
*
|
||||
* Note: this protocol exists as a workaround for the Swift compiler: if the Timestamp class
|
||||
* was extended directly to conform to Codable, the methods implementing the protocol would be need
|
||||
* to be marked required but that can't be done in an extension. Declaring the extension on the
|
||||
* protocol sidesteps this issue.
|
||||
*/
|
||||
private protocol CodableTimestamp: Codable {
|
||||
var seconds: Int64 { get }
|
||||
var nanoseconds: Int32 { get }
|
||||
|
||||
init(seconds: Int64, nanoseconds: Int32)
|
||||
}
|
||||
|
||||
/** The keys in a Timestamp. Must match the properties of CodableTimestamp. */
|
||||
private enum TimestampKeys: String, CodingKey {
|
||||
case seconds
|
||||
case nanoseconds
|
||||
}
|
||||
|
||||
/**
|
||||
* An extension of Timestamp that implements the behavior of the Codable protocol.
|
||||
*
|
||||
* Note: this is implemented manually here because the Swift compiler can't synthesize these methods
|
||||
* when declaring an extension to conform to Codable.
|
||||
*/
|
||||
extension CodableTimestamp {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: TimestampKeys.self)
|
||||
let seconds = try container.decode(Int64.self, forKey: .seconds)
|
||||
let nanoseconds = try container.decode(Int32.self, forKey: .nanoseconds)
|
||||
self.init(seconds: seconds, nanoseconds: nanoseconds)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: TimestampKeys.self)
|
||||
try container.encode(seconds, forKey: .seconds)
|
||||
try container.encode(nanoseconds, forKey: .nanoseconds)
|
||||
}
|
||||
}
|
||||
|
||||
/** Extends Timestamp to conform to Codable. */
|
||||
extension Timestamp: CodableTimestamp {}
|
||||
34
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/TimestampDecodingStrategy.swift
generated
Normal file
34
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/TimestampDecodingStrategy.swift
generated
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2022 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
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
import FirebaseSharedSwift
|
||||
|
||||
public extension FirebaseDataDecoder.DateDecodingStrategy {
|
||||
/// Decode the `Date` from a Firestore `Timestamp`
|
||||
static var timestamp: FirebaseDataDecoder.DateDecodingStrategy {
|
||||
return .custom { decoder in
|
||||
let container = try decoder.singleValueContainer()
|
||||
let value = try container.decode(Timestamp.self)
|
||||
return value.dateValue()
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/TimestampEncodingStrategy.swift
generated
Normal file
34
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/TimestampEncodingStrategy.swift
generated
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2022 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.
|
||||
*/
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
import FirebaseSharedSwift
|
||||
import Foundation
|
||||
|
||||
public extension FirebaseDataEncoder.DateEncodingStrategy {
|
||||
/// Encode the `Date` as a Firestore `Timestamp`.
|
||||
static var timestamp: FirebaseDataEncoder.DateEncodingStrategy {
|
||||
return .custom { date, encoder in
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(Timestamp(date: date))
|
||||
}
|
||||
}
|
||||
}
|
||||
99
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/Transaction+WriteEncodable.swift
generated
Normal file
99
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/Transaction+WriteEncodable.swift
generated
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2019 Google
|
||||
*
|
||||
* 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
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
public extension Transaction {
|
||||
/// Encodes an instance of `Encodable` and overwrites the encoded data
|
||||
/// to the document referred by `doc`. If no document exists,
|
||||
/// it is created. If a document already exists, it is overwritten.
|
||||
///
|
||||
/// See `Firestore.Encoder` for more details about the encoding process.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: a instance of `Encoded` to be encoded to a document.
|
||||
/// - encoder: The encoder instance to use to run the encoding.
|
||||
/// - doc: The document to create/overwrite the encoded data to.
|
||||
/// - Returns: This instance of `Transaction`. Used for chaining method calls.
|
||||
@discardableResult
|
||||
func setData<T: Encodable>(from value: T,
|
||||
forDocument doc: DocumentReference,
|
||||
encoder: Firestore.Encoder = Firestore
|
||||
.Encoder()) throws -> Transaction {
|
||||
let encoded = try encoder.encode(value)
|
||||
setData(encoded, forDocument: doc)
|
||||
return self
|
||||
}
|
||||
|
||||
/// Encodes an instance of `Encodable` and overwrites the encoded data
|
||||
/// to the document referred by `doc`. If no document exists,
|
||||
/// it is created. If a document already exists, it is overwritten. If you pass
|
||||
/// merge:true, the provided `Encodable` will be merged into any existing document.
|
||||
///
|
||||
/// See `Firestore.Encoder` for more details about the encoding process.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: An instance of `Encodable` to be encoded to a document.
|
||||
/// - doc: The document to create/overwrite the encoded data to.
|
||||
/// - merge: Whether to merge the provided `Encodable` into any existing
|
||||
/// document.
|
||||
/// - encoder: The encoder instance to use to run the encoding.
|
||||
/// - Returns: This instance of `Transaction`. Used for chaining method calls.
|
||||
@discardableResult
|
||||
func setData<T: Encodable>(from value: T,
|
||||
forDocument doc: DocumentReference,
|
||||
merge: Bool,
|
||||
encoder: Firestore.Encoder = Firestore
|
||||
.Encoder()) throws -> Transaction {
|
||||
let encoded = try encoder.encode(value)
|
||||
setData(encoded, forDocument: doc, merge: merge)
|
||||
return self
|
||||
}
|
||||
|
||||
/// Encodes an instance of `Encodable` and writes the encoded data to the document referred
|
||||
/// by `doc` by only replacing the fields specified under `mergeFields`.
|
||||
/// Any field that is not specified in mergeFields is ignored and remains untouched. If the
|
||||
/// document doesn’t yet exist, this method creates it and then sets the data.
|
||||
///
|
||||
/// It is an error to include a field in `mergeFields` that does not have a corresponding
|
||||
/// field in the `Encodable`.
|
||||
///
|
||||
/// See `Firestore.Encoder` for more details about the encoding process.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: An instance of `Encodable` to be encoded to a document.
|
||||
/// - doc: The document to create/overwrite the encoded data to.
|
||||
/// - mergeFields: Array of `String` or `FieldPath` elements specifying which fields to
|
||||
/// merge. Fields can contain dots to reference nested fields within the
|
||||
/// document.
|
||||
/// - encoder: The encoder instance to use to run the encoding.
|
||||
/// - Returns: This instance of `Transaction`. Used for chaining method calls.
|
||||
@discardableResult
|
||||
func setData<T: Encodable>(from value: T,
|
||||
forDocument doc: DocumentReference,
|
||||
mergeFields: [Any],
|
||||
encoder: Firestore.Encoder = Firestore
|
||||
.Encoder()) throws -> Transaction {
|
||||
let encoded = try encoder.encode(value)
|
||||
setData(encoded, forDocument: doc, mergeFields: mergeFields)
|
||||
return self
|
||||
}
|
||||
}
|
||||
99
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/WriteBatch+WriteEncodable.swift
generated
Normal file
99
Pods/FirebaseFirestore/Firestore/Swift/Source/Codable/WriteBatch+WriteEncodable.swift
generated
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2019 Google
|
||||
*
|
||||
* 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
|
||||
#if SWIFT_PACKAGE
|
||||
@_exported import FirebaseFirestoreInternalWrapper
|
||||
#else
|
||||
@_exported import FirebaseFirestoreInternal
|
||||
#endif // SWIFT_PACKAGE
|
||||
|
||||
public extension WriteBatch {
|
||||
/// Encodes an instance of `Encodable` and overwrites the encoded data
|
||||
/// to the document referred by `doc`. If no document exists,
|
||||
/// it is created. If a document already exists, it is overwritten.
|
||||
///
|
||||
/// See `Firestore.Encoder` for more details about the encoding process.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: An instance of `Encodable` to be encoded to a document.
|
||||
/// - encoder: The encoder instance to use to run the encoding.
|
||||
/// - doc: The document to create/overwrite the encoded data to.
|
||||
/// - Returns: This instance of `WriteBatch`. Used for chaining method calls.
|
||||
@discardableResult
|
||||
func setData<T: Encodable>(from value: T,
|
||||
forDocument doc: DocumentReference,
|
||||
encoder: Firestore.Encoder = Firestore
|
||||
.Encoder()) throws -> WriteBatch {
|
||||
let encoded = try encoder.encode(value)
|
||||
setData(encoded, forDocument: doc)
|
||||
return self
|
||||
}
|
||||
|
||||
/// Encodes an instance of `Encodable` and overwrites the encoded data
|
||||
/// to the document referred by `doc`. If no document exists,
|
||||
/// it is created. If a document already exists, it is overwritten. If you pass
|
||||
/// merge:true, the provided `Encodable` will be merged into any existing document.
|
||||
///
|
||||
/// See `Firestore.Encoder` for more details about the encoding process.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: An instance of `Encodable` to be encoded to a document.
|
||||
/// - doc: The document to create/overwrite the encoded data to.
|
||||
/// - merge: Whether to merge the provided `Encodable` into any existing
|
||||
/// document.
|
||||
/// - encoder: The encoder instance to use to run the encoding.
|
||||
/// - Returns: This instance of `WriteBatch`. Used for chaining method calls.
|
||||
@discardableResult
|
||||
func setData<T: Encodable>(from value: T,
|
||||
forDocument doc: DocumentReference,
|
||||
merge: Bool,
|
||||
encoder: Firestore.Encoder = Firestore
|
||||
.Encoder()) throws -> WriteBatch {
|
||||
let encoded = try encoder.encode(value)
|
||||
setData(encoded, forDocument: doc, merge: merge)
|
||||
return self
|
||||
}
|
||||
|
||||
/// Encodes an instance of `Encodable` and writes the encoded data to the document referred
|
||||
/// by `doc` by only replacing the fields specified under `mergeFields`.
|
||||
/// Any field that is not specified in mergeFields is ignored and remains untouched. If the
|
||||
/// document doesn’t yet exist, this method creates it and then sets the data.
|
||||
///
|
||||
/// It is an error to include a field in `mergeFields` that does not have a corresponding
|
||||
/// field in the `Encodable`.
|
||||
///
|
||||
/// See `Firestore.Encoder` for more details about the encoding process.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: An instance of `Encodable` to be encoded to a document.
|
||||
/// - doc: The document to create/overwrite the encoded data to.
|
||||
/// - mergeFields: Array of `String` or `FieldPath` elements specifying which fields to
|
||||
/// merge. Fields can contain dots to reference nested fields within the
|
||||
/// document.
|
||||
/// - encoder: The encoder instance to use to run the encoding.
|
||||
/// - Returns: This instance of `WriteBatch`. Used for chaining method calls.
|
||||
@discardableResult
|
||||
func setData<T: Encodable>(from value: T,
|
||||
forDocument doc: DocumentReference,
|
||||
mergeFields: [Any],
|
||||
encoder: Firestore.Encoder = Firestore
|
||||
.Encoder()) throws -> WriteBatch {
|
||||
let encoded = try encoder.encode(value)
|
||||
setData(encoded, forDocument: doc, mergeFields: mergeFields)
|
||||
return self
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user