mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-24 04:37:50 +00:00
Motivation: Early on in the design of Structured Headers I made a conscious effort to try to avoid transforming types in the low-level API. This design was built on the assumption that strings, tokens, and some other data types could be built entirely as slices of the backing data. Unfortunately, this isn't true: because of the need to escape chars in Strings, it became necessary to start using more complex types in Item. Eventually, we ended up in a place where the only field in Item that actually stored backing data was `.undecodedByteSequence`. This field forced almost the entire set of types in the StructuredHeaders module to be generic, just for the sake of carrying this one slice type around. This prevented powerful optimisations, bloated code size, and forced additional unnecessary heap allocations. It also forced us to constrain the types used in the parser to those where the subsequence type was Hashable, which excluded some really important types (like `String.UTF8View`). Fundamentally, this genericism was hurting more than it helped at this point, so I bit the bullet and replaced the slice type with `String` for `undecodedByteSequence`. This led to a huge cascading removal of generic type parameters for a wide range of types, as well as the removal of the "must have hashable subsequence" constraints on the parser data type. This lifts an enormous amount of complexity out of the module at the extremely minor cost of forcing an allocation whenever we have a lot of undecoded binary data: not a huge cost, in my view, and well worth what we got back in return. Modifications: - Remove generic type parameter in BaseItem. - Remove generic types everywhere else. - Remove hashable constraint. Result: Better, faster, simpler code.
89 lines
3.1 KiB
Swift
89 lines
3.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
|
|
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
|
|
}
|
|
}
|