mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-23 04:34:21 +00:00
Motivation: `swift-http-structured-headers` has to be updated to [RFC 9651](https://www.ietf.org/rfc/rfc9651.html). And it will be convenient for users to have this update as a SemVer minor. Modifications: - Deprecate the `BareItem` type and replace it with `RFC9651BareItem`, retaining existing behaviours. Updates for the new RFC will be based on `RFC9651BareItem`. Result: The library can be updated to the new RFC in a backward-compatible manner.
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-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, RFC9651BareItem>
|
|
|
|
private var decoder: _StructuredFieldDecoder<BaseData>
|
|
|
|
init(_ parameters: OrderedMap<String, RFC9651BareItem>, 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
|
|
}
|
|
}
|