Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continue Firestore Codable 3 #2229

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion FirebaseFirestoreSwift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Pod::Spec.new do |s|
s.name = 'FirebaseFirestoreSwift'
s.version = '0.1'
s.version = '0.2.0'
s.summary = 'Google Cloud Firestore for iOS Swift Extensions'

s.description = <<-DESC
Expand Down
28 changes: 26 additions & 2 deletions Firestore/Example/Firestore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,13 @@
DE03B2D61F2149D600A30B9C /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F58D195388D20070C39A /* Foundation.framework */; };
DE03B2DD1F2149D600A30B9C /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6003F5B8195388D20070C39A /* InfoPlist.strings */; };
DE03B3631F215E1A00A30B9C /* CAcert.pem in Resources */ = {isa = PBXBuildFile; fileRef = DE03B3621F215E1600A30B9C /* CAcert.pem */; };
DE26C61621C15FCC00DE141A /* CodableDocumentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE26C61521C15FCC00DE141A /* CodableDocumentTests.swift */; };
DE2EF0851F3D0B6E003D0CDC /* FSTArraySortedDictionaryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE2EF07E1F3D0B6E003D0CDC /* FSTArraySortedDictionaryTests.m */; };
DE2EF0861F3D0B6E003D0CDC /* FSTImmutableSortedDictionary+Testing.m in Sources */ = {isa = PBXBuildFile; fileRef = DE2EF0801F3D0B6E003D0CDC /* FSTImmutableSortedDictionary+Testing.m */; };
DE2EF0871F3D0B6E003D0CDC /* FSTImmutableSortedSet+Testing.m in Sources */ = {isa = PBXBuildFile; fileRef = DE2EF0821F3D0B6E003D0CDC /* FSTImmutableSortedSet+Testing.m */; };
DE2EF0881F3D0B6E003D0CDC /* FSTTreeSortedDictionaryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE2EF0841F3D0B6E003D0CDC /* FSTTreeSortedDictionaryTests.m */; };
DE84BBB421C18F060048A176 /* FSTAPIHelpers.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E04E202154AA00B64F25 /* FSTAPIHelpers.mm */; };
DE84BBB921C1A6ED0048A176 /* FSTHelpers.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E03A2021401F00B64F25 /* FSTHelpers.mm */; };
EBFC611B1BF195D0EC710AF4 /* app_testing.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5467FB07203E6A44009C9584 /* app_testing.mm */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -545,6 +548,8 @@
DE03B2E91F2149D600A30B9C /* Firestore_IntegrationTests_iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Firestore_IntegrationTests_iOS.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
DE03B3621F215E1600A30B9C /* CAcert.pem */ = {isa = PBXFileReference; lastKnownFileType = text; path = CAcert.pem; sourceTree = "<group>"; };
DE0761F61F2FE68D003233AF /* BasicCompileTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BasicCompileTests.swift; sourceTree = "<group>"; };
DE26C61521C15FCC00DE141A /* CodableDocumentTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodableDocumentTests.swift; sourceTree = "<group>"; };

DE2EF07E1F3D0B6E003D0CDC /* FSTArraySortedDictionaryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FSTArraySortedDictionaryTests.m; path = ../../third_party/Immutable/Tests/FSTArraySortedDictionaryTests.m; sourceTree = "<group>"; };
DE2EF07F1F3D0B6E003D0CDC /* FSTImmutableSortedDictionary+Testing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "FSTImmutableSortedDictionary+Testing.h"; path = "../../third_party/Immutable/Tests/FSTImmutableSortedDictionary+Testing.h"; sourceTree = "<group>"; };
DE2EF0801F3D0B6E003D0CDC /* FSTImmutableSortedDictionary+Testing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "FSTImmutableSortedDictionary+Testing.m"; path = "../../third_party/Immutable/Tests/FSTImmutableSortedDictionary+Testing.m"; sourceTree = "<group>"; };
Expand All @@ -557,6 +562,7 @@
DE51B1981F0D48AC0013853F /* FSTSpecTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FSTSpecTests.h; sourceTree = "<group>"; };
DE51B19A1F0D48AC0013853F /* FSTSyncEngineTestDriver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FSTSyncEngineTestDriver.h; sourceTree = "<group>"; };
DE51B1A71F0D48AC0013853F /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
DE84BBB821C1951E0048A176 /* BridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BridgingHeader.h; sourceTree = "<group>"; };
E592181BFD7C53C305123739 /* Pods-Firestore_Tests_iOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_Tests_iOS.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_Tests_iOS/Pods-Firestore_Tests_iOS.debug.xcconfig"; sourceTree = "<group>"; };
ECEBABC7E7B693BE808A1052 /* Pods_Firestore_IntegrationTests_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Firestore_IntegrationTests_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
ED4B3E3EA0EBF3ED19A07060 /* grpc_stream_tester.h */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.h; path = grpc_stream_tester.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -757,6 +763,7 @@
5495EB012040E90200EBA509 /* Codable */ = {
isa = PBXGroup;
children = (
DE26C61521C15FCC00DE141A /* CodableDocumentTests.swift */,
5495EB022040E90200EBA509 /* CodableGeoPointTests.swift */,
);
path = Codable;
Expand All @@ -779,6 +786,7 @@
544A20ED20F6C046004E52CD /* API */,
5495EB012040E90200EBA509 /* Codable */,
54C9EDF52040E16300A969CD /* Info.plist */,
DE84BBB821C1951E0048A176 /* BridgingHeader.h */,
);
name = SwiftTests;
path = ../Swift/Tests;
Expand Down Expand Up @@ -1399,6 +1407,7 @@
TargetAttributes = {
54C9EDF02040E16300A969CD = {
CreatedOnToolsVersion = 9.2;
LastSwiftMigration = 1010;
ProvisioningStyle = Automatic;
TestTargetID = 6003F589195388D20070C39A;
};
Expand Down Expand Up @@ -1831,8 +1840,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DE84BBB921C1A6ED0048A176 /* FSTHelpers.mm in Sources */,
544A20EE20F6C10C004E52CD /* BasicCompileTests.swift in Sources */,
5495EB032040E90200EBA509 /* CodableGeoPointTests.swift in Sources */,
DE84BBB421C18F060048A176 /* FSTAPIHelpers.mm in Sources */,
DE26C61621C15FCC00DE141A /* CodableDocumentTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2138,15 +2150,21 @@
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
HEADER_SEARCH_PATHS = (
"$(inherited)",
"\"${PODS_ROOT}/../../..\"",
"\"${PODS_ROOT}/../../../Firestore/third_party/abseil-cpp\"",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be required and is likely harmful. We don't want any C++ when compiling Swift.

Same for the Release configuration below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what happens without the abseil include building the swift tests:

In file included from /Users/paulbeusterien/gh/firebase-ios-sdk/Firestore/Example/Tests/API/FSTAPIHelpers.mm:25:
In file included from /Users/paulbeusterien/gh/firebase-ios-sdk/Firestore/Example/Tests/Util/FSTHelpers.h:22:
In file included from /Users/paulbeusterien/gh/firebase-ios-sdk/Firestore/Source/Model/FSTDocument.h:19:
In file included from /Users/paulbeusterien/gh/firebase-ios-sdk/Firestore/core/src/firebase/firestore/model/document_key.h:26:
In file included from /Users/paulbeusterien/gh/firebase-ios-sdk/Firestore/Source/Model/FSTDocumentKey.h:22:
In file included from /Users/paulbeusterien/gh/firebase-ios-sdk/Firestore/core/src/firebase/firestore/model/resource_path.h:24:
In file included from /Users/paulbeusterien/gh/firebase-ios-sdk/Firestore/core/src/firebase/firestore/model/base_path.h:27:
In file included from /Users/paulbeusterien/gh/firebase-ios-sdk/Firestore/core/src/firebase/firestore/util/hard_assert.h:23:
In file included from /Users/paulbeusterien/gh/firebase-ios-sdk/Firestore/core/src/firebase/firestore/util/string_format.h:24:
/Users/paulbeusterien/gh/firebase-ios-sdk/Firestore/core/src/firebase/firestore/util/string_apple.h:29:10: fatal error: 'absl/strings/string_view.h' file not found
#include "absl/strings/string_view.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

);
INFOPLIST_FILE = ../Swift/Tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.google.Firestore-SwiftTests-iOS";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OBJC_BRIDGING_HEADER = ../Swift/Tests/BridgingHeader.h;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Firestore_Example_iOS.app/Firestore_Example_iOS";
};
Expand Down Expand Up @@ -2177,14 +2195,20 @@
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
HEADER_SEARCH_PATHS = (
"$(inherited)",
"\"${PODS_ROOT}/../../..\"",
"\"${PODS_ROOT}/../../../Firestore/third_party/abseil-cpp\"",
);
INFOPLIST_FILE = ../Swift/Tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = "com.google.Firestore-SwiftTests-iOS";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = ../Swift/Tests/BridgingHeader.h;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Firestore_Example_iOS.app/Firestore_Example_iOS";
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
ReferencedContainer = "container:Firestore.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "54269AE8203B9FFC0039F03F"
BuildableName = "Firestore_SwiftTests_iOS.xctest"
BlueprintName = "Firestore_SwiftTests_iOS"
ReferencedContainer = "container:Firestore.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
Expand Down
1 change: 1 addition & 0 deletions Firestore/Example/Tests/API/FIRQueryTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "Firestore/Source/Core/FSTQuery.h"

#import "Firestore/Example/Tests/API/FSTAPIHelpers.h"
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
wilhuff marked this conversation as resolved.
Show resolved Hide resolved

NS_ASSUME_NONNULL_BEGIN

Expand Down
15 changes: 7 additions & 8 deletions Firestore/Example/Tests/API/FSTAPIHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

#import <Foundation/Foundation.h>

#import "Firestore/Example/Tests/Util/FSTHelpers.h"

#include "absl/strings/string_view.h"

@class FIRCollectionReference;
@class FIRDocumentReference;
@class FIRDocumentSnapshot;
@class FIRFirestore;
@class FIRQuerySnapshot;

/** Allow tests to just use an int literal for versions. */
typedef int64_t FSTTestSnapshotVersion;

NS_ASSUME_NONNULL_BEGIN

#if __cplusplus
Expand All @@ -36,17 +35,17 @@ extern "C" {
FIRFirestore *FSTTestFirestore();

/** A convenience method for creating a doc snapshot for tests. */
FIRDocumentSnapshot *FSTTestDocSnapshot(const absl::string_view path,
FIRDocumentSnapshot *FSTTestDocSnapshot(const char *path,
FSTTestSnapshotVersion version,
NSDictionary<NSString *, id> *_Nullable data,
BOOL hasMutations,
BOOL fromCache);

/** A convenience method for creating a collection reference from a path string. */
FIRCollectionReference *FSTTestCollectionRef(const absl::string_view path);
FIRCollectionReference *FSTTestCollectionRef(const char *path);

/** A convenience method for creating a document reference from a path string. */
FIRDocumentReference *FSTTestDocRef(const absl::string_view path);
FIRDocumentReference *FSTTestDocRef(const char *path);

/**
* A convenience method for creating a particular query snapshot for tests.
Expand All @@ -63,7 +62,7 @@ FIRDocumentReference *FSTTestDocRef(const absl::string_view path);
* @returns A query snapshot that consists of both sets of documents.
*/
FIRQuerySnapshot *FSTTestQuerySnapshot(
const absl::string_view path,
const char *path,
NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs,
NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd,
BOOL hasPendingWrites,
Expand Down
9 changes: 5 additions & 4 deletions Firestore/Example/Tests/API/FSTAPIHelpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <string>

#import "Firestore/Example/Tests/Util/FSTHelpers.h"
#import "Firestore/Source/API/FIRCollectionReference+Internal.h"
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
Expand Down Expand Up @@ -59,7 +60,7 @@
return sharedInstance;
}

FIRDocumentSnapshot *FSTTestDocSnapshot(const absl::string_view path,
FIRDocumentSnapshot *FSTTestDocSnapshot(const char *path,
FSTTestSnapshotVersion version,
NSDictionary<NSString *, id> *_Nullable data,
BOOL hasMutations,
Expand All @@ -75,19 +76,19 @@
hasPendingWrites:hasMutations];
}

FIRCollectionReference *FSTTestCollectionRef(const absl::string_view path) {
FIRCollectionReference *FSTTestCollectionRef(const char *path) {
return [FIRCollectionReference referenceWithPath:testutil::Resource(path)
firestore:FSTTestFirestore()];
}

FIRDocumentReference *FSTTestDocRef(const absl::string_view path) {
FIRDocumentReference *FSTTestDocRef(const char *path) {
return [FIRDocumentReference referenceWithPath:testutil::Resource(path)
firestore:FSTTestFirestore()];
}

/** A convenience method for creating a query snapshots for tests. */
FIRQuerySnapshot *FSTTestQuerySnapshot(
const absl::string_view path,
const char *path,
NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs,
NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd,
BOOL hasPendingWrites,
Expand Down
23 changes: 23 additions & 0 deletions Firestore/Swift/Source/Codable/CodableErrors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2018 Google
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
*
* 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.
*/

enum FirestoreDecodingError: Error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need comments if they're going to be public.

We can ignore this for now though.

case decodingIsNotSupported
}

enum FirestoreEncodingError: Error {
case encodingIsNotSupported
}
39 changes: 39 additions & 0 deletions Firestore/Swift/Source/Codable/DocumentReference+Codable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.
*/

import FirebaseFirestore

/**
* 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.
*/
fileprivate protocol CodableDocumentReference: Codable {}

extension CodableDocumentReference {
public init(from decoder: Decoder) throws {
throw FirestoreDecodingError.decodingIsNotSupported
}

public func encode(to encoder: Encoder) throws {
throw FirestoreEncodingError.encodingIsNotSupported
}
}

extension DocumentReference: CodableDocumentReference {}
35 changes: 35 additions & 0 deletions Firestore/Swift/Source/Codable/FieldValue+Codable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.
*/

import FirebaseFirestore

/**
* A protocol describing the encodable properties of a FirebaseFirestore.
*
* Note: this protocol exists as a workaround for the Swift compiler: if the FieldValue 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.
*/
fileprivate protocol CodableFieldValue: Encodable {}
ryanwilson marked this conversation as resolved.
Show resolved Hide resolved

extension CodableFieldValue {
public func encode(to encoder: Encoder) throws {
throw FirestoreEncodingError.encodingIsNotSupported
}
}
paulb777 marked this conversation as resolved.
Show resolved Hide resolved

extension FieldValue: CodableFieldValue {}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import FirebaseFirestore
/**
* 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 protcol 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.
* 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.
*/
fileprivate protocol CodableGeoPoint: Codable {
var latitude: Double { get }
Expand Down
45 changes: 45 additions & 0 deletions Firestore/Swift/Source/Codable/Timestamp+Codable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.
*/

import FirebaseFirestore

/**
* A protocol describing the encodable properties of a DocumentSnapshot.
*
* 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.
*/
public protocol CodableTimestamp: Codable {
init(date: Date)
func dateValue() -> Date
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
}

extension CodableTimestamp {
var date: Date { return dateValue() }
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.init(date: try container.decode(Date.self))
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(dateValue())
}
}

extension Timestamp: CodableTimestamp {}
Loading