BuilderMacro

main

Reduce boilerplate by generating Builders for your Models using swift macros.
dziobaczy/SwiftBuilderMacro

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

image

Is equivalent of writing this by hand

image

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

image

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

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Tue Oct 22 2024 07:38:04 GMT-0900 (Hawaii-Aleutian Daylight Time)