Add support for encoding/decoding binary data

This commit is contained in:
Cory Benfield committed 2020-07-16 13:16:20 +01:00
1 parent 5aaca631da
commit b797a6ff50
11 files changed
+211 -12

No files matched your search

@@ -11,6 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import StructuredHeaders
struct BareInnerListDecoder<BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
@@ -64,7 +65,13 @@ extension BareInnerListDecoder: UnkeyedDecodingContainer {
defer {
self.decoder.pop()
}
return try type.init(from: self.decoder)
if type is Data.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Data.self) as! T
} else {
return try type.init(from: self.decoder)
}
}
mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
@@ -11,6 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import StructuredHeaders
struct BareItemDecoder<BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
@@ -96,6 +97,18 @@ extension BareItemDecoder: SingleValueDecodingContainer {
return bool
}
func decode(_ type: Data.Type) throws -> Data {
guard case .undecodedByteSequence(let data) = self.item else {
throw StructuredHeaderError.invalidTypeForItem
}
guard let decoded = Data(base64Encoded: Data(data)) else {
throw StructuredHeaderError.invalidByteSequence
}
return decoded
}
func decodeNil() -> Bool {
// Items are never nil.
return false
@@ -131,6 +144,8 @@ extension BareItemDecoder: SingleValueDecodingContainer {
return try self.decode(String.self) as! T
case is Bool.Type:
return try self.decode(Bool.self) as! T
case is Data.Type:
return try self.decode(Data.self) as! T
default:
// Some other codable type. Not sure what to do here yet.
// TODO: What about binary data here? For now we'll ignore it.
@@ -11,6 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import StructuredHeaders
struct DictionaryKeyedContainer<Key: CodingKey, BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
@@ -47,7 +48,13 @@ extension DictionaryKeyedContainer: KeyedDecodingContainerProtocol {
defer {
self.decoder.pop()
}
return try type.init(from: self.decoder)
if type is Data.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Data.self) as! T
} else {
return try type.init(from: self.decoder)
}
}
func nestedContainer<NestedKey: CodingKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> {
@@ -11,6 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import StructuredHeaders
private let keyedInnerListDecoderSupportedKeys = ["items", "parameters"]
@@ -52,7 +53,13 @@ extension KeyedInnerListDecoder: KeyedDecodingContainerProtocol {
defer {
self.decoder.pop()
}
return try type.init(from: self.decoder)
if type is Data.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Data.self) as! T
} else {
return try type.init(from: self.decoder)
}
}
func nestedContainer<NestedKey: CodingKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> {
@@ -11,6 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import StructuredHeaders
private let keyedItemDecoderSupportedKeys = ["item", "parameters"]
@@ -52,7 +53,13 @@ extension KeyedItemDecoder: KeyedDecodingContainerProtocol {
defer {
self.decoder.pop()
}
return try type.init(from: self.decoder)
if type is Data.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Data.self) as! T
} else {
return try type.init(from: self.decoder)
}
}
func nestedContainer<NestedKey: CodingKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> {
@@ -11,6 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import StructuredHeaders
struct ParametersDecoder<Key: CodingKey, BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
@@ -47,7 +48,13 @@ extension ParametersDecoder: KeyedDecodingContainerProtocol {
defer {
self.decoder.pop()
}
return try type.init(from: self.decoder)
if type is Data.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Data.self) as! T
} else {
return try type.init(from: self.decoder)
}
}
func nestedContainer<NestedKey: CodingKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> {
@@ -11,6 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import StructuredHeaders
public struct StructuredFieldDecoder {
@@ -58,7 +59,15 @@ extension StructuredFieldDecoder {
let parser = StructuredFieldParser(data)
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
try decoder.parseItemField()
return try type.init(from: decoder)
// An escape hatch here for top-level data: if we don't do this, it'll ask for
// an unkeyed container and get very confused.
if type is Data.Type {
let container = try decoder.singleValueContainer()
return try container.decode(Data.self) as! StructuredField
} else {
return try type.init(from: decoder)
}
}
}
@@ -11,6 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import StructuredHeaders
struct TopLevelListDecoder<BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
@@ -64,7 +65,13 @@ extension TopLevelListDecoder: UnkeyedDecodingContainer {
defer {
self.decoder.pop()
}
return try type.init(from: self.decoder)
if type is Data.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Data.self) as! T
} else {
return try type.init(from: self.decoder)
}
}
mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {