mirror of
https://github.com/caoer/CodableFirebase.git
synced 2026-04-24 03:45:51 +08:00
Add support for FirTimestamp
This commit is contained in:
@@ -95,6 +95,11 @@ class TestCodableFirebase: XCTestCase {
|
||||
expectedValue: expected,
|
||||
dateEncodingStrategy: .secondsSince1970,
|
||||
dateDecodingStrategy: .secondsSince1970)
|
||||
|
||||
_testRoundTrip(of: TopLevelWrapper(FirTimestamp(date: Date(timeIntervalSince1970: seconds))),
|
||||
expectedValue: expected,
|
||||
dateEncodingStrategy: .secondsSince1970,
|
||||
dateDecodingStrategy: .secondsSince1970)
|
||||
|
||||
_testRoundTrip(of: OptionalTopLevelWrapper(Date(timeIntervalSince1970: seconds)),
|
||||
expectedValue: expected,
|
||||
@@ -500,6 +505,22 @@ fileprivate struct Timestamp : Codable, Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate struct FirTimestamp : TimestampType, Equatable {
|
||||
let date: Date
|
||||
|
||||
init(date: Date) {
|
||||
self.date = date
|
||||
}
|
||||
|
||||
func dateValue() -> Date {
|
||||
return date
|
||||
}
|
||||
|
||||
static func == (_ lhs: FirTimestamp, _ rhs: FirTimestamp) -> Bool {
|
||||
return lhs.date == rhs.date
|
||||
}
|
||||
}
|
||||
|
||||
/// A simple referential counter type that encodes as a single Int value.
|
||||
fileprivate final class Counter : Codable, Equatable {
|
||||
var count: Int = 0
|
||||
|
||||
@@ -128,7 +128,14 @@ class TestCodableFirestore: XCTestCase {
|
||||
XCTAssertEqual((try? FirestoreEncoder().encode(val)) as NSDictionary?, ["value": val.value])
|
||||
XCTAssertEqual(try? FirestoreDecoder().decode(TopLevelWrapper<DocumentReference>.self, from: ["value": val.value]), val)
|
||||
}
|
||||
|
||||
|
||||
func testEncodingTimestamp() {
|
||||
let timestamp = Timestamp(date: Date())
|
||||
let wrapper = TopLevelWrapper(timestamp)
|
||||
XCTAssertEqual((try? FirestoreEncoder().encode(wrapper)) as NSDictionary?, ["value": timestamp])
|
||||
XCTAssertEqual(try? FirestoreDecoder().decode(TopLevelWrapper<Timestamp>.self, from: ["value": timestamp]), wrapper)
|
||||
}
|
||||
|
||||
private func _testEncodeFailure<T : Encodable>(of value: T) {
|
||||
do {
|
||||
let _ = try FirestoreEncoder().encode(value)
|
||||
@@ -195,10 +202,29 @@ fileprivate class GeoPoint: NSObject, GeoPointType {
|
||||
self.longitude = longitude
|
||||
}
|
||||
|
||||
static func == (lhs: Point, rhs: Point) -> Bool {
|
||||
return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
|
||||
override func isEqual(_ object: Any?) -> Bool {
|
||||
guard let other = object.flatMap({ $0 as? GeoPoint }) else { return false }
|
||||
return latitude == other.latitude && longitude == other.longitude
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - ReferenceType
|
||||
fileprivate class DocumentReference: NSObject, DocumentReferenceType {}
|
||||
|
||||
// MARK: - Timestamp
|
||||
fileprivate class Timestamp: NSObject, TimestampType {
|
||||
let date: Date
|
||||
|
||||
required init(date: Date) {
|
||||
self.date = date
|
||||
}
|
||||
|
||||
func dateValue() -> Date {
|
||||
return date
|
||||
}
|
||||
|
||||
override func isEqual(_ object: Any?) -> Bool {
|
||||
guard let other = object.flatMap({ $0 as? Timestamp }) else { return false }
|
||||
return date == other.date
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user