Skip to content

About

Swift macro to generate a mutable version of an otherwise immutable value types.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

Draftable

Swift Platforms SwiftPM

A Swift macro that generates a mutable, all-optional Draft companion for a value type. Fill it in incrementally, then call validated() to get the complete value back once every required field is set.

Usage

import Draftable

@Draftable
struct User: Equatable {
    var id: Int
    var name: String
    var email: String?
    var roles: [String] = []
}

var draft = User.Draft()
draft.name = "Ada"
draft.validated()   // nil, id is still missing

draft.id = 42
draft.validated()   // User(id: 42, name: "Ada", email: nil, roles: [])

@Draftable adds a nested Draft where required (non-optional, no default) properties become optional, while optional and defaulted properties are carried through unchanged. validated() returns nil until every required field is present, then rebuilds the value through its memberwise initializer. It applies to structs only.

Installation

dependencies: [
    .package(url: "https://github.com/ramikay/swift-draftable-macro.git", from: "1.0.0"),
]

Then add Draftable to your target's dependencies.

Requirements

Swift 6.0+ (Xcode 16+).

About

Swift macro to generate a mutable version of an otherwise immutable value types.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages