One thought here: if this does get expressed in a generic way at a top-level context, I think it would be nice to spell the method as

static func decode<T: Decodable>(_ type: T.Type, from data: Data, at codingKeyPath: [CodingKey])

to allow key paths to include keys which have a delimiter in them (e.g. "my.key", which would otherwise be parsed as ["my", "key"]), as well as integer keys.

It's then possible to also offer

static func decode<T: Decodable>(_ type: T.Type, from data: Data, at codingKeyPath: String, separator: String = ".")

with a default implementation which splits codingKeyPath on the delimiter, turns each component into a CodingKey, then calls the [CodingKey] variant. JSONDecoder (and others) could also override this implementation to do something even more efficient if relevant.

(The [CodingKey] version can also made significantly easier to call using a general key type, e.g. AnyCodingKey pitched in [Pitch] Allow coding of non-`String`/`Int` keyed `Dictionary` into a `KeyedContainer`)

4 Likes