Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

zijievv/CodingKeysGenerator

Open more actions menu

Repository files navigation

CodingKeysGenerator

Swift macros generating customizable CodingKeys

Declaration

public enum CodingKeyStyle {
    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")

Usage

Source code:

@CodingKeys(style: .snakeCased) // default style is `snakeCased`
struct Entity {
    @CodingKey(custom: "entity_id")
    let id: String
    let currentValue: Int
    let count: Int
    let `protocol`: String
    @CodingKeyIgnored
    let foo: Bool
}

Expanded source:

struct Entity {
    let id: String
    let currentValue: Int
    let count: Int
    let `protocol`: String
    let foo: Bool
    enum CodingKeys: String, CodingKey {
        case id = "entity_id"
        case currentValue = "current_value"
        case count
        case `protocol`
    }
}

Installation

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.

Xcode

Go to File > Add Package Dependencies... and paste the repo's URL:

https://github.com/zijievv/CodingKeysGenerator

About

Swift macros generating customizable Coding Keys

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.