Skip to content

Latest commit

 

History

History
89 lines (87 loc) · 1.79 KB

README.md

File metadata and controls

89 lines (87 loc) · 1.79 KB

CodingKeysMacro

Swift Macro that automatically generates CodingKeys for converting snake_case to lowerCamelCase.

Installation

.package(url: "https://github.com/Ryu0118/CodingKeysMacro", branch: "main")

Usage

.all

@CodingKeys(.all)
struct UserResponse: Codable {
    let id: String
    let age: Int
    let userName: String
    let userDescription: String
}
// expanded to...
struct UserResponse: Codable {
    ...
    enum CodingKeys: String, CodingKey {
        case id
        case age
        case userName = "user_name"
        case userDescription = "user_description"
    }
}

.select

@CodingKeys(.select(["userName"]))
struct UserResponse: Codable {
    let id: String
    let age: Int
    let userName: String
    let userDescription: String
}
// expanded to...
struct UserResponse: Codable {
    ...
    enum CodingKeys: String, CodingKey {
        case id
        case age
        case userName = "user_name"
        case userDescription
    }
}

.exclude

@CodingKeys(.exclude(["userName", "userDescription"]))
struct UserResponse: Codable {
    let id: String
    let age: Int
    let userName: String
    let userDescription: String
}
// expanded to...
struct UserResponse: Codable {
    ...
    enum CodingKeys: String, CodingKey {
        case id
        case age
        case userName
        case userDescription
    }
}

.custom

@CodingKeys(.custom(["id": "user_id"]))
struct UserResponse: Codable {
    let id: String
    let age: Int
    let userName: String
    let userDescription: String
}
// expanded to...
struct UserResponse: Codable {
    ...
    enum CodingKeys: String, CodingKey {
        case id = "user_id"
        case age
        case userName = "user_name"
        case userDescription = "user_description"
    }
}