|
| 1 | +import SwiftSyntax |
| 2 | +import SwiftSyntaxMacros |
| 3 | + |
| 4 | +/// Finds and stores common strategies from a type declaration's macro attributes. |
| 5 | +/// |
| 6 | +/// This struct parses the macro attributes on a type declaration to extract any |
| 7 | +/// common type conversion strategies (such as value coding strategies) that should |
| 8 | +/// be applied to all properties. The extracted strategies can then be used to |
| 9 | +/// automatically transform property registrations during macro expansion. |
| 10 | +struct StrategyFinder { |
| 11 | + /// The list of value coding strategies (as type names) to apply to properties. |
| 12 | + let valueCodingStrategies: [TokenSyntax] |
| 13 | + // Extend with more strategies as needed |
| 14 | + |
| 15 | + /// Initializes a new `StrategyFinder` by parsing the macro attributes of the given declaration. |
| 16 | + /// |
| 17 | + /// - Parameter decl: The declaration to extract strategies from. |
| 18 | + init(decl: some AttributableDeclSyntax) { |
| 19 | + guard |
| 20 | + let attr = Codable(from: decl), |
| 21 | + let arguments = attr.node.arguments?.as(LabeledExprListSyntax.self), |
| 22 | + let arg = arguments.first(where: { |
| 23 | + $0.label?.text == "commonStrategies" |
| 24 | + }), |
| 25 | + let expr = arg.expression.as(ArrayExprSyntax.self) |
| 26 | + else { |
| 27 | + self.valueCodingStrategies = [] |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + let codedBys = expr.elements.lazy |
| 32 | + .compactMap({ $0.expression.as(FunctionCallExprSyntax.self) }) |
| 33 | + .filter({ |
| 34 | + $0.calledExpression.as(MemberAccessExprSyntax.self)?.declName |
| 35 | + .baseName.text == "codedBy" |
| 36 | + }) |
| 37 | + let valueCoders = codedBys.flatMap({ |
| 38 | + $0.arguments |
| 39 | + .compactMap({ $0.expression.as(FunctionCallExprSyntax.self) }) |
| 40 | + .filter({ |
| 41 | + $0.calledExpression.as(MemberAccessExprSyntax.self)? |
| 42 | + .declName.baseName.text == "valueCoder" |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + var valueCodingStrategies: [TokenSyntax] = [] |
| 47 | + if !valueCoders.isEmpty { |
| 48 | + // Default strategies for primitive types |
| 49 | + valueCodingStrategies = [ |
| 50 | + "Bool", "Double", "Float", "String", |
| 51 | + "Int", "Int8", "Int16", "Int32", "Int64", |
| 52 | + "UInt", "UInt8", "UInt16", "UInt32", "UInt64", |
| 53 | + ] |
| 54 | + // Add any additional types specified in valueCoder(...) |
| 55 | + valueCodingStrategies.append( |
| 56 | + contentsOf: valueCoders.flatMap { |
| 57 | + $0.arguments.first?.expression.as(ArrayExprSyntax.self)? |
| 58 | + .elements.compactMap { |
| 59 | + $0.expression.as(MemberAccessExprSyntax.self)?.base? |
| 60 | + .as(DeclReferenceExprSyntax.self)?.baseName |
| 61 | + } ?? [] |
| 62 | + } |
| 63 | + ) |
| 64 | + } |
| 65 | + self.valueCodingStrategies = valueCodingStrategies |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +extension Registration where Var: DefaultPropertyVariable { |
| 70 | + /// Applies common strategies (such as value coding) to the property registration if the type declaration specifies them. |
| 71 | + /// |
| 72 | + /// This method uses `StrategyFinder` to extract any common strategies (e.g., value coding strategies) |
| 73 | + /// from the macro attributes of the provided type declaration. If the property's type matches one of the |
| 74 | + /// strategies, it wraps the property in a `HelperCodedVariable` using the appropriate helper (such as `ValueCoder`). |
| 75 | + /// |
| 76 | + /// - Parameter decl: The type declaration to extract strategies from. |
| 77 | + /// - Returns: A new registration, possibly wrapping the property variable with a strategy-based helper. |
| 78 | + func detectCommonStrategies( |
| 79 | + from decl: some AttributableDeclSyntax |
| 80 | + ) -> Registration<Decl, Key, StrategyVariable<Var.Initialization>> { |
| 81 | + let finder = StrategyFinder(decl: decl) |
| 82 | + let inStrategy = |
| 83 | + finder.valueCodingStrategies.first { strategy in |
| 84 | + strategy.trimmedDescription |
| 85 | + == self.variable.type.trimmedDescription |
| 86 | + } != nil |
| 87 | + |
| 88 | + let newVariable: AnyPropertyVariable<Var.Initialization> |
| 89 | + if inStrategy { |
| 90 | + newVariable = |
| 91 | + HelperCodedVariable( |
| 92 | + base: self.variable, |
| 93 | + options: .helper("ValueCoder<\(variable.type)>()") |
| 94 | + ).any |
| 95 | + } else { |
| 96 | + newVariable = self.variable.any |
| 97 | + } |
| 98 | + return self.updating(with: StrategyVariable(base: newVariable)) |
| 99 | + } |
| 100 | +} |
0 commit comments