Add encoding the geo point and document reference

This commit is contained in:
Oleksii Dykan
2018-01-25 17:18:29 +01:00
parent 7aff81a267
commit f3eed29a7d
4 changed files with 14 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ class _FirebaseEncoder : Encoder {
struct _Options {
let dateEncodingStrategy: FirebaseEncoder.DateEncodingStrategy?
let dataEncodingStrategy: FirebaseEncoder.DataEncodingStrategy?
let skipGeoPointAndReference: Bool
let userInfo: [CodingUserInfoKey : Any]
}
@@ -382,6 +383,11 @@ 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) {
guard let value = value as? NSObject else {
throw DocumentReferenceError.typeIsNotNSObject
}
return value
}
// The value should request a container from the _FirebaseEncoder.

View File

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

View File

@@ -58,6 +58,7 @@ extension GeoPointType {
enum DocumentReferenceError: Error {
case typeIsNotSupported
case typeIsNotNSObject
}
extension DocumentReferenceType {

View File

@@ -26,7 +26,12 @@ open class FirestoreEncoder {
}
internal func encodeToTopLevelContainer<Value : Encodable>(_ value: Value) throws -> Any {
let options = _FirebaseEncoder._Options(dateEncodingStrategy: nil, dataEncodingStrategy: nil, userInfo: userInfo)
let options = _FirebaseEncoder._Options(
dateEncodingStrategy: nil,
dataEncodingStrategy: nil,
skipGeoPointAndReference: false,
userInfo: userInfo
)
let encoder = _FirebaseEncoder(options: options)
guard let topLevel = try encoder.box_(value) else {
throw EncodingError.invalidValue(value,