mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-24 04:37:50 +00:00
Introduce StructuredHeaderField protocol.
Motivation: We had a proliferation of decode/encode methods that required users to tell us how to encode their structured header field. This is a bit annoying for two reasons: firstly, it requires users to encode this piece of information separately from their header field; secondly, it makes it very hard to offer a generic interface to load something as a structured header field! Modifications: - Add a new StructuredHeaderField protocol that refines Codable and expresses what type of header field is used. - Remove the fancy encode/decode methods. Result: Simpler interfaces.
This commit is contained in:
1 parent
6a2b13e370
commit
101dec0a99
4 files changed
+278
-119
No files matched your search
@@ -0,0 +1,93 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
import Foundation
|
||||
import StructuredHeaders
|
||||
|
||||
private let keyedTopLevelListDecoderSupportedKeys = ["items"]
|
||||
|
||||
/// Used when someone has requested a keyed decoder for a property of list type.
|
||||
///
|
||||
/// There is only one valid key for this: "items".
|
||||
struct KeyedTopLevelListDecoder<Key: CodingKey, BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
private var list: [ItemOrInnerList<BaseData.SubSequence>]
|
||||
|
||||
private var decoder: _StructuredFieldDecoder<BaseData>
|
||||
|
||||
init(_ list: [ItemOrInnerList<BaseData.SubSequence>], decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
self.list = list
|
||||
self.decoder = decoder
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyedTopLevelListDecoder: KeyedDecodingContainerProtocol {
|
||||
var codingPath: [CodingKey] {
|
||||
self.decoder.codingPath
|
||||
}
|
||||
|
||||
var allKeys: [Key] {
|
||||
keyedTopLevelListDecoderSupportedKeys.compactMap { Key(stringValue: $0) }
|
||||
}
|
||||
|
||||
func contains(_ key: Key) -> Bool {
|
||||
keyedTopLevelListDecoderSupportedKeys.contains(key.stringValue)
|
||||
}
|
||||
|
||||
func decodeNil(forKey key: Key) throws -> Bool {
|
||||
// Keys are never nil for this type.
|
||||
false
|
||||
}
|
||||
|
||||
func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T: Decodable {
|
||||
try self.decoder.push(_StructuredHeaderCodingKey(key, keyDecodingStrategy: self.decoder.keyDecodingStrategy))
|
||||
defer {
|
||||
self.decoder.pop()
|
||||
}
|
||||
|
||||
if type is Data.Type {
|
||||
let container = try self.decoder.singleValueContainer()
|
||||
return try container.decode(Data.self) as! T
|
||||
} else if type is Decimal.Type {
|
||||
let container = try self.decoder.singleValueContainer()
|
||||
return try container.decode(Decimal.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> {
|
||||
try self.decoder.push(_StructuredHeaderCodingKey(key, keyDecodingStrategy: self.decoder.keyDecodingStrategy))
|
||||
defer {
|
||||
self.decoder.pop()
|
||||
}
|
||||
return try self.decoder.container(keyedBy: type)
|
||||
}
|
||||
|
||||
func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer {
|
||||
try self.decoder.push(_StructuredHeaderCodingKey(key, keyDecodingStrategy: self.decoder.keyDecodingStrategy))
|
||||
defer {
|
||||
self.decoder.pop()
|
||||
}
|
||||
return try self.decoder.unkeyedContainer()
|
||||
}
|
||||
|
||||
func superDecoder() throws -> Decoder {
|
||||
// Items never support inherited types.
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
|
||||
func superDecoder(forKey key: Key) throws -> Decoder {
|
||||
// Items never support inherited types.
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
@@ -41,20 +41,20 @@ extension StructuredFieldDecoder {
|
||||
extension StructuredFieldDecoder {
|
||||
/// Attempt to decode an object from a structured header field.
|
||||
///
|
||||
/// This method will attempt to guess what kind of structured header field is being
|
||||
/// parsed based on the structure of `type`. This is useful for quick prototyping, but
|
||||
/// generally this method should be avoided in favour of one of the other `decode`
|
||||
/// methods on this type.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - type: The type of the object to decode.
|
||||
/// - data: The bytes of the structured header field.
|
||||
/// - throws: If the header field could not be parsed, or could not be decoded.
|
||||
/// - returns: An object of type `StructuredField`.
|
||||
public func decode<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
let parser = StructuredFieldParser(data)
|
||||
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
|
||||
return try type.init(from: decoder)
|
||||
public func decode<StructuredField: StructuredHeaderField, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
switch StructuredField.structuredFieldType {
|
||||
case .item:
|
||||
return try self.decodeItemField(from: data)
|
||||
case .list:
|
||||
return try self.decodeListField(from: data)
|
||||
case .dictionary:
|
||||
return try self.decodeDictionaryField(from: data)
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempt to decode an object from a structured header dictionary field.
|
||||
@@ -64,7 +64,7 @@ extension StructuredFieldDecoder {
|
||||
/// - data: The bytes of the structured header field.
|
||||
/// - throws: If the header field could not be parsed, or could not be decoded.
|
||||
/// - returns: An object of type `StructuredField`.
|
||||
public func decodeDictionaryField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
private func decodeDictionaryField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
let parser = StructuredFieldParser(data)
|
||||
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
|
||||
try decoder.parseDictionaryField()
|
||||
@@ -78,7 +78,7 @@ extension StructuredFieldDecoder {
|
||||
/// - data: The bytes of the structured header field.
|
||||
/// - throws: If the header field could not be parsed, or could not be decoded.
|
||||
/// - returns: An object of type `StructuredField`.
|
||||
public func decodeListField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
private func decodeListField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
let parser = StructuredFieldParser(data)
|
||||
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
|
||||
try decoder.parseListField()
|
||||
@@ -92,7 +92,7 @@ extension StructuredFieldDecoder {
|
||||
/// - data: The bytes of the structured header field.
|
||||
/// - throws: If the header field could not be parsed, or could not be decoded.
|
||||
/// - returns: An object of type `StructuredField`.
|
||||
public func decodeItemField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
private func decodeItemField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
let parser = StructuredFieldParser(data)
|
||||
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
|
||||
try decoder.parseItemField()
|
||||
@@ -148,34 +148,24 @@ extension _StructuredFieldDecoder: Decoder {
|
||||
}
|
||||
|
||||
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key: CodingKey {
|
||||
if self.currentElement == nil {
|
||||
// First parse. This may be a dictionary, but depending on the keys this may also be list or item.
|
||||
// We can't really detect this (Key isn't caseiterable) so we just kinda have to hope. Users can tell
|
||||
// us with alternative methods if we're wrong.
|
||||
try self.parseDictionaryField()
|
||||
}
|
||||
|
||||
switch self.currentElement! {
|
||||
case .dictionary(let dictionary):
|
||||
return KeyedDecodingContainer(DictionaryKeyedContainer(dictionary, decoder: self))
|
||||
case .item(let item):
|
||||
return KeyedDecodingContainer(KeyedItemDecoder(item, decoder: self))
|
||||
case .list(let list):
|
||||
return KeyedDecodingContainer(KeyedTopLevelListDecoder(list, decoder: self))
|
||||
case .innerList(let innerList):
|
||||
return KeyedDecodingContainer(KeyedInnerListDecoder(innerList, decoder: self))
|
||||
case .parameters(let parameters):
|
||||
return KeyedDecodingContainer(ParametersDecoder(parameters, decoder: self))
|
||||
case .bareItem, .bareInnerList, .list:
|
||||
case .bareItem, .bareInnerList:
|
||||
// No keyed container for these types.
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
|
||||
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
|
||||
if self.currentElement == nil {
|
||||
// First parse, this is a list header.
|
||||
try self.parseListField()
|
||||
}
|
||||
|
||||
// We have unkeyed containers for lists, inner lists, and bare inner lists.
|
||||
switch self.currentElement! {
|
||||
case .list(let items):
|
||||
@@ -191,11 +181,6 @@ extension _StructuredFieldDecoder: Decoder {
|
||||
}
|
||||
|
||||
func singleValueContainer() throws -> SingleValueDecodingContainer {
|
||||
if self.currentElement == nil {
|
||||
// First parse, this is a an item header.
|
||||
try self.parseItemField()
|
||||
}
|
||||
|
||||
// We have single value containers for items and bareItems.
|
||||
switch self.currentElement! {
|
||||
case .item(let item):
|
||||
@@ -256,16 +241,24 @@ extension _StructuredFieldDecoder {
|
||||
return .innerList(innerList)
|
||||
}
|
||||
case .list(let list):
|
||||
guard let offset = key.intValue, offset < list.count else {
|
||||
if let offset = key.intValue {
|
||||
guard offset < list.count else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
let index = list.index(list.startIndex, offsetBy: offset)
|
||||
switch list[index] {
|
||||
case .item(let item):
|
||||
return .item(item)
|
||||
case .innerList(let innerList):
|
||||
return .innerList(innerList)
|
||||
}
|
||||
} else if key.stringValue == "items" {
|
||||
// Oh, the outer layer is keyed. That's fine, just put ourselves
|
||||
// back on the stack.
|
||||
return .list(list)
|
||||
} else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
let index = list.index(list.startIndex, offsetBy: offset)
|
||||
switch list[index] {
|
||||
case .item(let item):
|
||||
return .item(item)
|
||||
case .innerList(let innerList):
|
||||
return .innerList(innerList)
|
||||
}
|
||||
case .item(let item):
|
||||
// Two keys, "item" and "parameters".
|
||||
switch key.stringValue {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// A `StructuredHeaderField` is a `Codable` representation of a HTTP Structured
|
||||
/// Header Field.
|
||||
///
|
||||
/// This protocol is a helper protocol that refines `Codable` to indicate what kind
|
||||
/// of header field a given field uses.
|
||||
public protocol StructuredHeaderField: Codable {
|
||||
static var structuredFieldType: StructuredHeaderFieldType { get }
|
||||
}
|
||||
|
||||
|
||||
/// The kinds of header fields used in HTTP Structured Headers.
|
||||
public enum StructuredHeaderFieldType {
|
||||
/// An item field consists of a single item, optionally with parameters.
|
||||
case item
|
||||
|
||||
/// A list field consists of a list made up of inner lists or items.
|
||||
case list
|
||||
|
||||
/// A dictionary field is an ordered collection of key-value pairs.
|
||||
case dictionary
|
||||
}
|
||||
@@ -31,10 +31,36 @@ struct ListyDictionaryParameterisedList: Codable, Equatable {
|
||||
var parameters: ListyDictionaryFieldParameters
|
||||
}
|
||||
|
||||
fileprivate struct Item<Base: Codable & Equatable>: StructuredHeaderField, Equatable {
|
||||
static var structuredFieldType: StructuredHeaderFieldType {
|
||||
return .item
|
||||
}
|
||||
|
||||
var item: Base
|
||||
|
||||
init(_ item: Base) {
|
||||
self.item = item
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate struct List<Base: Codable & Equatable>: StructuredHeaderField, Equatable {
|
||||
static var structuredFieldType: StructuredHeaderFieldType {
|
||||
return .list
|
||||
}
|
||||
|
||||
var items: [Base]
|
||||
|
||||
init(_ items: [Base]) {
|
||||
self.items = items
|
||||
}
|
||||
}
|
||||
|
||||
/// An example ListyDictionary structured header field.
|
||||
///
|
||||
/// An example of this field is: 'primary=bar;q=1.0, secondary=baz;q=0.5;fallback=last, acceptablejurisdictions=(AU;q=1.0 GB;q=0.9 FR);fallback=primary'
|
||||
struct ListyDictionaryField: Codable, Equatable {
|
||||
struct ListyDictionaryField: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .dictionary
|
||||
|
||||
var primary: ListyDictionaryParameterisedString
|
||||
var secondary: ListyDictionaryParameterisedString
|
||||
var acceptablejurisdictions: ListyDictionaryParameterisedList
|
||||
@@ -53,7 +79,9 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
}
|
||||
|
||||
func testCanDecodeParameterisedItemsWithoutParameters() throws {
|
||||
struct ListyDictionaryNoParams: Codable, Equatable {
|
||||
struct ListyDictionaryNoParams: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .dictionary
|
||||
|
||||
var primary: String
|
||||
var secondary: String
|
||||
var acceptablejurisdictions: [String]
|
||||
@@ -67,16 +95,16 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
func testCanDecodeIntegersInVariousWays() throws {
|
||||
let headerField = "5;bar=baz"
|
||||
|
||||
XCTAssertEqual(UInt8(5), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Int8(5), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(UInt16(5), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Int16(5), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(UInt32(5), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Int32(5), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(UInt64(5), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Int64(5), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(UInt(5), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Int(5), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(UInt8(5)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(Int8(5)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(UInt16(5)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(Int16(5)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(UInt32(5)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(Int32(5)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(UInt64(5)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(Int64(5)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(UInt(5)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(Int(5)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
}
|
||||
|
||||
func testOutOfRangeNumbersAreReported() throws {
|
||||
@@ -85,63 +113,66 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
// rather than crashing for all non-Int64 types. (Ignoring Int/UInt due to their platform
|
||||
// dependence)
|
||||
let headerField = "-999999999999999;bar=baz"
|
||||
let expected = Int64(-999_999_999_999_999)
|
||||
let expected = Item(Int64(-999_999_999_999_999))
|
||||
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Int8.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(UInt8.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Int16.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(UInt16.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Int32.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(UInt32.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(UInt64.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<Int8>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<UInt8>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<Int16>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<UInt16>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<Int32>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<UInt32>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<UInt64>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(expected, try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
}
|
||||
|
||||
func testDoubleAndFloatInterchangeable() throws {
|
||||
let headerField = "5.0;bar=baz"
|
||||
|
||||
XCTAssertEqual(Float(5.0), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Double(5.0), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(Float(5.0)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(Item(Double(5.0)), try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
}
|
||||
|
||||
func testAskingForTheWrongType() throws {
|
||||
let headerField = "gzip"
|
||||
let intField = "5"
|
||||
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Int8.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(UInt8.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Int16.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(UInt16.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Int32.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(UInt32.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Int64.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(UInt64.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Double.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Float.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Bool.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(String.self, from: Array(intField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<Int8>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<UInt8>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<Int16>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<UInt16>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<Int32>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<UInt32>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<Int64>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<UInt64>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<Double>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<Float>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<Bool>.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(Item<String>.self, from: Array(intField.utf8)))
|
||||
}
|
||||
|
||||
func testDecodingTopLevelItemWithParameters() throws {
|
||||
struct IntWithParams: Codable, Equatable {
|
||||
struct IntWithParams: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .item
|
||||
|
||||
var item: Int
|
||||
var parameters: [String: String]
|
||||
}
|
||||
|
||||
let headerField = "5;bar=baz"
|
||||
let expected = IntWithParams(item: 5, parameters: ["bar": "baz"])
|
||||
XCTAssertEqual(expected, try StructuredFieldDecoder().decodeItemField(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(expected, try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
}
|
||||
|
||||
func testDecodingTopLevelList() throws {
|
||||
let headerField = "foo, bar, baz"
|
||||
let expected = ["foo", "bar", "baz"]
|
||||
XCTAssertEqual(expected, try StructuredFieldDecoder().decodeListField(from: Array(headerField.utf8)))
|
||||
let expected = List(["foo", "bar", "baz"])
|
||||
XCTAssertEqual(expected, try StructuredFieldDecoder().decode(from: Array(headerField.utf8)))
|
||||
}
|
||||
|
||||
func testDecodingLowercaseKeyStrategy() throws {
|
||||
struct Camel: Codable, Equatable {
|
||||
struct Camel: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .dictionary
|
||||
|
||||
var hasHump: Bool
|
||||
}
|
||||
|
||||
@@ -150,7 +181,7 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
var decoder = StructuredFieldDecoder()
|
||||
decoder.keyDecodingStrategy = .lowercase
|
||||
|
||||
XCTAssertEqual(expected, try decoder.decodeDictionaryField(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(expected, try decoder.decode(from: Array(headerField.utf8)))
|
||||
}
|
||||
|
||||
func testDecodingLowercaseKeyStrategyParameters() throws {
|
||||
@@ -158,7 +189,9 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
var hasHump: Bool
|
||||
}
|
||||
|
||||
struct Camel: Codable, Equatable {
|
||||
struct Camel: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .item
|
||||
|
||||
var item: String
|
||||
var parameters: CamelParameters
|
||||
}
|
||||
@@ -168,37 +201,40 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
var decoder = StructuredFieldDecoder()
|
||||
decoder.keyDecodingStrategy = .lowercase
|
||||
|
||||
XCTAssertEqual(expected, try decoder.decodeItemField(from: Array(headerField.utf8)))
|
||||
XCTAssertEqual(expected, try decoder.decode(from: Array(headerField.utf8)))
|
||||
}
|
||||
|
||||
func testDecodingKeyMissingFromDictionary() throws {
|
||||
struct MissingKey: Codable {
|
||||
struct MissingKey: StructuredHeaderField {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .dictionary
|
||||
var foo: Int
|
||||
}
|
||||
|
||||
let headerField = "bar=baz"
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decodeDictionaryField(MissingKey.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(MissingKey.self, from: Array(headerField.utf8)))
|
||||
}
|
||||
|
||||
func testDecodingKeyAsItemWantedInnerList() throws {
|
||||
struct MissingInnerList: Codable {
|
||||
struct MissingInnerList: StructuredHeaderField {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .dictionary
|
||||
var innerlist: [String]
|
||||
}
|
||||
|
||||
let headerField = "innerlist=x"
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decodeDictionaryField(MissingInnerList.self, from: Array(headerField.utf8)))
|
||||
XCTAssertThrowsError(try StructuredFieldDecoder().decode(MissingInnerList.self, from: Array(headerField.utf8)))
|
||||
}
|
||||
|
||||
func testDecodingBinaryAsTopLevelData() throws {
|
||||
let headerField = ":AQIDBA==:"
|
||||
XCTAssertEqual(
|
||||
Data([1, 2, 3, 4]),
|
||||
try StructuredFieldDecoder().decodeItemField(Data.self, from: Array(headerField.utf8))
|
||||
Item(Data([1, 2, 3, 4])),
|
||||
try StructuredFieldDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingBinaryAsParameterisedData() throws {
|
||||
struct Item: Codable, Equatable {
|
||||
struct Item: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .item
|
||||
var item: Data
|
||||
var parameters: [String: Float]
|
||||
}
|
||||
@@ -208,17 +244,18 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
|
||||
XCTAssertEqual(
|
||||
Item(item: Data([1, 2, 3, 4]), parameters: [:]),
|
||||
try StructuredFieldDecoder().decodeItemField(Item.self, from: Array(headerFieldNoParameters.utf8))
|
||||
try StructuredFieldDecoder().decode(Item.self, from: Array(headerFieldNoParameters.utf8))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
Item(item: Data([1, 2, 3, 4]), parameters: ["q": 0.8]),
|
||||
try StructuredFieldDecoder().decodeItemField(Item.self, from: Array(headerFieldParameters.utf8))
|
||||
try StructuredFieldDecoder().decode(Item.self, from: Array(headerFieldParameters.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingBinaryInParameterField() throws {
|
||||
struct Item: Codable, Equatable {
|
||||
struct Item: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .item
|
||||
var item: Int
|
||||
var parameters: [String: Data]
|
||||
}
|
||||
@@ -226,34 +263,31 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
let headerField = "1;q=:AQIDBA==:"
|
||||
XCTAssertEqual(
|
||||
Item(item: 1, parameters: ["q": Data([1, 2, 3, 4])]),
|
||||
try StructuredFieldDecoder().decodeItemField(Item.self, from: Array(headerField.utf8))
|
||||
try StructuredFieldDecoder().decode(Item.self, from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingBinaryInOuterListRaw() throws {
|
||||
let headerField = ":AQIDBA==:, :BQYHCA==:"
|
||||
XCTAssertEqual(
|
||||
[Data([1, 2, 3, 4]), Data([5, 6, 7, 8])],
|
||||
try StructuredFieldDecoder().decodeListField([Data].self, from: Array(headerField.utf8))
|
||||
List([Data([1, 2, 3, 4]), Data([5, 6, 7, 8])]),
|
||||
try StructuredFieldDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingBinaryInOuterListKeyed() throws {
|
||||
struct ListField: Codable, Equatable {
|
||||
var items: [Data]
|
||||
}
|
||||
let headerField = ":AQIDBA==:, :BQYHCA==:"
|
||||
XCTAssertEqual(
|
||||
[Data([1, 2, 3, 4]), Data([5, 6, 7, 8])],
|
||||
try StructuredFieldDecoder().decodeListField([Data].self, from: Array(headerField.utf8))
|
||||
List([Data([1, 2, 3, 4]), Data([5, 6, 7, 8])]),
|
||||
try StructuredFieldDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingBinaryInInnerListRaw() throws {
|
||||
let headerField = "(:AQIDBA==: :BQYHCA==:), (:AQIDBA==: :BQYHCA==:)"
|
||||
XCTAssertEqual(
|
||||
Array(repeating: [Data([1, 2, 3, 4]), Data([5, 6, 7, 8])], count: 2),
|
||||
try StructuredFieldDecoder().decodeListField([[Data]].self, from: Array(headerField.utf8))
|
||||
List(Array(repeating: [Data([1, 2, 3, 4]), Data([5, 6, 7, 8])], count: 2)),
|
||||
try StructuredFieldDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
@@ -264,13 +298,14 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
}
|
||||
let headerField = "(:AQIDBA==: :BQYHCA==:);foo, (:AQIDBA==: :BQYHCA==:);foo"
|
||||
XCTAssertEqual(
|
||||
Array(repeating: ListField(items: [Data([1, 2, 3, 4]), Data([5, 6, 7, 8])], parameters: ["foo": true]), count: 2),
|
||||
try StructuredFieldDecoder().decodeListField([ListField].self, from: Array(headerField.utf8))
|
||||
List(Array(repeating: ListField(items: [Data([1, 2, 3, 4]), Data([5, 6, 7, 8])], parameters: ["foo": true]), count: 2)),
|
||||
try StructuredFieldDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingBinaryInDictionaries() throws {
|
||||
struct DictionaryField: Codable, Equatable {
|
||||
struct DictionaryField: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .dictionary
|
||||
var bin: Data
|
||||
var box: Data
|
||||
}
|
||||
@@ -278,20 +313,21 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
let headerField = "bin=:AQIDBA==:, box=:AQIDBA==:"
|
||||
XCTAssertEqual(
|
||||
DictionaryField(bin: Data([1, 2, 3, 4]), box: Data([1, 2, 3, 4])),
|
||||
try StructuredFieldDecoder().decodeDictionaryField(DictionaryField.self, from: Array(headerField.utf8))
|
||||
try StructuredFieldDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDecimalAsTopLevelData() throws {
|
||||
let headerField = "987654321.123"
|
||||
XCTAssertEqual(
|
||||
Decimal(string: "987654321.123")!,
|
||||
try StructuredFieldDecoder().decodeItemField(Decimal.self, from: Array(headerField.utf8))
|
||||
Item(Decimal(string: "987654321.123")!),
|
||||
try StructuredFieldDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDecimalAsParameterisedData() throws {
|
||||
struct Item: Codable, Equatable {
|
||||
struct Item: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .item
|
||||
var item: Decimal
|
||||
var parameters: [String: Float]
|
||||
}
|
||||
@@ -301,17 +337,18 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
|
||||
XCTAssertEqual(
|
||||
Item(item: Decimal(string: "987654321.123")!, parameters: [:]),
|
||||
try StructuredFieldDecoder().decodeItemField(Item.self, from: Array(headerFieldNoParameters.utf8))
|
||||
try StructuredFieldDecoder().decode(from: Array(headerFieldNoParameters.utf8))
|
||||
)
|
||||
|
||||
XCTAssertEqual(
|
||||
Item(item: Decimal(string: "987654321.123")!, parameters: ["q": 0.8]),
|
||||
try StructuredFieldDecoder().decodeItemField(Item.self, from: Array(headerFieldParameters.utf8))
|
||||
try StructuredFieldDecoder().decode(from: Array(headerFieldParameters.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDecimalInParameterField() throws {
|
||||
struct Item: Codable, Equatable {
|
||||
struct Item: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .item
|
||||
var item: Int
|
||||
var parameters: [String: Decimal]
|
||||
}
|
||||
@@ -319,23 +356,23 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
let headerField = "1;q=987654321.123"
|
||||
XCTAssertEqual(
|
||||
Item(item: 1, parameters: ["q": Decimal(string: "987654321.123")!]),
|
||||
try StructuredFieldDecoder().decodeItemField(Item.self, from: Array(headerField.utf8))
|
||||
try StructuredFieldDecoder().decode(Item.self, from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDecimalInOuterListRaw() throws {
|
||||
let headerField = "987654321.123, 123456789.321"
|
||||
XCTAssertEqual(
|
||||
[Decimal(string: "987654321.123")!, Decimal(string: "123456789.321")!],
|
||||
try StructuredFieldDecoder().decodeListField([Decimal].self, from: Array(headerField.utf8))
|
||||
List([Decimal(string: "987654321.123")!, Decimal(string: "123456789.321")!]),
|
||||
try StructuredFieldDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDecimalInInnerListRaw() throws {
|
||||
let headerField = "(987654321.123 123456789.321), (987654321.123 123456789.321)"
|
||||
XCTAssertEqual(
|
||||
Array(repeating: [Decimal(string: "987654321.123")!, Decimal(string: "123456789.321")!], count: 2),
|
||||
try StructuredFieldDecoder().decodeListField([[Decimal]].self, from: Array(headerField.utf8))
|
||||
List(Array(repeating: [Decimal(string: "987654321.123")!, Decimal(string: "123456789.321")!], count: 2)),
|
||||
try StructuredFieldDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
@@ -346,13 +383,14 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
}
|
||||
let headerField = "(987654321.123 123456789.321);foo, (987654321.123 123456789.321);foo"
|
||||
XCTAssertEqual(
|
||||
Array(repeating: ListField(items: [Decimal(string: "987654321.123")!, Decimal(string: "123456789.321")!], parameters: ["foo": true]), count: 2),
|
||||
try StructuredFieldDecoder().decodeListField([ListField].self, from: Array(headerField.utf8))
|
||||
List(Array(repeating: ListField(items: [Decimal(string: "987654321.123")!, Decimal(string: "123456789.321")!], parameters: ["foo": true]), count: 2)),
|
||||
try StructuredFieldDecoder().decode(from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
|
||||
func testDecodingDecimalInDictionaries() throws {
|
||||
struct DictionaryField: Codable, Equatable {
|
||||
struct DictionaryField: StructuredHeaderField, Equatable {
|
||||
static let structuredFieldType: StructuredHeaderFieldType = .dictionary
|
||||
var bin: Decimal
|
||||
var box: Decimal
|
||||
}
|
||||
@@ -360,7 +398,7 @@ final class StructuredFieldDecoderTests: XCTestCase {
|
||||
let headerField = "bin=987654321.123, box=123456789.321"
|
||||
XCTAssertEqual(
|
||||
DictionaryField(bin: Decimal(string: "987654321.123")!, box: Decimal(string: "123456789.321")!),
|
||||
try StructuredFieldDecoder().decodeDictionaryField(DictionaryField.self, from: Array(headerField.utf8))
|
||||
try StructuredFieldDecoder().decode(DictionaryField.self, from: Array(headerField.utf8))
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user