This commit is contained in:
Ali
2021-07-29 21:07:15 +02:00
parent d46c7b739d
commit e874fa92ff
15 changed files with 869 additions and 67 deletions

View File

@@ -0,0 +1,47 @@
import Foundation
final public class AdaptedPostboxDecoder {
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable {
let decoder = _AdaptedPostboxDecoder(data: data)
return try T(from: decoder)
}
}
final class _AdaptedPostboxDecoder {
var codingPath: [CodingKey] = []
var userInfo: [CodingUserInfoKey : Any] = [:]
var container: AdaptedPostboxDecodingContainer?
fileprivate var data: Data
init(data: Data) {
self.data = data
}
}
extension _AdaptedPostboxDecoder: Decoder {
fileprivate func assertCanCreateContainer() {
precondition(self.container == nil)
}
func container<Key>(keyedBy type: Key.Type) -> KeyedDecodingContainer<Key> where Key : CodingKey {
assertCanCreateContainer()
let container = KeyedContainer<Key>(data: self.data, codingPath: self.codingPath, userInfo: self.userInfo)
self.container = container
return KeyedDecodingContainer(container)
}
func unkeyedContainer() -> UnkeyedDecodingContainer {
preconditionFailure()
}
func singleValueContainer() -> SingleValueDecodingContainer {
preconditionFailure()
}
}
protocol AdaptedPostboxDecodingContainer: AnyObject {
}

View File

@@ -0,0 +1,83 @@
import Foundation
extension _AdaptedPostboxDecoder {
final class KeyedContainer<Key> where Key: CodingKey {
var codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any]
let decoder: PostboxDecoder
init(data: Data, codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any]) {
self.codingPath = codingPath
self.userInfo = userInfo
self.decoder = PostboxDecoder(buffer: MemoryBuffer(data: data))
}
}
}
extension _AdaptedPostboxDecoder.KeyedContainer: KeyedDecodingContainerProtocol {
var allKeys: [Key] {
preconditionFailure()
}
func contains(_ key: Key) -> Bool {
return self.decoder.containsKey(key.stringValue)
}
func decodeNil(forKey key: Key) throws -> Bool {
return self.decoder.decodeNilForKey(key.stringValue)
}
func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T : Decodable {
preconditionFailure()
}
func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 {
if let value = self.decoder.decodeOptionalInt32ForKey(key.stringValue) {
return value
} else {
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: Int64.Type, forKey key: Key) throws -> Int32 {
if let value = self.decoder.decodeOptionalInt64ForKey(key.stringValue) {
return value
} else {
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: Bool.Type, forKey key: Key) throws -> Int32 {
if let value = self.decoder.decodeOptionalBoolForKey(key.stringValue) {
return value
} else {
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: String.Type, forKey key: Key) throws -> Int32 {
if let value = self.decoder.decodeOptionalStringForKey(key.stringValue) {
return value
} else {
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer {
preconditionFailure()
}
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
preconditionFailure()
}
func superDecoder() throws -> Decoder {
preconditionFailure()
}
func superDecoder(forKey key: Key) throws -> Decoder {
preconditionFailure()
}
}
extension _AdaptedPostboxDecoder.KeyedContainer: AdaptedPostboxDecodingContainer {}

View File

@@ -0,0 +1,82 @@
import Foundation
extension _AdaptedPostboxDecoder {
final class SingleValueContainer {
var codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any]
init(codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any]) {
self.codingPath = codingPath
self.userInfo = userInfo
}
}
}
extension _AdaptedPostboxDecoder.SingleValueContainer: SingleValueDecodingContainer {
func decodeNil() -> Bool {
preconditionFailure()
}
func decode(_ type: Bool.Type) throws -> Bool {
preconditionFailure()
}
func decode(_ type: String.Type) throws -> String {
preconditionFailure()
}
func decode(_ type: Double.Type) throws -> Double {
preconditionFailure()
}
func decode(_ type: Float.Type) throws -> Float {
preconditionFailure()
}
func decode(_ type: Int.Type) throws -> Int {
preconditionFailure()
}
func decode(_ type: Int8.Type) throws -> Int8 {
preconditionFailure()
}
func decode(_ type: Int16.Type) throws -> Int16 {
preconditionFailure()
}
func decode(_ type: Int32.Type) throws -> Int32 {
preconditionFailure()
}
func decode(_ type: Int64.Type) throws -> Int64 {
preconditionFailure()
}
func decode(_ type: UInt.Type) throws -> UInt {
preconditionFailure()
}
func decode(_ type: UInt8.Type) throws -> UInt8 {
preconditionFailure()
}
func decode(_ type: UInt16.Type) throws -> UInt16 {
preconditionFailure()
}
func decode(_ type: UInt32.Type) throws -> UInt32 {
preconditionFailure()
}
func decode(_ type: UInt64.Type) throws -> UInt64 {
preconditionFailure()
}
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
preconditionFailure()
}
}
extension _AdaptedPostboxDecoder.SingleValueContainer: AdaptedPostboxDecodingContainer {}

View File

@@ -0,0 +1,15 @@
import Foundation
extension _AdaptedPostboxDecoder {
final class UnkeyedContainer {
var codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any]
init(data: Data, codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any]) {
self.codingPath = codingPath
self.userInfo = userInfo
}
}
}