13 Commits
0.0.4 ... 0.0.7

Author SHA1 Message Date
Oleksii Dykan
3cae6e90d9 Bump version number 2018-02-20 23:13:02 +01:00
Oleksii Dykan
222b080693 Merge pull request #24 from alickbass/update-encoder
Update Encoder
2018-02-20 23:11:31 +01:00
Oleksii Dykan
5b8627dbbe If the value pushed a container before throwing, pop it back off to restore state 2018-02-20 23:00:29 +01:00
Oleksii Dykan
1384814716 Bump version number 2018-02-11 11:44:17 +01:00
Oleksii Dykan
b0e86866a0 Merge pull request #21 from alickbass/fix-decimal-encoding
Fix handling decimal value
2018-02-11 11:41:33 +01:00
Oleksii Dykan
0c60f70d91 Fix handling decimal value 2018-02-11 11:29:42 +01:00
Oleksii Dykan
df9dfe1dda Fix cocoapods link 2018-01-30 10:56:48 +01:00
Oleksii Dykan
2afa8c101c Bump version number 2018-01-29 11:59:22 +01:00
Oleksii Dykan
ec22c1649e Merge pull request #17 from alickbass/field-type-support
Field type support
2018-01-29 11:52:38 +01:00
Oleksii Dykan
8279c70901 Update README 2018-01-29 11:32:46 +01:00
Oleksii Dykan
c4ed0ec8eb Refactor to FirestoreDecodable and FirestoreEncodable protocols 2018-01-29 11:25:25 +01:00
Oleksii Dykan
fcfb7b8b1e Fix setting option for skipping values 2018-01-29 11:19:33 +01:00
Oleksii Dykan
747d540d77 Rename bool option to skip firestore types 2018-01-29 11:18:27 +01:00
10 changed files with 45 additions and 20 deletions

View File

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

