AutoCodingKeysMacro
is a Swift macro that automatically generates CodingKeys
enums for Codable
structs. It allows you to convert property names from camelCase to snake_case (or another specified case) automatically, with options for custom mappings of specific keys.
To use AutoCodingKeysMacro
in your project, add it as a Swift Package dependency.
- Open your project in Xcode.
- Go to
File
>Add Packages...
. - Enter the following Git URL for the package:
https://github.com/alimadhoun/AutoCodingKeys.git
To use AutoCodingKeysMacro
, simply add the @AutoCodingKeys
attribute to any Codable
struct. This will automatically generate a CodingKeys
enum, mapping struct properties to a specified case style (snake_case by default).
Here's a quick example:
import AutoCodingKeysMacro
@AutoCodingKeys
struct Person: Codable {
let firstName: String
let lastName: String
let age: Int
}
After applying the @AutoCodingKeys macro, the code will expand to:
struct Person: Codable {
let firstName: String
let lastName: String
let age: Int
enum CodingKeys: String, CodingKey {
case firstName = "first_name"
case lastName = "last_name"
case age = "age"
}
}
You can also specify custom keys for certain properties by passing a dictionary to the @AutoCodingKeys
macro. For instance, if a property’s JSON key is a reserved keyword or has a different naming convention, you can set a custom mapping.
@AutoCodingKeys(customKeys: ["productDescription": "description"])
struct Product: Codable {
let productName: String
let productDescription: String
let price: Double
}
The macro will generate:
@AutoCodingKeys(customKeys: ["productDescription": "description"])
struct Product: Codable {
let productName: String
let productDescription: String
let price: Double
enum CodingKeys: String, CodingKey {
case productName = "product_name"
case productDescription = "description"
case price = "price"
}
}
You can also specify the case style for all keys in the struct. By default, the macro uses snake_case, but you can set it to camelCase if needed. Example
@AutoCodingKeys(keyCase: .camelCase)
struct Person: Codable {
let first_name: String
let last_name: String
let age: Int
}
With keyCase
set to .camelCase
, the generated CodingKeys enum will use camelCase:
struct Person: Codable {
let first_name: String
let last_name: String
let age: Int
enum CodingKeys: String, CodingKey {
case first_name = "firstName"
case last_name = "lastName"
case age
}
}
Supported Key Cases
Currently, AutoCodingKeysMacr
o supports the following case styles:
.snakeCase
(default).camelCase
This project is licensed under the MIT License.