mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-25 04:47:48 +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
@@ -68,9 +68,9 @@ extension BareItem: Hashable { }
|
||||
// MARK:- Item
|
||||
public struct Item<BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
public var bareItem: BareItem<BaseData>
|
||||
public var parameters: OrderedMap<BaseData, BareItem<BaseData>>
|
||||
public var parameters: OrderedMap<String, BareItem<BaseData>>
|
||||
|
||||
public init(bareItem: BareItem<BaseData>, parameters: OrderedMap<BaseData, BareItem<BaseData>>) {
|
||||
public init(bareItem: BareItem<BaseData>, parameters: OrderedMap<String, BareItem<BaseData>>) {
|
||||
self.bareItem = bareItem
|
||||
self.parameters = parameters
|
||||
}
|
||||
@@ -152,9 +152,9 @@ extension BareInnerList.Index: Comparable {
|
||||
// MARK:- InnerList
|
||||
public struct InnerList<BaseData: RandomAccessCollection>: Hashable where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
public var bareInnerList: BareInnerList<BaseData>
|
||||
public var parameters: OrderedMap<BaseData, BareItem<BaseData>>
|
||||
public var parameters: OrderedMap<String, BareItem<BaseData>>
|
||||
|
||||
public init(bareInnerList: BareInnerList<BaseData>, parameters: OrderedMap<BaseData, BareItem<BaseData>>) {
|
||||
public init(bareInnerList: BareInnerList<BaseData>, parameters: OrderedMap<String, BareItem<BaseData>>) {
|
||||
self.bareInnerList = bareInnerList
|
||||
self.parameters = parameters
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -32,7 +32,7 @@ extension StructuredFieldParser {
|
||||
public typealias BareInnerList = StructuredHeaders.BareInnerList<BaseData.SubSequence>
|
||||
public typealias InnerList = StructuredHeaders.InnerList<BaseData.SubSequence>
|
||||
public typealias ItemOrInnerList = StructuredHeaders.ItemOrInnerList<BaseData.SubSequence>
|
||||
public typealias Key = BaseData.SubSequence
|
||||
public typealias Key = String
|
||||
|
||||
/// Parse the HTTP structured field as a list.
|
||||
///
|
||||
@@ -446,8 +446,8 @@ extension StructuredFieldParser {
|
||||
return .token(String(decoding: tokenSlice, as: UTF8.self))
|
||||
}
|
||||
|
||||
private mutating func _parseParameters() throws -> OrderedMap<BaseData.SubSequence, BareItem> {
|
||||
var parameters = OrderedMap<BaseData.SubSequence, BareItem>()
|
||||
private mutating func _parseParameters() throws -> OrderedMap<Key, BareItem> {
|
||||
var parameters = OrderedMap<Key, BareItem>()
|
||||
|
||||
// We want to loop while we still have bytes _and_ while the first character is asciiSemicolon.
|
||||
// This covers both.
|
||||
@@ -469,7 +469,7 @@ extension StructuredFieldParser {
|
||||
return parameters
|
||||
}
|
||||
|
||||
private mutating func _parseAKey() throws -> BaseData.SubSequence {
|
||||
private mutating func _parseAKey() throws -> Key {
|
||||
guard let first = self.underlyingData.first, asciiLowercases.contains(first) || first == asciiAsterisk else {
|
||||
throw StructuredHeaderError.invalidKey
|
||||
}
|
||||
@@ -483,7 +483,7 @@ extension StructuredFieldParser {
|
||||
}
|
||||
})
|
||||
self.underlyingData = self.underlyingData.dropFirst(key.count)
|
||||
return key
|
||||
return String(decoding: key, as: UTF8.self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ public struct StructuredFieldSerializer {
|
||||
|
||||
extension StructuredFieldSerializer {
|
||||
// TODO: decide if this is the best API, it forces allocations.
|
||||
public mutating func writeDictionaryHeader<BaseData: RandomAccessCollection>(_ root: OrderedMap<BaseData, ItemOrInnerList<BaseData>>) throws -> [UInt8] where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
public mutating func writeDictionaryHeader<BaseData: RandomAccessCollection>(_ root: OrderedMap<String, ItemOrInnerList<BaseData>>) throws -> [UInt8] where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
guard root.count > 0 else {
|
||||
return []
|
||||
}
|
||||
@@ -61,7 +61,7 @@ extension StructuredFieldSerializer {
|
||||
}
|
||||
|
||||
extension StructuredFieldSerializer {
|
||||
private mutating func serializeADictionary<BaseData: RandomAccessCollection>(_ dictionary: OrderedMap<BaseData, ItemOrInnerList<BaseData>>) throws where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
private mutating func serializeADictionary<BaseData: RandomAccessCollection>(_ dictionary: OrderedMap<String, ItemOrInnerList<BaseData>>) throws where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
for (name, value) in dictionary {
|
||||
try self.serializeAKey(name)
|
||||
|
||||
@@ -130,7 +130,7 @@ extension StructuredFieldSerializer {
|
||||
try self.serializeParameters(item.parameters)
|
||||
}
|
||||
|
||||
private mutating func serializeParameters<BaseData: RandomAccessCollection>(_ parameters: OrderedMap<BaseData, BareItem<BaseData>>) throws where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
private mutating func serializeParameters<BaseData: RandomAccessCollection>(_ parameters: OrderedMap<String, BareItem<BaseData>>) throws where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
for (key, value) in parameters {
|
||||
self.data.append(asciiSemicolon)
|
||||
try self.serializeAKey(key)
|
||||
@@ -145,10 +145,10 @@ extension StructuredFieldSerializer {
|
||||
}
|
||||
}
|
||||
|
||||
private mutating func serializeAKey<BaseData: RandomAccessCollection>(_ key: BaseData) throws where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
private mutating func serializeAKey(_ key: String) throws {
|
||||
// We touch each byte twice here, but that's ok: this is cache friendly and less branchy (the copy gets to be memcpy in some cases!)
|
||||
try key.validateStructuredHeaderKey()
|
||||
self.data.append(contentsOf: key)
|
||||
self.data.append(contentsOf: key.utf8)
|
||||
}
|
||||
|
||||
private mutating func serializeABareItem<BaseData: RandomAccessCollection>(_ item: BareItem<BaseData>) throws where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
@@ -193,9 +193,10 @@ extension StructuredFieldSerializer {
|
||||
}
|
||||
}
|
||||
|
||||
extension RandomAccessCollection where Element == UInt8 {
|
||||
extension String {
|
||||
func validateStructuredHeaderKey() throws {
|
||||
if let firstByte = self.first {
|
||||
let utf8View = self.utf8
|
||||
if let firstByte = utf8View.first {
|
||||
switch firstByte {
|
||||
case asciiLowercases, asciiAsterisk:
|
||||
// Good
|
||||
@@ -205,7 +206,7 @@ extension RandomAccessCollection where Element == UInt8 {
|
||||
}
|
||||
}
|
||||
|
||||
let validKey = self.dropFirst().allSatisfy {
|
||||
let validKey = utf8View.dropFirst().allSatisfy {
|
||||
switch $0 {
|
||||
case asciiLowercases, asciiDigits, asciiUnderscore,
|
||||
asciiDash, asciiPeriod, asciiAsterisk:
|
||||
|
||||
@@ -21,7 +21,7 @@ enum FixtureTestError: Error {
|
||||
|
||||
final class StructuredFieldParserTests: XCTestCase {
|
||||
enum TestResult<BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
case dictionary(OrderedMap<BaseData, ItemOrInnerList<BaseData>>)
|
||||
case dictionary(OrderedMap<String, ItemOrInnerList<BaseData>>)
|
||||
case list([ItemOrInnerList<BaseData>])
|
||||
case item(Item<BaseData>)
|
||||
}
|
||||
@@ -65,15 +65,15 @@ final class StructuredFieldParserTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
private func _validateParameters<BaseData: RandomAccessCollection>(_ parameters: OrderedMap<BaseData, BareItem<BaseData>>, against schema: JSONSchema, fixtureName: String) throws where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
private func _validateParameters<BaseData: RandomAccessCollection>(_ parameters: OrderedMap<String, BareItem<BaseData>>, against schema: JSONSchema, fixtureName: String) throws where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
guard case .dictionary(let expectedParameters) = schema else {
|
||||
XCTFail("\(fixtureName): Expected parameters to be a JSON dictionary, but got \(schema)")
|
||||
return
|
||||
}
|
||||
XCTAssertEqual(expectedParameters.count, parameters.count, "\(fixtureName): Different numbers of parameters: expected \(expectedParameters), got \(parameters)")
|
||||
for (name, value) in parameters {
|
||||
guard let expectedValue = expectedParameters[String(decoding: name, as: UTF8.self)] else {
|
||||
XCTFail("\(fixtureName): Did not contain parameter for \(String(decoding: name, as: UTF8.self))")
|
||||
guard let expectedValue = expectedParameters[name] else {
|
||||
XCTFail("\(fixtureName): Did not contain parameter for \(name)")
|
||||
return
|
||||
}
|
||||
try self._validateBareItem(value, against: expectedValue, fixtureName: fixtureName)
|
||||
@@ -128,7 +128,7 @@ final class StructuredFieldParserTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
private func _validateDictionary<BaseData: RandomAccessCollection>(_ result: OrderedMap<BaseData, ItemOrInnerList<BaseData>>, against schema: JSONSchema, fixtureName: String) throws where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
private func _validateDictionary<BaseData: RandomAccessCollection>(_ result: OrderedMap<String, ItemOrInnerList<BaseData>>, against schema: JSONSchema, fixtureName: String) throws where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
guard case .dictionary(let expectedElements) = schema else {
|
||||
XCTFail("\(fixtureName): Unexpected dictionary: got \(result), expected \(schema)")
|
||||
return
|
||||
@@ -136,8 +136,8 @@ final class StructuredFieldParserTests: XCTestCase {
|
||||
|
||||
XCTAssertEqual(expectedElements.count, result.count, "\(fixtureName): Different counts in dictionary: got \(result), expected \(expectedElements)")
|
||||
for (key, value) in result {
|
||||
guard let expectedEntry = expectedElements[String(decoding: key, as: UTF8.self)] else {
|
||||
XCTFail("\(fixtureName): Could not find \(String(decoding: key, as: UTF8.self)) in \(expectedElements)")
|
||||
guard let expectedEntry = expectedElements[key] else {
|
||||
XCTFail("\(fixtureName): Could not find \(key) in \(expectedElements)")
|
||||
return
|
||||
}
|
||||
switch value {
|
||||
|
||||
@@ -17,7 +17,7 @@ import StructuredHeaders
|
||||
|
||||
final class StructuredFieldSerializerTests: XCTestCase {
|
||||
enum TestResult<BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
|
||||
case dictionary(OrderedMap<BaseData, ItemOrInnerList<BaseData>>)
|
||||
case dictionary(OrderedMap<String, ItemOrInnerList<BaseData>>)
|
||||
case list([ItemOrInnerList<BaseData>])
|
||||
case item(Item<BaseData>)
|
||||
}
|
||||
@@ -132,10 +132,10 @@ extension StructuredFieldSerializerTests.TestResult where BaseData == ArraySlice
|
||||
switch schema {
|
||||
case .dictionary(let dictionary):
|
||||
// Top level JSON objects are encoding dictionaries.
|
||||
var dict = OrderedMap<BaseData, ItemOrInnerList<BaseData>>()
|
||||
var dict = OrderedMap<String, ItemOrInnerList<BaseData>>()
|
||||
|
||||
for (name, value) in dictionary {
|
||||
dict[ArraySlice(name.utf8)] = try ItemOrInnerList(value)
|
||||
dict[name] = try ItemOrInnerList(value)
|
||||
}
|
||||
|
||||
self = .dictionary(dict)
|
||||
@@ -234,7 +234,7 @@ extension BareInnerList where BaseData == ArraySlice<UInt8> {
|
||||
}
|
||||
}
|
||||
|
||||
extension OrderedMap where Key == ArraySlice<UInt8>, Value == BareItem<ArraySlice<UInt8>> {
|
||||
extension OrderedMap where Key == String, Value == BareItem<ArraySlice<UInt8>> {
|
||||
init(parameters: JSONSchema) throws {
|
||||
guard case .dictionary(let jsonDict) = parameters else {
|
||||
fatalError("Invalid format for parameters: \(parameters)")
|
||||
@@ -243,7 +243,7 @@ extension OrderedMap where Key == ArraySlice<UInt8>, Value == BareItem<ArraySlic
|
||||
self.init()
|
||||
|
||||
for (name, value) in jsonDict {
|
||||
self[ArraySlice(name.utf8)] = try BareItem(value)
|
||||
self[name] = try BareItem(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user