|
| 1 | +@_implementationOnly import SwiftSyntax |
| 2 | +@_implementationOnly import SwiftSyntaxMacros |
| 3 | + |
| 4 | +/// An `EnumSwitcherVariable` generating switch expression for adjacently |
| 5 | +/// tagged enums. |
| 6 | +/// |
| 7 | +/// Registers path for enum-case associated variables container root and |
| 8 | +/// generated syntax for this common container. |
| 9 | +struct AdjacentlyTaggedEnumSwitcher<Wrapped>: EnumSwitcherVariable, |
| 10 | + ComposedVariable |
| 11 | +where Wrapped: AdjacentlyTaggableSwitcher { |
| 12 | + /// The switcher value wrapped by this instance. |
| 13 | + /// |
| 14 | + /// The wrapped variable's type data is preserved and this variable is used |
| 15 | + /// to chain code generation implementations while changing the root |
| 16 | + /// container for enum-case associated variables. |
| 17 | + let base: Wrapped |
| 18 | + /// The container mapping variable. |
| 19 | + /// |
| 20 | + /// This variable is used to map the nested container |
| 21 | + /// for associated variables to a pre-defined name. |
| 22 | + let variable: CoderVariable |
| 23 | + |
| 24 | + /// Creates switcher variable with provided data. |
| 25 | + /// |
| 26 | + /// - Parameters: |
| 27 | + /// - base: The base variable that handles implementation. |
| 28 | + /// - contentDecoder: The mapped name for content root decoder. |
| 29 | + /// - contentEncoder: The mapped name for content root encoder. |
| 30 | + /// - keyPath: The key path to enum-case content root. |
| 31 | + /// - codingKeys: The map where `CodingKeys` maintained. |
| 32 | + /// - context: The context in which to perform the macro expansion. |
| 33 | + init( |
| 34 | + base: Wrapped, contentDecoder: TokenSyntax, contentEncoder: TokenSyntax, |
| 35 | + keyPath: [String], codingKeys: CodingKeysMap, |
| 36 | + context: some MacroExpansionContext |
| 37 | + ) { |
| 38 | + let keys = codingKeys.add(keys: keyPath, context: context) |
| 39 | + self.variable = .init(decoder: contentDecoder, encoder: contentEncoder) |
| 40 | + self.base = base.registering(variable: variable, keyPath: keys) |
| 41 | + } |
| 42 | + |
| 43 | + /// Creates value expression for provided enum-case variable. |
| 44 | + /// |
| 45 | + /// Provides value generated by the underlying variable value. |
| 46 | + /// |
| 47 | + /// - Parameters: |
| 48 | + /// - variable: The variable for which generated. |
| 49 | + /// - value: The optional value present in syntax. |
| 50 | + /// - codingKeys: The map where `CodingKeys` maintained. |
| 51 | + /// - context: The context in which to perform the macro expansion. |
| 52 | + /// |
| 53 | + /// - Returns: The generated value. |
| 54 | + func keyExpression<Var: EnumCaseVariable>( |
| 55 | + for variable: Var, value: ExprSyntax?, |
| 56 | + codingKeys: CodingKeysMap, context: some MacroExpansionContext |
| 57 | + ) -> EnumVariable.CaseValue { |
| 58 | + return base.keyExpression( |
| 59 | + for: variable, value: value, |
| 60 | + codingKeys: codingKeys, context: context |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + /// Provides the syntax for decoding at the provided location. |
| 65 | + /// |
| 66 | + /// Provides implementation generated by the underlying variable value |
| 67 | + /// while changing the root container name for enum-case associated |
| 68 | + /// values decoding. |
| 69 | + /// |
| 70 | + /// - Parameters: |
| 71 | + /// - context: The context in which to perform the macro expansion. |
| 72 | + /// - location: The decoding location. |
| 73 | + /// |
| 74 | + /// - Returns: The generated decoding syntax. |
| 75 | + func decoding( |
| 76 | + in context: some MacroExpansionContext, |
| 77 | + from location: EnumSwitcherLocation |
| 78 | + ) -> EnumSwitcherGenerated { |
| 79 | + let generated = base.decoding(in: context, from: location) |
| 80 | + let newData: EnumSwitcherGenerated.CaseData |
| 81 | + switch generated.data { |
| 82 | + case .container: |
| 83 | + newData = generated.data |
| 84 | + case .coder(_, let postfix): |
| 85 | + newData = .coder(variable.decoder, postfix) |
| 86 | + } |
| 87 | + return .init( |
| 88 | + data: newData, expr: generated.expr, code: generated.code, |
| 89 | + defaultCase: generated.defaultCase |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + /// Provides the syntax for encoding at the provided location. |
| 94 | + /// |
| 95 | + /// Provides implementation generated by the underlying variable value |
| 96 | + /// while changing the root container name for enum-case associated |
| 97 | + /// values encoding. |
| 98 | + /// |
| 99 | + /// - Parameters: |
| 100 | + /// - context: The context in which to perform the macro expansion. |
| 101 | + /// - location: The encoding location. |
| 102 | + /// |
| 103 | + /// - Returns: The generated encoding syntax. |
| 104 | + func encoding( |
| 105 | + in context: some MacroExpansionContext, |
| 106 | + to location: EnumSwitcherLocation |
| 107 | + ) -> EnumSwitcherGenerated { |
| 108 | + let generated = base.encoding(in: context, to: location) |
| 109 | + let newData: EnumSwitcherGenerated.CaseData |
| 110 | + switch generated.data { |
| 111 | + case .container: |
| 112 | + newData = generated.data |
| 113 | + case .coder(_, let postfix): |
| 114 | + newData = .coder(variable.encoder, postfix) |
| 115 | + } |
| 116 | + return .init( |
| 117 | + data: newData, expr: generated.expr, code: generated.code, |
| 118 | + defaultCase: generated.defaultCase |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + /// Creates additional enum declarations for enum variable. |
| 123 | + /// |
| 124 | + /// Provides enum declarations for of the underlying variable value. |
| 125 | + /// |
| 126 | + /// - Parameter context: The context in which to perform the macro |
| 127 | + /// expansion. |
| 128 | + /// - Returns: The generated enum declaration syntax. |
| 129 | + func codingKeys( |
| 130 | + in context: some MacroExpansionContext |
| 131 | + ) -> MemberBlockItemListSyntax { |
| 132 | + return base.codingKeys(in: context) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +extension AdjacentlyTaggedEnumSwitcher { |
| 137 | + /// A variable value exposing decoder and encoder. |
| 138 | + /// |
| 139 | + /// The `CoderVariable` exposes decoder and encoder via variable |
| 140 | + /// provided with `decoder` and `encoder` name respectively. |
| 141 | + struct CoderVariable: PropertyVariable, ComposedVariable { |
| 142 | + /// The initialization type of this variable. |
| 143 | + /// |
| 144 | + /// Initialization type is the same as underlying wrapped variable. |
| 145 | + typealias Initialization = BasicPropertyVariable.Initialization |
| 146 | + /// The mapped name for decoder. |
| 147 | + /// |
| 148 | + /// The decoder at location passed will be exposed |
| 149 | + /// with this variable name. |
| 150 | + let decoder: TokenSyntax |
| 151 | + /// The mapped name for encoder. |
| 152 | + /// |
| 153 | + /// The encoder at location passed will be exposed |
| 154 | + /// with this variable name. |
| 155 | + let encoder: TokenSyntax |
| 156 | + |
| 157 | + /// The value wrapped by this instance. |
| 158 | + /// |
| 159 | + /// The wrapped variable's type data is |
| 160 | + /// preserved and this variable is used |
| 161 | + /// to chain code generation implementations. |
| 162 | + let base = BasicPropertyVariable( |
| 163 | + name: "", type: "", value: nil, |
| 164 | + decodePrefix: "", encodePrefix: "", |
| 165 | + decode: true, encode: true |
| 166 | + ) |
| 167 | + |
| 168 | + /// Whether the variable is to be decoded. |
| 169 | + /// |
| 170 | + /// This variable is always set as to be decoded. |
| 171 | + var decode: Bool? { true } |
| 172 | + /// Whether the variable is to be encoded. |
| 173 | + /// |
| 174 | + /// This variable is always set as to be encoded. |
| 175 | + var encode: Bool? { true } |
| 176 | + |
| 177 | + /// Whether the variable type requires `Decodable` conformance. |
| 178 | + /// |
| 179 | + /// This variable never requires `Decodable` conformance |
| 180 | + var requireDecodable: Bool? { false } |
| 181 | + /// Whether the variable type requires `Encodable` conformance. |
| 182 | + /// |
| 183 | + /// This variable never requires `Encodable` conformance |
| 184 | + var requireEncodable: Bool? { false } |
| 185 | + |
| 186 | + /// Provides the code syntax for decoding this variable |
| 187 | + /// at the provided location. |
| 188 | + /// |
| 189 | + /// Creates/assigns the decoder passed in location to the variable |
| 190 | + /// created with the `decoder` name provided. |
| 191 | + /// |
| 192 | + /// - Parameters: |
| 193 | + /// - context: The context in which to perform the macro expansion. |
| 194 | + /// - location: The decoding location for the variable. |
| 195 | + /// |
| 196 | + /// - Returns: The generated variable decoding code. |
| 197 | + func decoding( |
| 198 | + in context: some MacroExpansionContext, |
| 199 | + from location: PropertyCodingLocation |
| 200 | + ) -> CodeBlockItemListSyntax { |
| 201 | + return switch location { |
| 202 | + case .coder(let decoder, _): |
| 203 | + "let \(self.decoder) = \(decoder)" |
| 204 | + case .container(let container, let key, _): |
| 205 | + "let \(self.decoder) = try \(container).superDecoder(forKey: \(key))" |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /// Provides the code syntax for encoding this variable |
| 210 | + /// at the provided location. |
| 211 | + /// |
| 212 | + /// Creates/assigns the encoder passed in location to the variable |
| 213 | + /// created with the `encoder` name provided. |
| 214 | + /// |
| 215 | + /// - Parameters: |
| 216 | + /// - context: The context in which to perform the macro expansion. |
| 217 | + /// - location: The encoding location for the variable. |
| 218 | + /// |
| 219 | + /// - Returns: The generated variable encoding code. |
| 220 | + func encoding( |
| 221 | + in context: some MacroExpansionContext, |
| 222 | + to location: PropertyCodingLocation |
| 223 | + ) -> CodeBlockItemListSyntax { |
| 224 | + return switch location { |
| 225 | + case .coder(let encoder, _): |
| 226 | + "let \(self.encoder) = \(encoder)" |
| 227 | + case .container(let container, let key, _): |
| 228 | + "let \(self.encoder) = \(container).superEncoder(forKey: \(key))" |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments