Awesome
Builder Macro
Getting Started 🚀
While macros are still very fresh inspired by work done by Doug Gregor here this repo aims to help get familliar with the macros by exploring their capabilities.
While the macros are still in "beta" since dependencies point to 5.9-DEVELOPMENT-SNAPSHOT you can probably add a Package library to your project if it will be created with Xcode 15 beta.
Play around 🛝
For now to check it out download the repo and run executable target BuilderMacroClient
the BuilderMacro
contains most important code.
The basic concept of this macro is to generate a Builder
helper class which will contain same stored properties as the struct that it's attached to, all optional to set them at need. Also a helper fill method to set all properties based on the filling object, and the build method which will attempt to generate the struct based on what's inside. Example of how it's influencing the code can be found inside BuilderMacroTests
Overview
Writing code with @Builder annotation like this
Is equivalent of writing this by hand
There are different flavours of builders that you can use depending on your use case
Throwing
If you want to understand the reason behind builder not being able to build your model use @ThrowingBuilder
which will throw an error telling about the first missing property which was required to be present.
Fluent
When you prefer to have a chain of building commands to get your result attach @FluentBuilder
.
Extra Builder Features
On top of standard fill / build methods there are extra mechanisms.
- UUID stubbing -> if uuid field is found it will be auto generated by builder if not present
- Simple Diagnostics -> auto fix from xcode for using wrong decl like class / actor