mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
85 lines
2.6 KiB
Swift
85 lines
2.6 KiB
Swift
import Foundation
|
|
|
|
extension _AdaptedPostboxEncoder {
|
|
final class KeyedContainer<Key> where Key: CodingKey {
|
|
var codingPath: [CodingKey]
|
|
var userInfo: [CodingUserInfoKey: Any]
|
|
|
|
let encoder: PostboxEncoder
|
|
|
|
init(codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any]) {
|
|
self.codingPath = codingPath
|
|
self.userInfo = userInfo
|
|
|
|
self.encoder = PostboxEncoder()
|
|
}
|
|
|
|
func makeData() -> (Data, ValueType) {
|
|
let buffer = WriteBuffer()
|
|
|
|
var typeHash: Int32 = 0
|
|
buffer.write(&typeHash, offset: 0, length: 4)
|
|
|
|
let data = self.encoder.makeData()
|
|
|
|
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 {
|
|
let innerEncoder = _AdaptedPostboxEncoder()
|
|
try! value.encode(to: innerEncoder)
|
|
|
|
let (data, valueType) = innerEncoder.makeData()
|
|
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: 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 {}
|