@Pampel You might want to look at KeyPath-to-String conversion performed by GitHub - vapor/core: 🌎 Utility package containing tools for byte manipulation, Codable, OS APIs, and debugging.. See for example https://github.com/vapor/core/blob/master/Tests/CoreTests/ReflectableTests.swift
Now, personally, as the author of GRDB (an SQLite database library that quite a few users love to use), I have never wanted to provide KeyPath-to-column automatic conversion.
The reason is that I think the intimate details of the relationship between a record type and the database should remain private. Column names are such details. The fact that the synthesized CodingKeys are private as well gives a good precedent.
By not relying on key paths, you can encapsulate your record exactly how you need it.
For example, the record below hides its latitude/longitude. There is no available key path which can talk to those database columns.
struct Place: Codable {
var id: UUID
var title: String
private var latitude: CLLocationDegrees
private var longitude: CLLocationDegrees
var coordinate: CLLocationCoordinate2D {
get { ... }
set { ... }
}
}
This ability to cleanly distinguish the private inner details and the internal/public facet of your records, the ability to refactor and migrate your database with minimal-to-zero impact on clients of those types, the ability to have complex records behave just the same as simple trivial ones, those are advantages that would be instantly ruined if key paths were publicly fostered as column proxies.
When such guts are unfortunately exposed, and when you realize that you really need to hide those guts because they are impractical to work with, you have to build a second layer of models, that wrap the first ones. Not only is this second layer a chore to build, but it is likely that not all of your records need one. You end up with an inconsistent database facade, with a mix of low-level and high-level types, without any clear reason why this mess has started. As a matter of fact, it's pretty clear: that's because of the KeyPath-to-string "convenience" conversion