Skip to content

Commit 3a16b33

Browse files
author
Alexander Schmutz
committed
Update readme documentation
1 parent e96a9d6 commit 3a16b33

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

README.md

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Swift Struct Builder Macro
1+
# Swift Builder Macro
22
An attached swift macro that produces a peer struct which implements the builder pattern.
33
This allows the creation of the struct with minimal effort using default values.
44

5-
> **Important!** This macro is intended to be used for simple structs without explicit initialiser.
6-
The macro is by no means a perfect solution that works for all struct implementations.
5+
> **Important!** This macro is intended to be used for simple structs and enums without explicit initialiser.
6+
The macro is by no means a perfect solution that works for all struct or enum implementations.
77
The macro makes a best guess to decide how the implicit memberwise initialiser could look like and might fail if some edge cases have not been considered.
88
The macro was created out of personal interest and for a few smaller private projects.
99

@@ -15,6 +15,7 @@ struct Person {
1515
let age: Int
1616
let address: Address
1717
let hobby: String?
18+
let favouriteSeason: Season
1819

1920
var likesReading: Bool {
2021
hobby == "Reading"
@@ -23,8 +24,16 @@ struct Person {
2324
static let minimumAge = 21
2425
}
2526

27+
@Buildable
28+
enum Season {
29+
case .winter
30+
case .spring
31+
case .summer
32+
case .autumn
33+
}
34+
2635
let anyPerson = PersonBuilder().build()
27-
let max = PersonBuilder(name: "Max").build()
36+
let max = PersonBuilder(name: "Max", favouriteSeason: .summer).build()
2837
```
2938
Expanded macro
3039
```swift
@@ -33,22 +42,31 @@ struct PersonBuilder {
3342
var age: Int = 0
3443
var address: Address = AddressBuilder().build()
3544
var hobby: String?
45+
var favouriteSeason: SeasonBuilder = SeasonBuilder().build()
3646

3747
func build() -> Person {
3848
return Person(
3949
name: name,
4050
age: age,
4151
address: address,
42-
hobby: hobby
52+
hobby: hobby,
53+
favouriteSeason: favouriteSeason
4354
)
4455
}
4556
}
57+
58+
struct SeasonBuilder {
59+
var value: Season = .spring
60+
61+
func build() -> Season {
62+
return value
63+
}
64+
}
4665
```
4766

4867
### Limitations
4968
- The list of default values is limited to the values specified in the below table. The list can be extended, if necessary.
50-
- For an enum Type `MyEnum` the default value is currently set to `MyEnumBuilder().build()`, instead to the first enum case
51-
- The macro only works on `struct` definitions
69+
- The macro only works on `struct` and `enum` definitions
5270

5371

5472
### Specified default values
@@ -92,7 +110,6 @@ assuming that the `UnknownTypeBuilder` was created somewhere else.
92110

93111
Why did I create the macro?
94112
- I created the macro out of personal curiosity and the hope to be able to reduce repetitive code in my own projects
95-
- I am not using the macro in any real project yet, mostly because I don't know yet how to get the first enum case when the type is en enum. If anybody knows how, please reach out :)
96113

97114
Will I continue developing the macro?
98115
- Depending on my own use cases, which I am/was trying to solve, I might continue developing the macro, **though, I do guarantee to keep the project up to date**

Sources/StructBuilder/StructBuilder.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,42 @@
88
/// let name: String
99
/// let age: Int
1010
/// let address: Address
11+
/// let favouriteSeason: Season
12+
/// }
13+
///
14+
/// @Buildable
15+
/// enum Season {
16+
/// case .winter
17+
/// case .spring
18+
/// case .summer
19+
/// case .autumn
1120
/// }
1221
///
1322
/// will expand to
1423
///
15-
/// struct Person {
16-
/// let name: String
17-
/// let age: Int
18-
/// let address: Address
19-
/// }
2024
/// struct PersonBuilder {
2125
/// var name: String = ""
2226
/// var age: Int = 0
2327
/// var address: Address = AddressBuilder().build()
28+
/// var favouriteSeason: SeasonBuilder = SeasonBuilder().build()
2429
///
2530
/// func build() -> Person {
2631
/// return Person(
2732
/// name: name,
2833
/// age: age,
29-
/// address: address
34+
/// address: address,
35+
/// favouriteSeason: favouriteSeason
3036
/// )
3137
/// }
3238
/// }
39+
///
40+
/// struct SeasonBuilder {
41+
/// var value: Season = .spring
42+
///
43+
/// func build() -> Season {
44+
/// return value
45+
/// }
46+
/// }
3347
@attached(peer, names: suffixed(Builder))
3448
public macro Buildable() = #externalMacro(
3549
module: "StructBuilderMacro",

0 commit comments

Comments
 (0)