4 Commits

Author SHA1 Message Date
Oleksii Dykan
f445059fdb 0.1.0 release 2018-05-19 22:22:49 +02:00
Oleksii Dykan
25867e12c9 Merge pull request #43 from alickbass/support-firtimestamp
Add support for FirTimestamp
2018-05-19 21:19:19 +01:00
Oleksii Dykan
0d4a779a85 Update README 2018-05-19 20:01:28 +02:00
Oleksii Dykan
c17c0603b2 Add support for FirTimestamp 2018-05-19 19:50:58 +02:00
7 changed files with 72 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "CodableFirebase"
s.version = "0.0.12"
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"

View File

@@ -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())
}
}

View File

@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.0.12</string>
<string>0.1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>

View File

@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>0.0.12</string>
<string>0.1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>

View File

@@ -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

View File

@@ -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
}
}

View File

@@ -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`.