Get the coding key for a keypath in Swift

1.4k Views Asked by At

What I have: A codable struct with different properties.

What I want: A function, where I can get the exact name of the property when it is encoded in a Json. I think the most promising approach is using Keypath, but I have no idea how and whether it is possible at all. Thanks!

1

There are 1 best solutions below

3
On

There is no way to do this out of the box, since there's no 1-1 mapping between properties of a Codable type and its coding keys, since there might be properties that aren't part of the encoded model or properties that depend on several encoded keys.

However, you should be able to achieve your goals by defining a mapping between the properties and their coding keys. You were on the right track with KeyPaths, you just need to define a function that takes a KeyPath whose root type is your codable model and return the coding key from said function.

struct MyCodable: Codable {
    let id: Int
    let name: String

    // This property isn't part of the JSON
    var description: String {
        "\(id) \(name)"
    }

    enum CodingKeys: String, CodingKey {
        case name = "Name"
        case id = "identifier"
    }

    static func codingKey<Value>(for keyPath: KeyPath<MyCodable, Value>) -> String? {
        let codingKey: CodingKeys
        switch keyPath {
        case \MyCodable.id:
            codingKey = .id
        case \MyCodable.name:
            codingKey = .name
        default: // handle properties that aren't encoded
            return nil
        }
        return codingKey.rawValue
    }
}

MyCodable.codingKey(for: \.id)