Merge commit '7621e2f8dec938cf48181c8b10afc9b01f444e68' into beta

This commit is contained in:
Ilya Laktyushin
2025-12-06 02:17:48 +04:00
commit 8344b97e03
28070 changed files with 7995182 additions and 0 deletions
@@ -0,0 +1,152 @@
import Foundation
public final class AdaptedPostboxDecoder {
enum ContentType {
case object
case int32Array
case int64Array
case objectArray
case stringArray
case dataArray
case objectDict
}
public final class RawObjectData: Decodable {
public let data: Data
public let typeHash: Int32
public init(data: Data, typeHash: Int32) {
self.data = data
self.typeHash = typeHash
}
public init(from decoder: Decoder) throws {
preconditionFailure()
}
}
public init() {
}
public func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable {
return try self.decode(type, from: data, contentType: .object)
}
func decode<T>(_ type: T.Type, from data: Data, contentType: ContentType) throws -> T where T : Decodable {
if type == AdaptedPostboxDecoder.RawObjectData.self {
if case .object = contentType {
return AdaptedPostboxDecoder.RawObjectData(data: data, typeHash: 0) as! T
} else {
preconditionFailure()
}
}
let decoder = _AdaptedPostboxDecoder(data: data, contentType: contentType)
return try T(from: decoder)
}
}
extension AdaptedPostboxDecoder.ContentType {
init?(valueType: ObjectDataValueType) {
switch valueType {
case .Int32:
return nil
case .Int64:
return nil
case .Bool:
return nil
case .Double:
return nil
case .String:
return nil
case .Object:
self = .object
case .Int32Array:
self = .int32Array
case .Int64Array:
self = .int64Array
case .ObjectArray:
self = .objectArray
case .ObjectDictionary:
self = .objectDict
case .Bytes:
return nil
case .Nil:
return nil
case .StringArray:
self = .stringArray
case .BytesArray:
self = .dataArray
}
}
}
final class _AdaptedPostboxDecoder {
var codingPath: [CodingKey] = []
var userInfo: [CodingUserInfoKey : Any] = [:]
var container: AdaptedPostboxDecodingContainer?
fileprivate let data: Data
fileprivate let contentType: AdaptedPostboxDecoder.ContentType
init(data: Data, contentType: AdaptedPostboxDecoder.ContentType) {
self.data = data
self.contentType = contentType
}
}
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 {
assertCanCreateContainer()
let decoder = PostboxDecoder(buffer: MemoryBuffer(data: self.data))
var content: UnkeyedContainer.Content?
switch self.contentType {
case .object:
preconditionFailure()
case .int32Array:
content = .int32Array(decoder.decodeInt32ArrayRaw())
case .int64Array:
content = .int64Array(decoder.decodeInt64ArrayRaw())
case .objectArray:
content = .objectArray(decoder.decodeObjectDataArrayRaw())
case .stringArray:
content = .stringArray(decoder.decodeStringArrayRaw())
case .dataArray:
content = .dataArray(decoder.decodeBytesArrayRaw().map { $0.makeData() })
case .objectDict:
content = .objectDict(decoder.decodeObjectDataDictRaw())
}
if let content = content {
let container = UnkeyedContainer(data: self.data, codingPath: self.codingPath, userInfo: self.userInfo, content: content)
self.container = container
return container
} else {
preconditionFailure()
}
}
func singleValueContainer() -> SingleValueDecodingContainer {
preconditionFailure()
}
}
protocol AdaptedPostboxDecodingContainer: AnyObject {
}
@@ -0,0 +1,134 @@
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))
}
}
}
private func decodingErrorBreakpoint() {
#if DEBUG
print("Decoding error. Install a breakpoint at decodingErrorBreakpoint to debug.")
#endif
}
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 {
if let (data, valueType) = self.decoder.decodeObjectDataForKey(key.stringValue) {
if type == AdaptedPostboxDecoder.RawObjectData.self {
if case let .Object(typeHash) = valueType {
return AdaptedPostboxDecoder.RawObjectData(data: data, typeHash: typeHash) as! T
} else {
decodingErrorBreakpoint()
throw DecodingError.typeMismatch(T.self, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
if let mappedType = AdaptedPostboxDecoder.ContentType(valueType: valueType) {
return try AdaptedPostboxDecoder().decode(T.self, from: data, contentType: mappedType)
} else {
switch valueType {
case .Bytes:
guard let resultData = PostboxDecoder.parseDataRaw(data: data) else {
decodingErrorBreakpoint()
throw DecodingError.typeMismatch(T.self, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
if let resultData = resultData as? T {
return resultData
} else {
decodingErrorBreakpoint()
throw DecodingError.typeMismatch(T.self, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
default:
decodingErrorBreakpoint()
throw DecodingError.typeMismatch(T.self, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 {
if let value = self.decoder.decodeOptionalInt32ForKey(key.stringValue) {
return value
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 {
if let value = self.decoder.decodeOptionalInt64ForKey(key.stringValue) {
return value
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {
if let value = self.decoder.decodeOptionalBoolForKey(key.stringValue) {
return value
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: String.Type, forKey key: Key) throws -> String {
if let value = self.decoder.decodeOptionalStringForKey(key.stringValue) {
return value
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: Double.Type, forKey key: Key) throws -> Double {
if let value = self.decoder.decodeOptionalDoubleForKey(key.stringValue) {
return value
} else {
decodingErrorBreakpoint()
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 {}
@@ -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 {}
@@ -0,0 +1,161 @@
import Foundation
extension _AdaptedPostboxDecoder {
final class UnkeyedContainer {
enum Content {
case int32Array([Int32])
case int64Array([Int64])
case objectArray([Data])
case stringArray([String])
case dataArray([Data])
case objectDict([(Data, Data)])
var count: Int {
switch self {
case let .int32Array(array):
return array.count
case let .int64Array(array):
return array.count
case let .objectArray(array):
return array.count
case let .stringArray(array):
return array.count
case let .dataArray(array):
return array.count
case let .objectDict(dict):
return dict.count * 2
}
}
}
let codingPath: [CodingKey]
let userInfo: [CodingUserInfoKey: Any]
let content: Content
var count: Int? {
return self.content.count
}
var isAtEnd: Bool {
return self.currentIndex >= self.content.count
}
fileprivate var _currentIndex: Int = 0
var currentIndex: Int {
return self._currentIndex
}
init(data: Data, codingPath: [CodingKey], userInfo: [CodingUserInfoKey: Any], content: Content) {
self.codingPath = codingPath
self.userInfo = userInfo
self.content = content
}
}
}
extension _AdaptedPostboxDecoder.UnkeyedContainer: UnkeyedDecodingContainer {
func decodeNil() throws -> Bool {
preconditionFailure()
}
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
if type == Data.self {
switch self.content {
case let .dataArray(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index] as! T
default:
assertionFailure()
throw DecodingError.typeMismatch(Data.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
} else {
switch self.content {
case let .objectArray(array):
let index = self._currentIndex
self._currentIndex += 1
let data = array[index]
return try AdaptedPostboxDecoder().decode(T.self, from: data)
case let .objectDict(dict):
let index = self._currentIndex
self._currentIndex += 1
let dataPair = dict[index / 2]
let data: Data
if index % 2 == 0 {
data = dataPair.0
} else {
data = dataPair.1
}
return try AdaptedPostboxDecoder().decode(T.self, from: data)
case let .int32Array(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index] as! T
case let .int64Array(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index] as! T
case let .stringArray(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index] as! T
default:
assertionFailure()
throw DecodingError.typeMismatch(T.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
}
}
func decode(_ type: Int32.Type) throws -> Int32 {
switch self.content {
case let .int32Array(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index]
default:
throw DecodingError.typeMismatch(Int32.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
}
func decode(_ type: Int64.Type) throws -> Int64 {
switch self.content {
case let .int64Array(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index]
default:
throw DecodingError.typeMismatch(Int64.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
}
func decode(_ type: String.Type) throws -> String {
switch self.content {
case let .stringArray(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index]
default:
throw DecodingError.typeMismatch(String.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
}
func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
preconditionFailure()
}
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
preconditionFailure()
}
func superDecoder() throws -> Decoder {
preconditionFailure()
}
}
extension _AdaptedPostboxDecoder.UnkeyedContainer: AdaptedPostboxDecodingContainer {}
@@ -0,0 +1,100 @@
import Foundation
import MurMurHash32
public class AdaptedPostboxEncoder {
public final class RawObjectData: Encodable {
public let typeHash: Int32
public let data: Data
public init(typeHash: Int32, data: Data) {
self.typeHash = typeHash
self.data = data
}
public func encode(to encoder: Encoder) throws {
preconditionFailure()
}
}
public init() {
}
public func encode(_ value: Encodable) throws -> Data {
let typeHash: Int32 = murMurHashString32("\(type(of: value))")
let encoder = _AdaptedPostboxEncoder(typeHash: typeHash)
try value.encode(to: encoder)
return encoder.makeData(addHeader: false, isDictionary: false).0
}
}
final class _AdaptedPostboxEncoder {
var codingPath: [CodingKey] = []
var userInfo: [CodingUserInfoKey : Any] = [:]
let typeHash: Int32
fileprivate var container: AdaptedPostboxEncodingContainer?
init(typeHash: Int32) {
self.typeHash = typeHash
}
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType) {
if let container = self.container {
return container.makeData(addHeader: addHeader, isDictionary: isDictionary)
} else {
let buffer = WriteBuffer()
if addHeader {
var typeHash: Int32 = self.typeHash
buffer.write(&typeHash, offset: 0, length: 4)
}
let innerEncoder = PostboxEncoder()
let data = innerEncoder.makeData()
if addHeader {
var length: Int32 = Int32(data.count)
buffer.write(&length, offset: 0, length: 4)
}
buffer.write(data)
return (buffer.makeData(), .Object)
}
}
}
extension _AdaptedPostboxEncoder: Encoder {
fileprivate func assertCanCreateContainer() {
precondition(self.container == nil)
}
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey {
assertCanCreateContainer()
let container = KeyedContainer<Key>(codingPath: self.codingPath, userInfo: self.userInfo, typeHash: self.typeHash)
self.container = container
return KeyedEncodingContainer(container)
}
func unkeyedContainer() -> UnkeyedEncodingContainer {
assertCanCreateContainer()
let container = UnkeyedContainer(codingPath: self.codingPath, userInfo: self.userInfo)
self.container = container
return container
}
func singleValueContainer() -> SingleValueEncodingContainer {
preconditionFailure()
}
}
protocol AdaptedPostboxEncodingContainer: AnyObject {
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType)
}
@@ -0,0 +1,113 @@
import Foundation
import MurMurHash32
extension _AdaptedPostboxEncoder {
final class KeyedContainer<Key> where Key: CodingKey {
var codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any]
let typeHash: Int32
let encoder: PostboxEncoder
init(codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any], typeHash: Int32) {
self.codingPath = codingPath
self.userInfo = userInfo
self.typeHash = typeHash
self.encoder = PostboxEncoder()
}
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType) {
let buffer = WriteBuffer()
if addHeader {
var typeHash: Int32 = self.typeHash
buffer.write(&typeHash, offset: 0, length: 4)
}
let data = self.encoder.makeData()
if addHeader {
var length: Int32 = Int32(data.count)
buffer.write(&length, offset: 0, length: 4)
}
buffer.write(data)
return (buffer.makeData(), .Object)
}
}
}
extension _AdaptedPostboxEncoder.KeyedContainer: KeyedEncodingContainerProtocol {
func encode<T>(_ value: T, forKey key: Key) throws where T : Encodable {
if let value = value as? Data {
self.encoder.encodeData(value, forKey: key.stringValue)
} else if let value = value as? AdaptedPostboxEncoder.RawObjectData {
self.encoder.encodeInnerObjectDataWithHeader(typeHash: value.typeHash, data: value.data, valueType: .Object, forKey: key.stringValue)
} else {
let typeHash: Int32 = murMurHashString32("\(type(of: value))")
let innerEncoder = _AdaptedPostboxEncoder(typeHash: typeHash)
try! value.encode(to: innerEncoder)
let typeOfValue = type(of: value)
let typeString = "\(typeOfValue)"
var isDictionary = false
if typeString.hasPrefix("Dictionary<") {
isDictionary = true
}
let (data, valueType) = innerEncoder.makeData(addHeader: true, isDictionary: isDictionary)
self.encoder.encodeInnerObjectData(data, valueType: valueType, forKey: key.stringValue)
}
}
func encodeNil(forKey key: Key) throws {
self.encoder.encodeNil(forKey: key.stringValue)
}
func encode(_ value: Int32, forKey key: Key) throws {
self.encoder.encodeInt32(value, forKey: key.stringValue)
}
func encode(_ value: Int64, forKey key: Key) throws {
self.encoder.encodeInt64(value, forKey: key.stringValue)
}
func encode(_ value: Int, forKey key: Key) throws {
assertionFailure()
self.encoder.encodeInt32(Int32(value), forKey: key.stringValue)
}
func encode(_ value: Bool, forKey key: Key) throws {
self.encoder.encodeBool(value, forKey: key.stringValue)
}
func encode(_ value: Double, forKey key: Key) throws {
self.encoder.encodeDouble(value, forKey: key.stringValue)
}
func encode(_ value: String, forKey key: Key) throws {
self.encoder.encodeString(value, forKey: key.stringValue)
}
func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
preconditionFailure()
}
func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
preconditionFailure()
}
func superEncoder() -> Encoder {
preconditionFailure()
}
func superEncoder(forKey key: Key) -> Encoder {
preconditionFailure()
}
}
extension _AdaptedPostboxEncoder.KeyedContainer: AdaptedPostboxEncodingContainer {}
@@ -0,0 +1,85 @@
import Foundation
extension _AdaptedPostboxEncoder {
final class SingleValueContainer {
var codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any]
init(codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any]) {
self.codingPath = codingPath
self.userInfo = userInfo
}
}
}
extension _AdaptedPostboxEncoder.SingleValueContainer: SingleValueEncodingContainer {
func encodeNil() throws {
preconditionFailure()
}
func encode(_ value: Bool) throws {
preconditionFailure()
}
func encode(_ value: String) throws {
preconditionFailure()
}
func encode(_ value: Double) throws {
preconditionFailure()
}
func encode(_ value: Float) throws {
preconditionFailure()
}
func encode(_ value: Int) throws {
preconditionFailure()
}
func encode(_ value: Int8) throws {
preconditionFailure()
}
func encode(_ value: Int16) throws {
preconditionFailure()
}
func encode(_ value: Int32) throws {
preconditionFailure()
}
func encode(_ value: Int64) throws {
preconditionFailure()
}
func encode(_ value: UInt) throws {
preconditionFailure()
}
func encode(_ value: UInt8) throws {
preconditionFailure()
}
func encode(_ value: UInt16) throws {
preconditionFailure()
}
func encode(_ value: UInt32) throws {
preconditionFailure()
}
func encode(_ value: UInt64) throws {
preconditionFailure()
}
func encode<T>(_ value: T) throws where T : Encodable {
preconditionFailure()
}
}
extension _AdaptedPostboxEncoder.SingleValueContainer: AdaptedPostboxEncodingContainer {
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType) {
preconditionFailure()
}
}
@@ -0,0 +1,186 @@
import Foundation
import MurMurHash32
extension _AdaptedPostboxEncoder {
final class UnkeyedContainer {
fileprivate enum Item {
case int32(Int32)
case int64(Int64)
case string(String)
case object(Data)
case data(Data)
}
let codingPath: [CodingKey]
let userInfo: [CodingUserInfoKey: Any]
fileprivate var items: [Item] = []
var count: Int {
return self.items.count
}
init(codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any]) {
self.codingPath = codingPath
self.userInfo = userInfo
}
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType) {
precondition(addHeader)
if self.items.isEmpty {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
buffer.write(&length, offset: 0, length: 4)
return (buffer.makeData(), .Int32Array)
} else if self.items.allSatisfy({ if case .int32 = $0 { return true } else { return false } }) {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
buffer.write(&length, offset: 0, length: 4)
for case .int32(var value) in self.items {
buffer.write(&value, offset: 0, length: 4)
}
return (buffer.makeData(), .Int32Array)
} else if self.items.allSatisfy({ if case .int64 = $0 { return true } else { return false } }) {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
buffer.write(&length, offset: 0, length: 4)
for case .int64(var value) in self.items {
buffer.write(&value, offset: 0, length: 8)
}
return (buffer.makeData(), .Int64Array)
} else if self.items.allSatisfy({ if case .string = $0 { return true } else { return false } }) {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
buffer.write(&length, offset: 0, length: 4)
for case .string(let value) in self.items {
let data = value.data(using: .utf8, allowLossyConversion: true) ?? (String("").data(using: .utf8)!)
var valueLength: Int32 = Int32(data.count)
buffer.write(&valueLength, offset: 0, length: 4)
buffer.write(data)
}
return (buffer.makeData(), .StringArray)
} else if self.items.allSatisfy({ if case .object = $0 { return true } else { return false } }) {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
if isDictionary {
precondition(length % 2 == 0)
length /= 2
}
buffer.write(&length, offset: 0, length: 4)
for case .object(let data) in self.items {
buffer.write(data)
}
return (buffer.makeData(), isDictionary ? .ObjectDictionary : .ObjectArray)
} else if self.items.allSatisfy({ if case .data = $0 { return true } else { return false } }) {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
buffer.write(&length, offset: 0, length: 4)
for case .data(let data) in self.items {
var valueLength: Int32 = Int32(data.count)
buffer.write(&valueLength, offset: 0, length: 4)
buffer.write(data)
}
return (buffer.makeData(), .BytesArray)
} else {
preconditionFailure()
}
}
}
}
extension _AdaptedPostboxEncoder.UnkeyedContainer: UnkeyedEncodingContainer {
func encodeNil() throws {
preconditionFailure()
}
func encode<T>(_ value: T) throws where T : Encodable {
if value is Int32 {
try self.encode(value as! Int32)
} else if value is Int64 {
try self.encode(value as! Int64)
} else if value is String {
try self.encode(value as! String)
} else if value is Data {
try self.encodeData(value as! Data)
} else if let value = value as? AdaptedPostboxEncoder.RawObjectData {
let buffer = WriteBuffer()
var typeHash: Int32 = value.typeHash
buffer.write(&typeHash, offset: 0, length: 4)
var length: Int32 = Int32(value.data.count)
buffer.write(&length, offset: 0, length: 4)
buffer.write(value.data)
self.items.append(.object(buffer.makeData()))
} else {
let typeHash: Int32 = murMurHashString32("\(type(of: value))")
let innerEncoder = _AdaptedPostboxEncoder(typeHash: typeHash)
try! value.encode(to: innerEncoder)
let (data, _) = innerEncoder.makeData(addHeader: true, isDictionary: false)
let buffer = WriteBuffer()
buffer.write(data)
self.items.append(.object(buffer.makeData()))
}
}
func encode(_ value: Int32) throws {
self.items.append(.int32(value))
}
func encode(_ value: Int64) throws {
self.items.append(.int64(value))
}
func encode(_ value: String) throws {
self.items.append(.string(value))
}
func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
preconditionFailure()
}
func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
preconditionFailure()
}
func superEncoder() -> Encoder {
preconditionFailure()
}
}
private extension _AdaptedPostboxEncoder.UnkeyedContainer {
func encodeData(_ value: Data) throws {
self.items.append(.data(value))
}
}
extension _AdaptedPostboxEncoder.UnkeyedContainer: AdaptedPostboxEncodingContainer {
func makeData() -> Data {
preconditionFailure()
}
}
@@ -0,0 +1 @@