Lately I have been working on an ASN.1 Codable
implementation (link here). Without going into the boring details of ASN.1, its type system includes additional metadata beyond that which one would require for encoding, say, JSON – for example, fields can have a tag associated with them (not unlike XML, I suppose).
PotentCodables solves this by expressing the ASN.1 syntax tree in a DSL and requiring the user to attach that when encoding or decoding. My approach has been to attempt to shoe-horn the ASN.1 type system into Swift, using property wrappers specialised with placeholder types representing the ASN.1 metadata sprinkled with some generous reflection. It has the advantage of only needing to define types in one place, making Swift types more or less isomorphic to ASN.1 types.
For example, the ASN.1 type:
AuthorityKeyIdentifier ::= SEQUENCE {
keyIdentifier [0] IMPLICIT OCTET STRING OPTIONAL,
authorityCertIssuer [1] IMPLICIT -- GeneralName --
SEQUENCE -- SIZE (1..MAX) -- OF GeneralName OPTIONAL,
authorityCertSerialNumber [2] IMPLICIT INTEGER OPTIONAL
}
is expressed as:
struct AuthorityKeyIdentifier: Codable {
enum CodingKeys: String, CodingKey {
case keyIdentifier
case authorityCertIssuer
case authorityCertSerialNumber
}
@ASN1ContextTagged<ASN1TagNumber$0, ASN1ImplicitTagging, Data?>
var keyIdentifier: Data? = nil
@ASN1ContextTagged<ASN1TagNumber$1, ASN1ImplicitTagging, Array<GeneralName>?>
var authorityCertIssuer: Array<GeneralName>? = nil
@ASN1ContextTagged<ASN1TagNumber$2, ASN1ImplicitTagging, Int?>
var authorityCertSerialNumber: Int? = nil
}
However, this approach has some ergonomic rough edges: namely, property wrappers can't be used everywhere (such as in enum associated values), and nesting property wrappers or wrapping optionals can be cumbersome. Ideally the library consumer never needs to explicitly unwrap a property wrapper.
I was wondering: are there any plans to make property wrappers more useful, in more places, or perhaps to introduce a generic type annotation system that can be interrogated at runtime? I realise that trying to shoe-horn one type system into another may be a fool's errand!