@@ -13,7 +13,7 @@ class _FirebaseDecoder : Decoder {
struct _Options {
let dateDecodingStrategy: FirebaseDecoder.DateDecodingStrategy?
let dataDecodingStrategy: FirebaseDecoder.DataDecodingStrategy?
let skipGeoPointAndReference: Bool
let skipFirestoreTypes: Bool
let userInfo: [CodingUserInfoKey : Any]
}
@@ -1230,7 +1230,7 @@ extension _FirebaseDecoder {
} else if T.self == Decimal.self || T.self == NSDecimalNumber.self {
guard let decimal = try self.unbox(value, as: Decimal.self) else { return nil }
decoded = decimal as! T
} else if options.skipGeoPointAndReference && (T.self is GeoPointType.Type || T.self is DocumentReferenceType.Type) {
} else if options.skipFirestoreTypes && (T.self is FirestoreDecodable.Type) {
decoded = value as! T
} else {
self.storage.push(container: value)

View File

@@ -13,7 +13,7 @@ class _FirebaseEncoder : Encoder {
struct _Options {
let dateEncodingStrategy: FirebaseEncoder.DateEncodingStrategy?
let dataEncodingStrategy: FirebaseEncoder.DataEncodingStrategy?
let skipGeoPointAndReference: Bool
let skipFirestoreTypes: Bool
let userInfo: [CodingUserInfoKey : Any]
}
@@ -383,7 +383,9 @@ extension _FirebaseEncoder {
return try self.box((value as! Data))
} else if T.self == URL.self || T.self == NSURL.self {
return self.box((value as! URL).absoluteString)
} else if options.skipGeoPointAndReference && (value is GeoPointType || value is DocumentReferenceType) {
} else if T.self == Decimal.self || T.self == NSDecimalNumber.self {
return (value as! NSDecimalNumber)
} else if options.skipFirestoreTypes && (value is FirestoreEncodable) {
guard let value = value as? NSObject else {
throw DocumentReferenceError.typeIsNotNSObject
}
@@ -391,11 +393,20 @@ extension _FirebaseEncoder {
}
// The value should request a container from the _FirebaseEncoder.
let depth = storage.count
try value.encode(to: self)
let depth = self.storage.count
do {
try value.encode(to: self)
} catch {
// If the value pushed a container before throwing, pop it back off to restore state.
if self.storage.count > depth {
let _ = self.storage.popContainer()
}
throw error
}
// The top container should be a new container.
guard storage.count > depth else {
guard self.storage.count > depth else {
return nil
}

View File

@@ -53,7 +53,7 @@ open class FirebaseDecoder {
let options = _FirebaseDecoder._Options(
dateDecodingStrategy: dateDecodingStrategy,
dataDecodingStrategy: dataDecodingStrategy,
skipGeoPointAndReference: false,
skipFirestoreTypes: false,
userInfo: userInfo
)
let decoder = _FirebaseDecoder(referencing: container, options: options)

View File

@@ -57,7 +57,7 @@ open class FirebaseEncoder {
let options = _FirebaseEncoder._Options(
dateEncodingStrategy: dateEncodingStrategy,
dataEncodingStrategy: dataEncodingStrategy,
skipGeoPointAndReference: false,
skipFirestoreTypes: false,
userInfo: userInfo
)
let encoder = _FirebaseEncoder(options: options)

View File

@@ -8,14 +8,18 @@
import Foundation
public protocol GeoPointType: Codable {
public protocol FirestoreDecodable: Decodable {}
public protocol FirestoreEncodable: Encodable {}
public typealias DocumentReferenceType = FirestoreDecodable & FirestoreEncodable
public typealias FieldValueType = FirestoreEncodable
public protocol GeoPointType: FirestoreDecodable, FirestoreEncodable {
var latitude: Double { get }
var longitude: Double { get }
init(latitude: Double, longitude: Double)
}
public protocol DocumentReferenceType: Codable {}
open class FirestoreDecoder {
public init() {}
@@ -25,7 +29,7 @@ open class FirestoreDecoder {
let options = _FirebaseDecoder._Options(
dateDecodingStrategy: nil,
dataDecodingStrategy: nil,
skipGeoPointAndReference: true,
skipFirestoreTypes: true,
userInfo: userInfo
)
let decoder = _FirebaseDecoder(referencing: container, options: options)
@@ -61,11 +65,13 @@ enum DocumentReferenceError: Error {
case typeIsNotNSObject
}
extension DocumentReferenceType {
extension FirestoreDecodable {
public init(from decoder: Decoder) throws {
throw DocumentReferenceError.typeIsNotSupported
}
}
extension FirestoreEncodable {
public func encode(to encoder: Encoder) throws {
throw DocumentReferenceError.typeIsNotSupported
}

View File

@@ -29,7 +29,7 @@ open class FirestoreEncoder {
let options = _FirebaseEncoder._Options(
dateEncodingStrategy: nil,
dataEncodingStrategy: nil,
skipGeoPointAndReference: true,
skipFirestoreTypes: true,
userInfo: userInfo
)
let encoder = _FirebaseEncoder(options: options)

View File

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

View File

@@ -109,6 +109,11 @@ class TestCodableFirestore: XCTestCase {
_testRoundTrip(of: TopLevelWrapper(date), expected: ["value": date])
}
func testDecimalValue() {
let value = Decimal(2)
_testRoundTrip(of: TopLevelWrapper(value), expected: ["value": value])
}
// MARK: - GeoPoint & Document Reference
func testEncodingGeoPoint() {
let point = GeoPoint(latitude: 2, longitude: 2)

View File

@@ -1,7 +1,7 @@
# CodableFirebase
Use [Codable](https://developer.apple.com/documentation/swift/codable) with [Firebase](https://firebase.google.com)
[![CocoaPods](https://img.shields.io/cocoapods/p/CodableFirebase.svg)]()
[![CocoaPods](https://img.shields.io/cocoapods/p/CodableFirebase.svg)](https://github.com/alickbass/CodableFirebase)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Build Status](https://travis-ci.org/alickbass/CodableFirebase.svg?branch=master)](https://travis-ci.org/alickbass/CodableFirebase)
@@ -86,17 +86,20 @@ Firestore.firestore().collection("data").document("one").getDocument { (document
}
```
### How to use `GeoPoint` and `DocumentRefence` in Firestore
### How to use `GeoPoint`, `DocumentRefence`, `FieldValue` in Firestore
In order to use these 2 types with `Firestore`, you need to add the following code somewhere in your app:
```swift
extension DocumentReference: DocumentReferenceType {}
extension GeoPoint: GeoPointType {}
extension FieldValue: FieldValueType {}
```
and now they become `Codable` and can be used properly with `FirestoreEncoder` and `FirestoreDecoder`.
***PLEASE NOTE*** that as `FieldValue` is only used to [`setData()` and `updateData()`](https://firebase.google.com/docs/reference/swift/firebasefirestore/api/reference/Classes/FieldValue), it only adopts the `Encodable` protocol.
## Integration
### CocoaPods (iOS 9+)