From f3eed29a7d6bca4e738dd26c970601cedfd5b51f Mon Sep 17 00:00:00 2001 From: Oleksii Dykan Date: Thu, 25 Jan 2018 17:18:29 +0100 Subject: [PATCH] Add encoding the geo point and document reference --- CodableFirebase/Encoder.swift | 6 ++++++ CodableFirebase/FirebaseEncoder.swift | 1 + CodableFirebase/FirestoreDecoder.swift | 1 + CodableFirebase/FirestoreEncoder.swift | 7 ++++++- 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CodableFirebase/Encoder.swift b/CodableFirebase/Encoder.swift index c9f3076..5419094 100644 --- a/CodableFirebase/Encoder.swift +++ b/CodableFirebase/Encoder.swift @@ -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. diff --git a/CodableFirebase/FirebaseEncoder.swift b/CodableFirebase/FirebaseEncoder.swift index d257441..8935075 100644 --- a/CodableFirebase/FirebaseEncoder.swift +++ b/CodableFirebase/FirebaseEncoder.swift @@ -57,6 +57,7 @@ open class FirebaseEncoder { let options = _FirebaseEncoder._Options( dateEncodingStrategy: dateEncodingStrategy, dataEncodingStrategy: dataEncodingStrategy, + skipGeoPointAndReference: true, userInfo: userInfo ) let encoder = _FirebaseEncoder(options: options) diff --git a/CodableFirebase/FirestoreDecoder.swift b/CodableFirebase/FirestoreDecoder.swift index 40ffb7c..62e4e6d 100644 --- a/CodableFirebase/FirestoreDecoder.swift +++ b/CodableFirebase/FirestoreDecoder.swift @@ -58,6 +58,7 @@ extension GeoPointType { enum DocumentReferenceError: Error { case typeIsNotSupported + case typeIsNotNSObject } extension DocumentReferenceType { diff --git a/CodableFirebase/FirestoreEncoder.swift b/CodableFirebase/FirestoreEncoder.swift index c1c576e..1874ce5 100644 --- a/CodableFirebase/FirestoreEncoder.swift +++ b/CodableFirebase/FirestoreEncoder.swift @@ -26,7 +26,12 @@ open class FirestoreEncoder { } internal func encodeToTopLevelContainer(_ 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,