mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-29 05:14:21 +00:00
Make dictionary/parameter keys strings too
This commit is contained in:
1 parent
f07f69282d
commit
882cb88c87
8 files changed
+42
-41
No files matched your search
@@ -13,11 +13,11 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
struct DictionaryKeyedContainer<Key: CodingKey, BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
private var dictionary: OrderedMap<BaseData.SubSequence, ItemOrInnerList<BaseData.SubSequence>>
|
||||
private var dictionary: OrderedMap<String, ItemOrInnerList<BaseData.SubSequence>>
|
||||
|
||||
private var decoder: _StructuredFieldDecoder<BaseData>
|
||||
|
||||
init(_ dictionary: OrderedMap<BaseData.SubSequence, ItemOrInnerList<BaseData.SubSequence>>, decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
init(_ dictionary: OrderedMap<String, ItemOrInnerList<BaseData.SubSequence>>, decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
self.dictionary = dictionary
|
||||
self.decoder = decoder
|
||||
}
|
||||
@@ -29,11 +29,11 @@ extension DictionaryKeyedContainer: KeyedDecodingContainerProtocol {
|
||||
}
|
||||
|
||||
var allKeys: [Key] {
|
||||
return self.dictionary.compactMap { Key(stringValue: String(decoding: $0.0, as: UTF8.self)) }
|
||||
return self.dictionary.compactMap { Key(stringValue: $0.0) }
|
||||
}
|
||||
|
||||
func contains(_ key: Key) -> Bool {
|
||||
return self.dictionary.contains(where: { $0.0.elementsEqual(key.stringValue.utf8) })
|
||||
return self.dictionary.contains(where: { $0.0 == key.stringValue })
|
||||
}
|
||||
|
||||
func decodeNil(forKey key: Key) throws -> Bool {
|
||||
|
||||
@@ -13,11 +13,11 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
struct ParametersDecoder<Key: CodingKey, BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
private var parameters: OrderedMap<BaseData.SubSequence, BareItem<BaseData.SubSequence>>
|
||||
private var parameters: OrderedMap<String, BareItem<BaseData.SubSequence>>
|
||||
|
||||
private var decoder: _StructuredFieldDecoder<BaseData>
|
||||
|
||||
init(_ parameters: OrderedMap<BaseData.SubSequence, BareItem<BaseData.SubSequence>>, decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
init(_ parameters: OrderedMap<String, BareItem<BaseData.SubSequence>>, decoder: _StructuredFieldDecoder<BaseData>) {
|
||||
self.parameters = parameters
|
||||
self.decoder = decoder
|
||||
}
|
||||
@@ -29,11 +29,11 @@ extension ParametersDecoder: KeyedDecodingContainerProtocol {
|
||||
}
|
||||
|
||||
var allKeys: [Key] {
|
||||
return self.parameters.compactMap { Key(stringValue: String(decoding: $0.0, as: UTF8.self)) }
|
||||
return self.parameters.compactMap { Key(stringValue: $0.0) }
|
||||
}
|
||||
|
||||
func contains(_ key: Key) -> Bool {
|
||||
return self.parameters.contains(where: { $0.0.elementsEqual(key.stringValue.utf8) })
|
||||
return self.parameters.contains(where: { $0.0 == key.stringValue })
|
||||
}
|
||||
|
||||
func decodeNil(forKey key: Key) throws -> Bool {
|
||||
|
||||
@@ -185,18 +185,18 @@ extension _StructuredFieldDecoder: Decoder {
|
||||
extension _StructuredFieldDecoder {
|
||||
/// The basic elements that make up a Structured Header
|
||||
fileprivate enum Element {
|
||||
case dictionary(OrderedMap<BaseData.SubSequence, ItemOrInnerList<BaseData.SubSequence>>)
|
||||
case dictionary(OrderedMap<String, ItemOrInnerList<BaseData.SubSequence>>)
|
||||
case list([ItemOrInnerList<BaseData.SubSequence>])
|
||||
case item(Item<BaseData.SubSequence>)
|
||||
case innerList(InnerList<BaseData.SubSequence>)
|
||||
case bareItem(BareItem<BaseData.SubSequence>)
|
||||
case bareInnerList(BareInnerList<BaseData.SubSequence>)
|
||||
case parameters(OrderedMap<BaseData.SubSequence, BareItem<BaseData.SubSequence>>)
|
||||
case parameters(OrderedMap<String, BareItem<BaseData.SubSequence>>)
|
||||
|
||||
func innerElement(for key: _StructuredHeaderCodingKey) throws -> Element {
|
||||
switch self {
|
||||
case .dictionary(let dictionary):
|
||||
guard let element = dictionary.first(where: { $0.0.elementsEqual(key.stringValue.utf8) }) else {
|
||||
guard let element = dictionary.first(where: { $0.0 == key.stringValue }) else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
switch element.1 {
|
||||
@@ -251,7 +251,7 @@ extension _StructuredFieldDecoder {
|
||||
let index = innerList.index(innerList.startIndex, offsetBy: offset)
|
||||
return .item(innerList[index])
|
||||
case .parameters(let params):
|
||||
guard let element = params.first(where: { $0.0.elementsEqual(key.stringValue.utf8) }) else {
|
||||
guard let element = params.first(where: { $0.0 == key.stringValue }) else {
|
||||
throw StructuredHeaderError.invalidTypeForItem
|
||||
}
|
||||
return .bareItem(element.1)
|
||||
|
||||
Reference in new issue
Block a user