You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
let parsed =try decoder.decodeListField([String].self, from: field)
52
+
let parsed =try decoder.decode(AcceptCH.self, from: field)
47
53
48
54
// Encoding
49
55
let encoder =StructuredFieldEncoder()
50
-
let serialized =try encoder.encodeListField(["Sec-CH-Example", "Sec-CH-Example-2"])
56
+
let serialized =try encoder.encode(AcceptCH(items: ["Sec-CH-Example", "Sec-CH-Example-2"]))
51
57
```
52
58
53
59
However, structured header fields can be substantially more complex. Structured header fields can make use of 4 containers and 6 base item types. The containers are:
54
60
55
-
1. Dictionaries. These are top-level elements and associate token keys with values. The values may be items, or may be inner lists, and each value may also have parameters associated with them. `CodableStructuredHeaders` can model dictionaries as either Swift objects (where the property names are dictionary keys), or as Swift dictionaries.
56
-
2. Lists. These are top-level elements, providing a sequence of items or inner lists. Each item or inner list may have parameters associated with them. `CodableStructuredHeaders` models these as Swift `Array`s where the entries are items of some kind.
61
+
1. Dictionaries. These are top-level elements and associate token keys with values. The values may be items, or may be inner lists, and each value may also have parameters associated with them. `CodableStructuredHeaders` can model dictionaries as either Swift objects (where the property names are dictionary keys).
62
+
2. Lists. These are top-level elements, providing a sequence of items or inner lists. Each item or inner list may have parameters associated with them. `CodableStructuredHeaders` models these as Swift objects with one key, `items`, that must be a collection of entries.
57
63
3. Inner Lists. These are lists that may be sub-entries of a dictionary or a list. The list entries are items, which may have parameters associated with them: additionally, an inner list may have parameters associated with itself as well. `CodableStructuredHeaders` models these as either Swift `Array`s _or_, if it's important to extract parameters, as a two-field Swift `struct` where one field is called `items` and contains an `Array`, and other field is called `parameters` and contains a dictionary.
58
64
4. Parameters. Parameters associated token keys with items without parameters. These are used to store metadata about objects within a field. `CodableStructuredHeaders` models these as either Swift objects (where the property names are the parameter keys) or as Swift dictionaries.
59
65
@@ -68,6 +74,8 @@ The base types are:
68
74
69
75
For any Structured Header Field Item, the item may either be represented directly by the appropriate type, or by a Swift struct with two properties: `item` and `parameters`. This latter mode is how parameters on a given item may be captured.
70
76
77
+
The top-level structured header field must identify what kind of header field it corresponds to: `.item`, `.list`, or `.dictionary`. This is inherent in the type of the field and will be specified in the relevant field specification.
78
+
71
79
## Lower Levels
72
80
73
81
In some cases the Codable interface will not be either performant enough or powerful enough for the intended use-case. In cases like this, users can use the types in the `StructuredHeaders` module instead.
/// An example ListyDictionary structured header field.
59
79
///
60
80
/// An example of this field is: 'primary=bar;q=1.0, secondary=baz;q=0.5;fallback=last, acceptablejurisdictions=(AU;q=1.0 GB;q=0.9 FR);fallback=primary'
@@ -95,16 +115,16 @@ final class StructuredFieldDecoderTests: XCTestCase {
0 commit comments