Swift macro to add CodingKeys to structs with supports for setting custom values for CodingKey keys.
.package(url: "https://github.com/saeid/CodingKeyGeneratorMacro", branch: "main")
For providing custom keys, the goal is to support KeyPath to provide a type-safe api but there are issues with this approach. see limitation section.
The implementation for this is in branch keypath.
@CodingKeyGen([
"name": "userValue",
"id": "userid",
"userName": "new_user"
])
public struct MyStruct {
let name: String
let id: Int
let userName: String
}
// Will be expanded to:
public struct MyStruct {
let name: String
let id: Int
let userName: String
enum CodingKeys: String, CodingKey {
case name = "userValue"
case id = "userid"
case userName = "new_user"
}
}
@CodingKeyGen()
public struct MyCodableStruct: Codable {
let name: String
let id: Int
let userName: String
}
// Will be expanded to:
public struct MyCodableStruct: Codable {
let name: String
let id: Int
let userName: String
enum CodingKeys: String, CodingKey {
case name
case id
case userName
}
}
In current state @attached macros have bug/limitation which doesn't allow to use a reference of the type in macro's argument list. It results to Circular reference resolving attached macro error.
CodingKeyGeneratorMacro is available under the MIT license. See the LICENSE.txt file for more info.