CodingKeysMacro

0.0.1

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

What's New

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"
    }
}

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Sun Oct 20 2024 18:04:41 GMT-0900 (Hawaii-Aleutian Daylight Time)