Skip to content

ramikay/swift-draftable-macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

Watchers

Forks

Releases

Packages

Contributors

Languages