BuildableMacro is a Swift package inspired by Apple's SwiftUI and Observation frameworks, simplifying property modification in Swift through automated fluent setter function generation.
BuildableMacro is a Swift package designed to enhance the way properties are modified and managed in Swift. Drawing inspiration from Apple's Observation framework, it introduces a set of macros that streamline the property modification process, making it more intuitive and fluent. The package's main feature is its ability to automatically generate setter functions for properties, following a pattern similar to SwiftUI's modifier approach.
This approach not only makes the code more readable but also maintains the familiarity for Swift developers accustomed to the SwiftUI framework.
The package includes @Buildable
, @BuildableTracked
, and @BuildableIgnored
macros, each serving a specific purpose in the property modification workflow. @BuildableTracked
, for example, creates fluent setter functions, which can be customized with desired names, whereas @BuildableIgnored
excludes certain properties from this automated process, providing developers with control over their code structure and behavior.
Automates the marking of settable properties as @BuildableTracked
, excluding those marked as @BuildableIgnored
.
The @Buildable
macro selectively applies @BuildableTracked
to eligible properties. It skips constants, computed properties without setters, and those marked as @BuildableIgnored
, ensuring only suitable properties are modified.
Generates setter functions for each marked property, allowing fluent chaining of modifications.
In @BuildableTracked
, a custom name for the generated setter function can be specified through the name
parameter. This allows for more descriptive and context-specific naming of setter functions, enhancing code clarity and expressiveness.
Although @BuildableTracked
automatically recognizes function types and adds @escaping
attribute to the generated functions, the forceEscaping
parameter ensures correct handling of type aliases for function types. It marks parameters as @escaping
when needed, ensuring closure and callback patterns are treated accurately, crucial for asynchronous or callback-based code structures.
Excludes properties from automatic setter function generation.
Apply the @Buildable
macro to a type (class, struct, or actor) to automatically generate fluent setter functions for its properties. Customize the behavior using @BuildableTracked
and @BuildableIgnored
macros.
@Buildable
struct Team {
@BuildableIgnored
var name: String
var nickname: String?
@BuildableTracked(name: "updateSponsor")
var sponser: Sponser?
var owner: Person
var players: [Person]
var coach: Person
}
// Old AFC Richmond team:
let afcRichmond = Team(
name: "AFC Richmond"
nickname: "The Greyhounds"
sponser: "Dubai Air"
owner: Person(name: "Rupert Mannion")
players: [...]
coach: Person(name: "George Cartrick")
)
// Change some properties easily by calling generated setter functions to assign new values. (Similar to SwiftUI)
let newAFCRichmond = afcRichmond
.owner(Person(name: "Rebecca Welton"))
.coach(Person(name: "Ted Lasso"))
.updateSponsor(Sponsor(name: "Bantr")) // -> function name is customized by setting`name` property in @BuildableTracked() macro.
// .name("Crystal Palace") -> This cannot be called since the related property, "name", is ignored and no setter is created for it.
BuildableMacro is available through Swift Package Manager. To install it, simply add the package to the dependencies of your Package.swift file:
dependencies: [
.package(url: "https://github.com/Mr-Alirezaa/BuildableMacro.git", from: "0.2.0"),
]
Normally you'll want to depend on the BuildableMacro
target:
.product(name: "BuildableMacro", package: "BuildableMacro")
Or, if you are using Xcode, Click on "File > Add Packages" enter the following URL in the search box.
then set the version to from: 0.2.0
.
https://github.com/Mr-Alirezaa/BuildableMacro.git
We warmly welcome contributions to the BuildableMacro project! Whether you're fixing bugs, improving the documentation, or adding new features, your help is appreciated. Here’s how you can contribute:
- Fork the Repository: Start by forking the repository to your own GitHub account.
- Create a Branch: Make your changes in a new branch.
- Make Your Changes: Whether it's a new feature or a bug fix, your contributions make a difference.
- Write Tests: Ensure your changes are working as expected.
- Submit a Pull Request: Once you're satisfied, submit a pull request for review.
To give clarity of what is expected of our members, we have adopted the code of conduct defined by the Contributor Covenant. This document is used across many open source communities. For more, see the Code of Conduct.
This project is licensed under the MIT License - see the LICENSE file for details.