mirror of
https://github.com/caoer/CodableFirebase.git
synced 2026-04-05 16:51:05 +08:00
Add geopointtype and documentreference protocols
This commit is contained in:
@@ -1230,6 +1230,8 @@ 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 || T.self is DocumentReferenceType) {
|
||||
decoded = value as! T
|
||||
} else {
|
||||
self.storage.push(container: value)
|
||||
decoded = try T(from: self)
|
||||
|
||||
@@ -8,6 +8,14 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol GeoPointType: Codable {
|
||||
var latitude: Double { get }
|
||||
var longitude: Double { get }
|
||||
init(latitude: Double, longitude: Double)
|
||||
}
|
||||
|
||||
protocol DocumentReferenceType: Codable {}
|
||||
|
||||
open class FirestoreDecoder {
|
||||
public init() {}
|
||||
|
||||
@@ -28,3 +36,36 @@ open class FirestoreDecoder {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
enum GeoPointKeys: CodingKey {
|
||||
case latitude, longitude
|
||||
}
|
||||
|
||||
extension GeoPointType {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: GeoPointKeys.self)
|
||||
let latitude = try container.decode(Double.self, forKey: .latitude)
|
||||
let longitude = try container.decode(Double.self, forKey: .longitude)
|
||||
self.init(latitude: latitude, longitude: longitude)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: GeoPointKeys.self)
|
||||
try container.encode(latitude, forKey: .latitude)
|
||||
try container.encode(longitude, forKey: .longitude)
|
||||
}
|
||||
}
|
||||
|
||||
enum DocumentReferenceError: Error {
|
||||
case typeIsNotSupported
|
||||
}
|
||||
|
||||
extension DocumentReferenceType {
|
||||
public init(from decoder: Decoder) throws {
|
||||
throw DocumentReferenceError.typeIsNotSupported
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
throw DocumentReferenceError.typeIsNotSupported
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user