mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-25 04:47:48 +00:00
Rename a number of types and modules. (#13)
Motivation: In general we should try to be specific about what we're referring to. "Structured headers" is not really an accurate description of what this module works with: "Structured Header Field Values", or "Structured Field Value" is usually better. To that end this change adjusts our naming to be closer to what we want. It also changes our module names to prioritise the use of the Codable-based one. Modifications: - Renamed CodableStructuredHeaders to StructuredFieldValues. - Renamed StructuredHeaders to RawStructuredFieldValues. - Renamed a number of types from Field to FieldValue. - Renamed a number of types from Header to Field or FieldValue. - Renamed some functions along the above lines. Result: Better and more consistent terminology will be used.
This commit is contained in:
1 parent
f881e6c41b
commit
27e163f96a
31 files changed
+313
-313
No files matched your search
@@ -0,0 +1,119 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020-2021 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 RawStructuredFieldValues
|
||||
|
||||
struct BareInnerListDecoder<BaseData: RandomAccessCollection> where BaseData.Element == UInt8 {
|
||||
private var list: BareInnerList
|
||||
|
||||
private var currentOffset: Int
|
||||
|
||||
private var decoder: _StructuredFieldDecoder<BaseData>
|
||||
|
||||
init(_ list: BareInnerList, decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
self.list = list
|
||||
self.currentOffset = 0
|
||||
self.decoder = decoder
|
||||
}
|
||||
}
|
||||
|
||||
extension BareInnerListDecoder: UnkeyedDecodingContainer {
|
||||
var codingPath: [CodingKey] {
|
||||
self.decoder.codingPath
|
||||
}
|
||||
|
||||
var count: Int? {
|
||||
self.list.count
|
||||
}
|
||||
|
||||
var isAtEnd: Bool {
|
||||
self.currentOffset == self.list.count
|
||||
}
|
||||
|
||||
var currentIndex: Int {
|
||||
self.currentOffset
|
||||
}
|
||||
|
||||
mutating func decodeNil() throws -> Bool {
|
||||
// We never decode nil.
|
||||
false
|
||||
}
|
||||
|
||||
mutating func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
|
||||
// This is a request to decode a scalar. We decode the next entry and increment the index.
|
||||
guard !self.isAtEnd else {
|
||||
throw StructuredHeaderError.indexOutOfRange
|
||||
}
|
||||
|
||||
let codingKey = _StructuredHeaderCodingKey(intValue: self.currentOffset)
|
||||
defer {
|
||||
self.currentOffset += 1
|
||||
}
|
||||
|
||||
try self.decoder.push(codingKey)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey {
|
||||
// This is a request to decode a full item. We decode the next entry and increment the index.
|
||||
guard !self.isAtEnd else {
|
||||
throw StructuredHeaderError.indexOutOfRange
|
||||
}
|
||||
|
||||
let codingKey = _StructuredHeaderCodingKey(intValue: self.currentOffset)
|
||||
defer {
|
||||
self.currentOffset += 1
|
||||
}
|
||||
|
||||
try self.decoder.push(codingKey)
|
||||
defer {
|
||||
self.decoder.pop()
|
||||
}
|
||||
return try self.decoder.container(keyedBy: type)
|
||||
}
|
||||
|
||||
mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
|
||||
guard !self.isAtEnd else {
|
||||
throw StructuredHeaderError.indexOutOfRange
|
||||
}
|
||||
|
||||
let codingKey = _StructuredHeaderCodingKey(intValue: self.currentOffset)
|
||||
defer {
|
||||
self.currentOffset += 1
|
||||
}
|
||||
|
||||
try self.decoder.push(codingKey)
|
||||
defer {
|
||||
self.decoder.pop()
|
||||
}
|
||||
return try self.decoder.unkeyedContainer()
|
||||
}
|
||||
|
||||
mutating func superDecoder() throws -> Decoder {
|
||||
// No inheritance here folks
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020-2021 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 RawStructuredFieldValues
|
||||
|
||||
struct BareItemDecoder {
|
||||
private var item: BareItem
|
||||
|
||||
private var _codingPath: [_StructuredHeaderCodingKey]
|
||||
|
||||
init(_ item: BareItem, codingPath: [_StructuredHeaderCodingKey]) {
|
||||
self.item = item
|
||||
self._codingPath = codingPath
|
||||
}
|
||||
}
|
||||
|
||||
extension BareItemDecoder: SingleValueDecodingContainer {
|
||||
var codingPath: [CodingKey] {
|
||||
self._codingPath as [CodingKey]
|
||||
}
|
||||
|
||||
func decode(_ type: UInt8.Type) throws -> UInt8 {
|
||||
try self._decodeFixedWidthInteger(type)
|
||||
}
|
||||
|
||||
func decode(_ type: Int8.Type) throws -> Int8 {
|
||||
try self._decodeFixedWidthInteger(type)
|
||||
}
|
||||
|
||||
func decode(_ type: UInt16.Type) throws -> UInt16 {
|
||||
try self._decodeFixedWidthInteger(type)
|
||||
}
|
||||
|
||||
func decode(_ type: Int16.Type) throws -> Int16 {
|
||||
try self._decodeFixedWidthInteger(type)
|
||||
}
|
||||
|
||||
func decode(_ type: UInt32.Type) throws -> UInt32 {
|
||||
try self._decodeFixedWidthInteger(type)
|
||||
}
|
||||
|
||||
func decode(_ type: Int32.Type) throws -> Int32 {
|
||||
try self._decodeFixedWidthInteger(type)
|
||||
}
|
||||
|
||||
func decode(_ type: UInt64.Type) throws -> UInt64 {
|
||||
try self._decodeFixedWidthInteger(type)
|
||||
}
|
||||
|
||||
func decode(_ type: Int64.Type) throws -> Int64 {
|
||||
try self._decodeFixedWidthInteger(type)
|
||||
}
|
||||
|
||||
func decode(_ type: UInt.Type) throws -> UInt {
|
||||
try self._decodeFixedWidthInteger(type)
|
||||
}
|
||||
|
||||
func decode(_ type: Int.Type) throws -> Int {
|
||||
try self._decodeFixedWidthInteger(type)
|
||||
}
|
||||
|
||||
func decode(_ type: Float.Type) throws -> Float {
|
||||
try self._decodeBinaryFloatingPoint(type)
|
||||
}
|
||||
|
||||
func decode(_ type: Double.Type) throws -> Double {
|
||||
try self._decodeBinaryFloatingPoint(type)
|
||||
}
|
||||
|
||||
func decode(_: String.Type) throws -> String {
|
||||
switch self.item {
|
||||
case .string(let string):
|
||||
return string
|
||||
case .token(let token):
|
||||
return token
|
||||
default:
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_: Bool.Type) throws -> Bool {
|
||||
guard case .bool(let bool) = self.item else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
|
||||
return bool
|
||||
}
|
||||
|
||||
func decode(_: Data.Type) throws -> Data {
|
||||
guard case .undecodedByteSequence(let data) = self.item else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
|
||||
guard let decoded = Data(base64Encoded: data) else {
|
||||
throw StructuredHeaderError.invalidByteSequence
|
||||
}
|
||||
|
||||
return decoded
|
||||
}
|
||||
|
||||
func decode(_: Decimal.Type) throws -> Decimal {
|
||||
guard case .decimal(let pseudoDecimal) = self.item else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
|
||||
return Decimal(sign: pseudoDecimal.mantissa > 0 ? .plus : .minus,
|
||||
exponent: Int(pseudoDecimal.exponent),
|
||||
significand: Decimal(pseudoDecimal.mantissa))
|
||||
}
|
||||
|
||||
func decodeNil() -> Bool {
|
||||
// Items are never nil.
|
||||
false
|
||||
}
|
||||
|
||||
func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
|
||||
switch type {
|
||||
case is UInt8.Type:
|
||||
return try self.decode(UInt8.self) as! T
|
||||
case is Int8.Type:
|
||||
return try self.decode(Int8.self) as! T
|
||||
case is UInt16.Type:
|
||||
return try self.decode(UInt16.self) as! T
|
||||
case is Int16.Type:
|
||||
return try self.decode(Int16.self) as! T
|
||||
case is UInt32.Type:
|
||||
return try self.decode(UInt32.self) as! T
|
||||
case is Int32.Type:
|
||||
return try self.decode(Int32.self) as! T
|
||||
case is UInt64.Type:
|
||||
return try self.decode(UInt64.self) as! T
|
||||
case is Int64.Type:
|
||||
return try self.decode(Int64.self) as! T
|
||||
case is UInt.Type:
|
||||
return try self.decode(UInt.self) as! T
|
||||
case is Int.Type:
|
||||
return try self.decode(Int.self) as! T
|
||||
case is Float.Type:
|
||||
return try self.decode(Float.self) as! T
|
||||
case is Double.Type:
|
||||
return try self.decode(Double.self) as! T
|
||||
case is String.Type:
|
||||
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
|
||||
case is Decimal.Type:
|
||||
return try self.decode(Decimal.self) as! T
|
||||
default:
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
|
||||
private func _decodeBinaryFloatingPoint<T: BinaryFloatingPoint>(_: T.Type) throws -> T {
|
||||
guard case .decimal(let decimal) = self.item else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
|
||||
// Going via Double is a bit sad. Swift Numerics would help here.
|
||||
return T(Double(decimal))
|
||||
}
|
||||
|
||||
private func _decodeFixedWidthInteger<T: FixedWidthInteger>(_: T.Type) throws -> T {
|
||||
guard case .integer(let int) = self.item else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
|
||||
guard let result = T(exactly: int) else {
|
||||
throw StructuredHeaderError.integerOutOfRange
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020-2021 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 RawStructuredFieldValues
|
||||
|
||||
struct DictionaryKeyedContainer<Key: CodingKey, BaseData: RandomAccessCollection> where BaseData.Element == UInt8 {
|
||||
private var dictionary: OrderedMap<String, ItemOrInnerList>
|
||||
|
||||
private var decoder: _StructuredFieldDecoder<BaseData>
|
||||
|
||||
init(_ dictionary: OrderedMap<String, ItemOrInnerList>, decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
self.dictionary = dictionary
|
||||
self.decoder = decoder
|
||||
}
|
||||
}
|
||||
|
||||
extension DictionaryKeyedContainer: KeyedDecodingContainerProtocol {
|
||||
var codingPath: [CodingKey] {
|
||||
self.decoder.codingPath
|
||||
}
|
||||
|
||||
var allKeys: [Key] {
|
||||
self.dictionary.compactMap { Key(stringValue: $0.0) }
|
||||
}
|
||||
|
||||
func contains(_ key: Key) -> Bool {
|
||||
self.dictionary.contains(where: { $0.0 == key.stringValue })
|
||||
}
|
||||
|
||||
func decodeNil(forKey key: Key) throws -> Bool {
|
||||
// We will decode nil if the key is not present.
|
||||
!self.contains(key)
|
||||
}
|
||||
|
||||
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 {
|
||||
// Dictionary headers never support inherited types.
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
|
||||
func superDecoder(forKey key: Key) throws -> Decoder {
|
||||
// Dictionary headers never support inherited types.
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020-2021 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 RawStructuredFieldValues
|
||||
|
||||
private let keyedInnerListDecoderSupportedKeys = ["items", "parameters"]
|
||||
|
||||
/// Used when someone has requested a keyed decoder for a property of inner list type.
|
||||
///
|
||||
/// There are only two valid keys for this: "items" and "parameters".
|
||||
struct KeyedInnerListDecoder<Key: CodingKey, BaseData: RandomAccessCollection> where BaseData.Element == UInt8 {
|
||||
private var innerList: InnerList
|
||||
|
||||
private var decoder: _StructuredFieldDecoder<BaseData>
|
||||
|
||||
init(_ innerList: InnerList, decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
self.innerList = innerList
|
||||
self.decoder = decoder
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyedInnerListDecoder: KeyedDecodingContainerProtocol {
|
||||
var codingPath: [CodingKey] {
|
||||
self.decoder.codingPath
|
||||
}
|
||||
|
||||
var allKeys: [Key] {
|
||||
keyedInnerListDecoderSupportedKeys.compactMap { Key(stringValue: $0) }
|
||||
}
|
||||
|
||||
func contains(_ key: Key) -> Bool {
|
||||
keyedInnerListDecoderSupportedKeys.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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020-2021 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 RawStructuredFieldValues
|
||||
|
||||
private let keyedItemDecoderSupportedKeys = ["item", "parameters"]
|
||||
|
||||
/// Used when someone has requested a keyed decoder for a property of item type.
|
||||
///
|
||||
/// There are only two valid keys for this: "item" and "parameters".
|
||||
struct KeyedItemDecoder<Key: CodingKey, BaseData: RandomAccessCollection> where BaseData.Element == UInt8 {
|
||||
private var item: Item
|
||||
|
||||
private var decoder: _StructuredFieldDecoder<BaseData>
|
||||
|
||||
init(_ item: Item, decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
self.item = item
|
||||
self.decoder = decoder
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyedItemDecoder: KeyedDecodingContainerProtocol {
|
||||
var codingPath: [CodingKey] {
|
||||
self.decoder.codingPath
|
||||
}
|
||||
|
||||
var allKeys: [Key] {
|
||||
keyedItemDecoderSupportedKeys.compactMap { Key(stringValue: $0) }
|
||||
}
|
||||
|
||||
func contains(_ key: Key) -> Bool {
|
||||
keyedItemDecoderSupportedKeys.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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020-2021 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 RawStructuredFieldValues
|
||||
|
||||
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 {
|
||||
private var list: [ItemOrInnerList]
|
||||
|
||||
private var decoder: _StructuredFieldDecoder<BaseData>
|
||||
|
||||
init(_ list: [ItemOrInnerList], 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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020-2021 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 RawStructuredFieldValues
|
||||
|
||||
struct ParametersDecoder<Key: CodingKey, BaseData: RandomAccessCollection> where BaseData.Element == UInt8 {
|
||||
private var parameters: OrderedMap<String, BareItem>
|
||||
|
||||
private var decoder: _StructuredFieldDecoder<BaseData>
|
||||
|
||||
init(_ parameters: OrderedMap<String, BareItem>, decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
self.parameters = parameters
|
||||
self.decoder = decoder
|
||||
}
|
||||
}
|
||||
|
||||
extension ParametersDecoder: KeyedDecodingContainerProtocol {
|
||||
var codingPath: [CodingKey] {
|
||||
self.decoder.codingPath
|
||||
}
|
||||
|
||||
var allKeys: [Key] {
|
||||
self.parameters.compactMap { Key(stringValue: $0.0) }
|
||||
}
|
||||
|
||||
func contains(_ key: Key) -> Bool {
|
||||
self.parameters.contains(where: { $0.0 == key.stringValue })
|
||||
}
|
||||
|
||||
func decodeNil(forKey key: Key) throws -> Bool {
|
||||
// We will decode nil if the key is not present.
|
||||
!self.contains(key)
|
||||
}
|
||||
|
||||
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 {
|
||||
// Parameters never support inherited types.
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
|
||||
func superDecoder(forKey key: Key) throws -> Decoder {
|
||||
// Parameters never support inherited types.
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020-2021 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 RawStructuredFieldValues
|
||||
|
||||
/// A `StructuredFieldValueDecoder` allows decoding `Decodable` objects from a HTTP
|
||||
/// structured header field.
|
||||
public struct StructuredFieldValueDecoder {
|
||||
/// A strategy that should be used to map coding keys to wire format keys.
|
||||
public var keyDecodingStrategy: KeyDecodingStrategy?
|
||||
|
||||
public init() {}
|
||||
}
|
||||
|
||||
extension StructuredFieldValueDecoder {
|
||||
/// A strategy that should be used to map coding keys to wire format keys.
|
||||
public struct KeyDecodingStrategy: Hashable {
|
||||
fileprivate enum Base: Hashable {
|
||||
case lowercase
|
||||
}
|
||||
|
||||
fileprivate var base: Base
|
||||
|
||||
/// Lowercase all coding keys before searching for them in keyed containers such as
|
||||
/// dictionaries or parameters.
|
||||
public static let lowercase = KeyDecodingStrategy(base: .lowercase)
|
||||
}
|
||||
}
|
||||
|
||||
extension StructuredFieldValueDecoder {
|
||||
/// Attempt to decode an object from a structured header field.
|
||||
///
|
||||
/// - 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: StructuredFieldValue, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8 {
|
||||
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.
|
||||
///
|
||||
/// - 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`.
|
||||
private func decodeDictionaryField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8 {
|
||||
let parser = StructuredFieldValueParser(data)
|
||||
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
|
||||
try decoder.parseDictionaryField()
|
||||
return try type.init(from: decoder)
|
||||
}
|
||||
|
||||
/// Attempt to decode an object from a structured header list field.
|
||||
///
|
||||
/// - 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`.
|
||||
private func decodeListField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8 {
|
||||
let parser = StructuredFieldValueParser(data)
|
||||
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
|
||||
try decoder.parseListField()
|
||||
return try type.init(from: decoder)
|
||||
}
|
||||
|
||||
/// Attempt to decode an object from a structured header item field.
|
||||
///
|
||||
/// - 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`.
|
||||
private func decodeItemField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8 {
|
||||
let parser = StructuredFieldValueParser(data)
|
||||
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
|
||||
try decoder.parseItemField()
|
||||
|
||||
// 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 if type is Decimal.Type {
|
||||
let container = try decoder.singleValueContainer()
|
||||
return try container.decode(Decimal.self) as! StructuredField
|
||||
} else {
|
||||
return try type.init(from: decoder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _StructuredFieldDecoder<BaseData: RandomAccessCollection> where BaseData.Element == UInt8 {
|
||||
private var parser: StructuredFieldValueParser<BaseData>
|
||||
|
||||
// For now we use a stack here because the CoW operations on Array would suck. Ideally I'd just have us decode
|
||||
// our way down with values, but doing that is a CoWy nightmare from which we cannot escape.
|
||||
private var _codingStack: [CodingStackEntry]
|
||||
|
||||
var keyDecodingStrategy: StructuredFieldValueDecoder.KeyDecodingStrategy?
|
||||
|
||||
init(_ parser: StructuredFieldValueParser<BaseData>, keyDecodingStrategy: StructuredFieldValueDecoder.KeyDecodingStrategy?) {
|
||||
self.parser = parser
|
||||
self._codingStack = []
|
||||
self.keyDecodingStrategy = keyDecodingStrategy
|
||||
}
|
||||
}
|
||||
|
||||
extension _StructuredFieldDecoder: Decoder {
|
||||
var codingPath: [CodingKey] {
|
||||
self._codingStack.map { $0.key as CodingKey }
|
||||
}
|
||||
|
||||
var userInfo: [CodingUserInfoKey: Any] {
|
||||
[:]
|
||||
}
|
||||
|
||||
func push(_ codingKey: _StructuredHeaderCodingKey) throws {
|
||||
// This force-unwrap is safe: we cannot create containers without having first
|
||||
// produced the base element, which will always be present.
|
||||
let nextElement = try self.currentElement!.innerElement(for: codingKey)
|
||||
self._codingStack.append(CodingStackEntry(key: codingKey, element: nextElement))
|
||||
}
|
||||
|
||||
func pop() {
|
||||
self._codingStack.removeLast()
|
||||
}
|
||||
|
||||
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key: CodingKey {
|
||||
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:
|
||||
// No keyed container for these types.
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
|
||||
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
|
||||
// We have unkeyed containers for lists, inner lists, and bare inner lists.
|
||||
switch self.currentElement! {
|
||||
case .list(let items):
|
||||
return TopLevelListDecoder(items, decoder: self)
|
||||
case .innerList(let innerList):
|
||||
return BareInnerListDecoder(innerList.bareInnerList, decoder: self)
|
||||
case .bareInnerList(let bareInnerList):
|
||||
return BareInnerListDecoder(bareInnerList, decoder: self)
|
||||
case .dictionary, .item, .bareItem, .parameters:
|
||||
// No unkeyed container for these types.
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
|
||||
func singleValueContainer() throws -> SingleValueDecodingContainer {
|
||||
// We have single value containers for items and bareItems.
|
||||
switch self.currentElement! {
|
||||
case .item(let item):
|
||||
return BareItemDecoder(item.bareItem, codingPath: self._codingStack.map { $0.key })
|
||||
case .bareItem(let bareItem):
|
||||
return BareItemDecoder(bareItem, codingPath: self._codingStack.map { $0.key })
|
||||
case .dictionary, .list, .innerList, .bareInnerList, .parameters:
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
|
||||
func parseDictionaryField() throws {
|
||||
precondition(self._codingStack.isEmpty)
|
||||
let parsed = try self.parser.parseDictionaryFieldValue()
|
||||
|
||||
// We unconditionally add to the base of the coding stack here. This element is never popped off.
|
||||
self._codingStack.append(CodingStackEntry(key: .init(stringValue: ""), element: .dictionary(parsed)))
|
||||
}
|
||||
|
||||
func parseListField() throws {
|
||||
precondition(self._codingStack.isEmpty)
|
||||
let parsed = try self.parser.parseListFieldValue()
|
||||
|
||||
// We unconditionally add to the base of the coding stack here. This element is never popped off.
|
||||
self._codingStack.append(CodingStackEntry(key: .init(stringValue: ""), element: .list(parsed)))
|
||||
}
|
||||
|
||||
func parseItemField() throws {
|
||||
precondition(self._codingStack.isEmpty)
|
||||
let parsed = try self.parser.parseItemFieldValue()
|
||||
|
||||
// We unconditionally add to the base of the coding stack here. This element is never popped off.
|
||||
self._codingStack.append(CodingStackEntry(key: .init(stringValue: ""), element: .item(parsed)))
|
||||
}
|
||||
}
|
||||
|
||||
extension _StructuredFieldDecoder {
|
||||
/// The basic elements that make up a Structured Header
|
||||
fileprivate enum Element {
|
||||
case dictionary(OrderedMap<String, ItemOrInnerList>)
|
||||
case list([ItemOrInnerList])
|
||||
case item(Item)
|
||||
case innerList(InnerList)
|
||||
case bareItem(BareItem)
|
||||
case bareInnerList(BareInnerList)
|
||||
case parameters(OrderedMap<String, BareItem>)
|
||||
|
||||
func innerElement(for key: _StructuredHeaderCodingKey) throws -> Element {
|
||||
switch self {
|
||||
case .dictionary(let dictionary):
|
||||
guard let element = dictionary.first(where: { $0.0 == key.stringValue }) else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
switch element.1 {
|
||||
case .item(let item):
|
||||
return .item(item)
|
||||
case .innerList(let innerList):
|
||||
return .innerList(innerList)
|
||||
}
|
||||
case .list(let list):
|
||||
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
|
||||
}
|
||||
case .item(let item):
|
||||
// Two keys, "item" and "parameters".
|
||||
switch key.stringValue {
|
||||
case "item":
|
||||
return .bareItem(item.bareItem)
|
||||
case "parameters":
|
||||
return .parameters(item.parameters)
|
||||
default:
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
case .innerList(let innerList):
|
||||
// Quick check: is this an integer key? If it is, treat this like a bare inner list. Otherwise
|
||||
// there are two string keys: "items" and "parameters"
|
||||
if key.intValue != nil {
|
||||
return try Element.bareInnerList(innerList.bareInnerList).innerElement(for: key)
|
||||
}
|
||||
|
||||
switch key.stringValue {
|
||||
case "items":
|
||||
return .bareInnerList(innerList.bareInnerList)
|
||||
case "parameters":
|
||||
return .parameters(innerList.parameters)
|
||||
default:
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
case .bareItem:
|
||||
// Bare items may never be parsed through.
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
case .bareInnerList(let innerList):
|
||||
guard let offset = key.intValue, offset < innerList.count else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
let index = innerList.index(innerList.startIndex, offsetBy: offset)
|
||||
return .item(innerList[index])
|
||||
case .parameters(let params):
|
||||
guard let element = params.first(where: { $0.0 == key.stringValue }) else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
return .bareItem(element.1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension _StructuredFieldDecoder {
|
||||
/// An entry in the coding stack for _StructuredFieldDecoder.
|
||||
///
|
||||
/// This is used to keep track of where we are in the decode.
|
||||
private struct CodingStackEntry {
|
||||
var key: _StructuredHeaderCodingKey
|
||||
var element: Element
|
||||
}
|
||||
|
||||
/// The element at the current head of the coding stack.
|
||||
private var currentElement: Element? {
|
||||
self._codingStack.last.map { $0.element }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020-2021 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 RawStructuredFieldValues
|
||||
|
||||
struct _StructuredHeaderCodingKey: CodingKey {
|
||||
var stringValue: String
|
||||
var intValue: Int?
|
||||
|
||||
init(stringValue: String) {
|
||||
self.stringValue = stringValue
|
||||
self.intValue = nil
|
||||
}
|
||||
|
||||
init(intValue: Int) {
|
||||
self.stringValue = "\(intValue)"
|
||||
self.intValue = intValue
|
||||
}
|
||||
|
||||
init<Key: CodingKey>(_ key: Key, keyDecodingStrategy: StructuredFieldValueDecoder.KeyDecodingStrategy?) {
|
||||
switch keyDecodingStrategy {
|
||||
case .none:
|
||||
self.stringValue = key.stringValue
|
||||
case .some(.lowercase):
|
||||
self.stringValue = key.stringValue.lowercased()
|
||||
default:
|
||||
preconditionFailure("Invalid key decoding strategy")
|
||||
}
|
||||
|
||||
self.intValue = key.intValue
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2020-2021 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 RawStructuredFieldValues
|
||||
|
||||
struct TopLevelListDecoder<BaseData: RandomAccessCollection> where BaseData.Element == UInt8 {
|
||||
private var list: [ItemOrInnerList]
|
||||
|
||||
private var currentOffset: Int
|
||||
|
||||
private var decoder: _StructuredFieldDecoder<BaseData>
|
||||
|
||||
init(_ list: [ItemOrInnerList], decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
self.list = list
|
||||
self.currentOffset = 0
|
||||
self.decoder = decoder
|
||||
}
|
||||
}
|
||||
|
||||
extension TopLevelListDecoder: UnkeyedDecodingContainer {
|
||||
var codingPath: [CodingKey] {
|
||||
self.decoder.codingPath
|
||||
}
|
||||
|
||||
var count: Int? {
|
||||
self.list.count
|
||||
}
|
||||
|
||||
var isAtEnd: Bool {
|
||||
self.currentOffset == self.list.count
|
||||
}
|
||||
|
||||
var currentIndex: Int {
|
||||
self.currentOffset
|
||||
}
|
||||
|
||||
mutating func decodeNil() throws -> Bool {
|
||||
// We never decode nil.
|
||||
false
|
||||
}
|
||||
|
||||
mutating func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
|
||||
// This is a request to decode a scalar. We decode the next entry and increment the index.
|
||||
guard !self.isAtEnd else {
|
||||
throw StructuredHeaderError.indexOutOfRange
|
||||
}
|
||||
|
||||
let codingKey = _StructuredHeaderCodingKey(intValue: self.currentOffset)
|
||||
defer {
|
||||
self.currentOffset += 1
|
||||
}
|
||||
|
||||
try self.decoder.push(codingKey)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey {
|
||||
// This is a request to decode a full item. We decode the next entry and increment the index.
|
||||
guard !self.isAtEnd else {
|
||||
throw StructuredHeaderError.indexOutOfRange
|
||||
}
|
||||
|
||||
let codingKey = _StructuredHeaderCodingKey(intValue: self.currentOffset)
|
||||
defer {
|
||||
self.currentOffset += 1
|
||||
}
|
||||
|
||||
try self.decoder.push(codingKey)
|
||||
defer {
|
||||
self.decoder.pop()
|
||||
}
|
||||
return try self.decoder.container(keyedBy: type)
|
||||
}
|
||||
|
||||
mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
|
||||
guard !self.isAtEnd else {
|
||||
throw StructuredHeaderError.indexOutOfRange
|
||||
}
|
||||
|
||||
let codingKey = _StructuredHeaderCodingKey(intValue: self.currentOffset)
|
||||
defer {
|
||||
self.currentOffset += 1
|
||||
}
|
||||
|
||||
try self.decoder.push(codingKey)
|
||||
defer {
|
||||
self.decoder.pop()
|
||||
}
|
||||
return try self.decoder.unkeyedContainer()
|
||||
}
|
||||
|
||||
mutating func superDecoder() throws -> Decoder {
|
||||
// No inheritance here folks
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user