Swift macros generating customizable CodingKeys
publicenumCodingKeyStyle{case camelCased
case kebabCased
case snakeCased
}@attached(member, names:named(CodingKeys))public macro CodingKeys(style:CodingKeyStyle=.snakeCased)= #externalMacro(module:"CodingKeysGeneratorMacros", type:"CodingKeysMacro")@attached(peer)public macro CodingKey(custom:String)= #externalMacro(module:"CodingKeysGeneratorMacros", type:"CustomCodingKeyMacro")@attached(peer)public macro CodingKeyIgnored()= #externalMacro(module:"CodingKeysGeneratorMacros", type:"CodingKeyIgnoredMacro")Source code:
@CodingKeys(style:.snakeCased) // default style is `snakeCased`
structEntity{@CodingKey(custom:"entity_id")letid:StringletcurrentValue:Intletcount:Intlet`protocol`:String@CodingKeyIgnoredletfoo:Bool}Expanded source:
structEntity{letid:StringletcurrentValue:Intletcount:Intlet`protocol`:Stringletfoo:BoolenumCodingKeys:String,CodingKey{case id ="entity_id"case currentValue ="current_value"case count
case `protocol`
}}Swift Package Manager (SPM)
Add the following line to the dependencies in Package.swift, to use CodingKeysGenerator macros in a SPM project:
.package(url:"https://github.com/zijievv/CodingKeysGenerator", from:"0.1.0"),In your target:
.target(name:"<TARGET_NAME>", dependencies:[.product(name:"CodingKeysGenerator",package:"CodingKeysGenerator"),
// ...
]),Add import CodingKeysGenerator into your source code to use CodingKeysGenerator macros.
Go to File > Add Package Dependencies... and paste the repo's URL:
https://github.com/zijievv/CodingKeysGenerator