You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+25-8Lines changed: 25 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
-
# Swift Struct Builder Macro
1
+
# Swift Builder Macro
2
2
An attached swift macro that produces a peer struct which implements the builder pattern.
3
3
This allows the creation of the struct with minimal effort using default values.
4
4
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.
7
7
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.
8
8
The macro was created out of personal interest and for a few smaller private projects.
9
9
@@ -15,6 +15,7 @@ struct Person {
15
15
let age: Int
16
16
let address: Address
17
17
let hobby: String?
18
+
let favouriteSeason: Season
18
19
19
20
var likesReading: Bool {
20
21
hobby =="Reading"
@@ -23,8 +24,16 @@ struct Person {
23
24
staticlet minimumAge =21
24
25
}
25
26
27
+
@Buildable
28
+
enumSeason {
29
+
case .winter
30
+
case .spring
31
+
case .summer
32
+
case .autumn
33
+
}
34
+
26
35
let anyPerson =PersonBuilder().build()
27
-
let max =PersonBuilder(name: "Max").build()
36
+
let max =PersonBuilder(name: "Max", favouriteSeason: .summer).build()
28
37
```
29
38
Expanded macro
30
39
```swift
@@ -33,22 +42,31 @@ struct PersonBuilder {
33
42
var age: Int=0
34
43
var address: Address =AddressBuilder().build()
35
44
var hobby: String?
45
+
var favouriteSeason: SeasonBuilder =SeasonBuilder().build()
36
46
37
47
funcbuild() -> Person {
38
48
returnPerson(
39
49
name: name,
40
50
age: age,
41
51
address: address,
42
-
hobby: hobby
52
+
hobby: hobby,
53
+
favouriteSeason: favouriteSeason
43
54
)
44
55
}
45
56
}
57
+
58
+
structSeasonBuilder {
59
+
var value: Season = .spring
60
+
61
+
funcbuild() -> Season {
62
+
return value
63
+
}
64
+
}
46
65
```
47
66
48
67
### Limitations
49
68
- 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
52
70
53
71
54
72
### Specified default values
@@ -92,7 +110,6 @@ assuming that the `UnknownTypeBuilder` was created somewhere else.
92
110
93
111
Why did I create the macro?
94
112
- 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 :)
96
113
97
114
Will I continue developing the macro?
98
115
- 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**
0 commit comments