|
1 |
| -import SwiftCompilerPlugin |
2 | 1 | import Foundation
|
| 2 | +import SwiftCompilerPlugin |
3 | 3 | import SwiftSyntax
|
4 | 4 | import SwiftSyntaxBuilder
|
5 | 5 | import SwiftSyntaxMacros
|
@@ -28,6 +28,8 @@ public struct StringifyMacro: ExpressionMacro {
|
28 | 28 |
|
29 | 29 | // MARK: -
|
30 | 30 |
|
| 31 | +/// Source: https://www.avanderlee.com/swift/macros/ |
| 32 | + |
31 | 33 | public struct URLMacro: ExpressionMacro {
|
32 | 34 | public static func expansion(
|
33 | 35 | of node: some FreestandingMacroExpansionSyntax,
|
@@ -83,10 +85,62 @@ enum URLMacroError: Error, CustomStringConvertible {
|
83 | 85 |
|
84 | 86 | // MARK: -
|
85 | 87 |
|
| 88 | +/// Source: https://betterprogramming.pub/use-swift-macros-to-initialize-a-structure-516728c5fb49 |
| 89 | + |
| 90 | +public struct StructInitMacro: MemberMacro { |
| 91 | + public static func expansion( |
| 92 | + of node: AttributeSyntax, |
| 93 | + providingMembersOf declaration: some DeclGroupSyntax, |
| 94 | + in context: some MacroExpansionContext |
| 95 | + ) throws -> [SwiftSyntax.DeclSyntax] { |
| 96 | + guard let structDecl = declaration.as(StructDeclSyntax.self) else { |
| 97 | + throw StructInitError.onlyApplicableToStruct |
| 98 | + } |
| 99 | + |
| 100 | + let members = structDecl.memberBlock.members |
| 101 | + let variableDecl = members.compactMap { $0.decl.as(VariableDeclSyntax.self) } |
| 102 | + let variablesName = variableDecl.compactMap { $0.bindings.first?.pattern } |
| 103 | + let variablesType = variableDecl.compactMap { $0.bindings.first?.typeAnnotation?.type } |
| 104 | + |
| 105 | + let initializer = try InitializerDeclSyntax(StructInitMacro.generateInitialCode(variablesName: variablesName, variablesType: variablesType)) { |
| 106 | + for name in variablesName { |
| 107 | + ExprSyntax("self.\(name) = \(name)") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return [DeclSyntax(initializer)] |
| 112 | + } |
| 113 | + |
| 114 | + public static func generateInitialCode(variablesName: [PatternSyntax], |
| 115 | + variablesType: [TypeSyntax]) -> SyntaxNodeString |
| 116 | + { |
| 117 | + var initialCode = "init(" |
| 118 | + for (name, type) in zip(variablesName, variablesType) { |
| 119 | + initialCode += "\(name): \(type), " |
| 120 | + } |
| 121 | + initialCode = String(initialCode.dropLast(2)) |
| 122 | + initialCode += ")" |
| 123 | + return SyntaxNodeString(stringLiteral: initialCode) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +enum StructInitError: CustomStringConvertible, Error { |
| 128 | + case onlyApplicableToStruct |
| 129 | + |
| 130 | + var description: String { |
| 131 | + switch self { |
| 132 | + case .onlyApplicableToStruct: return "@StructInit can only be applied to a structure" |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// MARK: - |
| 138 | + |
86 | 139 | @main
|
87 | 140 | struct URLMacroPlugin: CompilerPlugin {
|
88 | 141 | let providingMacros: [Macro.Type] = [
|
89 | 142 | StringifyMacro.self,
|
90 | 143 | URLMacro.self,
|
| 144 | + StructInitMacro.self, |
91 | 145 | ]
|
92 | 146 | }
|
0 commit comments