Use a higher-level protocol for encoding/decoding.

Motivation:

Right now we disambiguate the multiple top-level SH types by using three
separate entry points on the encoder/decoder. This isn't great: it
forces that information to be known statically at compile time, making
it much harder to write generic APIs that use structured header fields.

Modifications:

- Provide a new protocol, `StructuredHeaderField`, that must be used for
  the top-level `Codable` object.
- Provide `StructuredHeaderFieldType` to communicate the types of
  structured header fields.
- Remove the multiple entry points.
- Update the tests.

Result:

Types will encode what kind of structured header field they are.
This commit is contained in:
Cory Benfield committed 2020-07-27 08:07:46 +01:00
1 parent 101dec0a99
commit 246d886cf7
4 files changed
+233 -143

No files matched your search

@@ -38,13 +38,30 @@ extension StructuredFieldEncoder {
}
extension StructuredFieldEncoder {
/// Attempt to encode an object into a structured header field.
///
/// - parameters:
/// - data: The object to encode.
/// - throws: If the header field could not be encoded, or could not be serialized.
/// - returns: The bytes representing the HTTP structured header field.
public func encode<StructuredField: StructuredHeaderField>(_ data: StructuredField) throws -> [UInt8] {
switch StructuredField.structuredFieldType {
case .item:
return try self.encodeItemField(data)
case .list:
return try self.encodeListField(data)
case .dictionary:
return try self.encodeDictionaryField(data)
}
}
/// Attempt to encode an object into a structured header dictionary field.
///
/// - parameters:
/// - data: The object to encode.
/// - throws: If the header field could not be encoded, or could not be serialized.
/// - returns: The bytes representing the HTTP structured header field.
public func encodeDictionaryField<StructuredField: Encodable>(_ data: StructuredField) throws -> [UInt8] {
private func encodeDictionaryField<StructuredField: Encodable>(_ data: StructuredField) throws -> [UInt8] {
let serializer = StructuredFieldSerializer()
let encoder = _StructuredFieldEncoder(serializer, keyEncodingStrategy: self.keyEncodingStrategy)
return try encoder.encodeDictionaryField(data)
@@ -56,7 +73,7 @@ extension StructuredFieldEncoder {
/// - data: The object to encode.
/// - throws: If the header field could not be encoded, or could not be serialized.
/// - returns: The bytes representing the HTTP structured header field.
public func encodeListField<StructuredField: Encodable>(_ data: StructuredField) throws -> [UInt8] {
private func encodeListField<StructuredField: Encodable>(_ data: StructuredField) throws -> [UInt8] {
let serializer = StructuredFieldSerializer()
let encoder = _StructuredFieldEncoder(serializer, keyEncodingStrategy: self.keyEncodingStrategy)
return try encoder.encodeListField(data)
@@ -68,7 +85,7 @@ extension StructuredFieldEncoder {
/// - data: The object to encode.
/// - throws: If the header field could not be encoded, or could not be serialized.
/// - returns: The bytes representing the HTTP structured header field.
public func encodeItemField<StructuredField: Encodable>(_ data: StructuredField) throws -> [UInt8] {
private func encodeItemField<StructuredField: Encodable>(_ data: StructuredField) throws -> [UInt8] {
let serializer = StructuredFieldSerializer()
let encoder = _StructuredFieldEncoder(serializer, keyEncodingStrategy: self.keyEncodingStrategy)
return try encoder.encodeItemField(data)
@@ -635,7 +652,7 @@ extension _StructuredFieldEncoder {
try self.encode(value, forKey: key)
default:
// Ok, we don't know what this is. This can only happen for a dictionary, or
// for anything with parameters, or for inner lists.
// for anything with parameters, or for lists, or for inner lists.
switch self.currentStackEntry.storage {
case .dictionaryHeader:
// Ah, this is a dictionary, good to know. Initialize the storage, keep going.
@@ -686,7 +703,18 @@ extension _StructuredFieldEncoder {
throw StructuredHeaderError.invalidTypeForItem
}
case .listHeader, .list, .itemHeader, .bareInnerList,
case .listHeader:
switch key {
case "items":
// Ok, we're a list. Good to know.
self.push(key: .init(stringValue: key), newStorage: .list([]))
try value.encode(to: self)
try self.pop()
default:
throw StructuredHeaderError.invalidTypeForItem
}
case .list, .itemHeader, .bareInnerList,
.parameters:
throw StructuredHeaderError.invalidTypeForItem
}
@@ -794,6 +822,9 @@ extension _StructuredFieldEncoder {
map[key.stringValue] = .item(Item(item))
self = .dictionary(map)
case (.listHeader, .list(let list)) where key.stringValue == "items":
self = .list(list)
case (.listHeader, .innerList(let innerList)) where key.intValue != nil:
self = .list([.innerList(innerList)])