mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-23 04:34:21 +00:00
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.
94 lines
3.3 KiB
Swift
94 lines
3.3 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
|
|
|
|
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
|
|
}
|
|
}
|