mirror of
https://github.com/caoer/CodableFirebase.git
synced 2026-06-14 17:08:56 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f445059fdb | ||
|
|
25867e12c9 | ||
|
|
0d4a779a85 | ||
|
|
c17c0603b2 | ||
|
|
f1d465b936 |
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "CodableFirebase"
|
||||
s.version = "0.0.11"
|
||||
s.version = "0.1.0"
|
||||
s.summary = "Use Codable with Firebase"
|
||||
s.description = "This library helps you use your custom models that conform to Codable protocol with Firebase Realtime Database and Firestore"
|
||||
s.homepage = "https://github.com/alickbass/CodableFirebase"
|
||||
|
||||
@@ -20,6 +20,11 @@ public protocol GeoPointType: FirestoreDecodable, FirestoreEncodable {
|
||||
init(latitude: Double, longitude: Double)
|
||||
}
|
||||
|
||||
public protocol TimestampType: FirestoreDecodable, FirestoreEncodable {
|
||||
init(date: Date)
|
||||
func dateValue() -> Date
|
||||
}
|
||||
|
||||
open class FirestoreDecoder {
|
||||
public init() {}
|
||||
|
||||
@@ -76,3 +81,15 @@ extension FirestoreEncodable {
|
||||
throw DocumentReferenceError.typeIsNotSupported
|
||||
}
|
||||
}
|
||||
|
||||
extension TimestampType {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
self.init(date: try container.decode(Date.self))
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(self.dateValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>0.0.11</string>
|
||||
<string>0.1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||
<key>NSPrincipalClass</key>
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>BNDL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>0.0.11</string>
|
||||
<string>0.1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ Firestore.firestore().collection("data").document("one").getDocument { (document
|
||||
}
|
||||
```
|
||||
|
||||
### How to use `GeoPoint`, `DocumentRefence`, `FieldValue` in Firestore
|
||||
### How to use `GeoPoint`, `DocumentRefence`, `FieldValue`, `Timestamp` in Firestore
|
||||
|
||||
In order to use these 2 types with `Firestore`, you need to add the following code somewhere in your app:
|
||||
|
||||
@@ -94,6 +94,7 @@ In order to use these 2 types with `Firestore`, you need to add the following co
|
||||
extension DocumentReference: DocumentReferenceType {}
|
||||
extension GeoPoint: GeoPointType {}
|
||||
extension FieldValue: FieldValueType {}
|
||||
extension Timestamp: TimestampType {}
|
||||
```
|
||||
|
||||
and now they become `Codable` and can be used properly with `FirestoreEncoder` and `FirestoreDecoder`.
|
||||
|
||||
Reference in New Issue
Block a user