|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the DynamoDBTables open source project |
| 4 | +// |
| 5 | +// See LICENSE.txt for license information |
| 6 | +// See CONTRIBUTORS.txt for the list of DynamoDBTables authors |
| 7 | +// |
| 8 | +// SPDX-License-Identifier: Apache-2.0 |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +// |
| 13 | +// BaseEntryMacro.swift |
| 14 | +// DynamoDBTablesMacros |
| 15 | +// |
| 16 | + |
| 17 | +import SwiftDiagnostics |
| 18 | +import SwiftSyntax |
| 19 | +import SwiftSyntaxMacros |
| 20 | + |
| 21 | +protocol MacroAttributes { |
| 22 | + static var macroName: String { get } |
| 23 | + |
| 24 | + static var protocolName: String { get } |
| 25 | + |
| 26 | + static var transformType: String { get } |
| 27 | + |
| 28 | + static var contextType: String { get } |
| 29 | +} |
| 30 | + |
| 31 | +enum BaseEntryDiagnostic<Attributes: MacroAttributes>: String, DiagnosticMessage { |
| 32 | + case notAttachedToEnumDeclaration |
| 33 | + case enumMustHaveSendableConformance |
| 34 | + case enumMustNotHaveZeroCases |
| 35 | + case enumCasesMustHaveASingleParameter |
| 36 | + |
| 37 | + var diagnosticID: MessageID { |
| 38 | + MessageID(domain: "\(Attributes.macroName)Macro", id: rawValue) |
| 39 | + } |
| 40 | + |
| 41 | + var severity: DiagnosticSeverity { .error } |
| 42 | + |
| 43 | + static var obj: String { "" } |
| 44 | + |
| 45 | + var message: String { |
| 46 | + switch self { |
| 47 | + case .notAttachedToEnumDeclaration: |
| 48 | + return "@\(Attributes.macroName) must be attached to an enum declaration." |
| 49 | + case .enumMustHaveSendableConformance: |
| 50 | + return "@\(Attributes.macroName) decorated enum must conform to Sendable." |
| 51 | + case .enumMustNotHaveZeroCases: |
| 52 | + return "@\(Attributes.macroName) decorated enum must be have at least a singe case." |
| 53 | + case .enumCasesMustHaveASingleParameter: |
| 54 | + return "@\(Attributes.macroName) decorated enum can only have case entries with a single parameter." |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +enum BaseEntryMacro<Attributes: MacroAttributes>: ExtensionMacro { |
| 60 | + private static func getCases(caseMembers: [EnumCaseDeclSyntax], context: some MacroExpansionContext, passCompositePrimaryKey: Bool) |
| 61 | + -> (hasDiagnostics: Bool, handleCases: SwitchCaseListSyntax, compositePrimaryKeyCases: SwitchCaseListSyntax) |
| 62 | + { |
| 63 | + var handleCases: SwitchCaseListSyntax = [] |
| 64 | + var compositePrimaryKeyCases: SwitchCaseListSyntax = [] |
| 65 | + var hasDiagnostics = false |
| 66 | + for caseMember in caseMembers { |
| 67 | + for element in caseMember.elements { |
| 68 | + // ensure that the enum case only has one parameter |
| 69 | + guard let parameterClause = element.parameterClause, parameterClause.parameters.count == 1 else { |
| 70 | + context.diagnose(.init(node: element, message: BaseEntryDiagnostic<Attributes>.enumCasesMustHaveASingleParameter)) |
| 71 | + hasDiagnostics = true |
| 72 | + // do nothing for this case |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + // TODO: when made possible by the language, check that the type of the parameter conforms to `WriteEntry` or `TransactionConstraintEntry` |
| 77 | + // https://github.com/swift-server-community/dynamo-db-tables/issues/38 |
| 78 | + |
| 79 | + let handleCaseSyntax = SwitchCaseListSyntax.Element( |
| 80 | + """ |
| 81 | + case let .\(element.name)(writeEntry): |
| 82 | + return try context.transform(writeEntry) |
| 83 | + """) |
| 84 | + |
| 85 | + handleCases.append(handleCaseSyntax) |
| 86 | + |
| 87 | + if passCompositePrimaryKey { |
| 88 | + let compositePrimaryKeyCaseSyntax = SwitchCaseListSyntax.Element( |
| 89 | + """ |
| 90 | + case let .\(element.name)(writeEntry): |
| 91 | + return writeEntry.compositePrimaryKey |
| 92 | + """) |
| 93 | + |
| 94 | + compositePrimaryKeyCases.append(compositePrimaryKeyCaseSyntax) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return (hasDiagnostics, handleCases, compositePrimaryKeyCases) |
| 100 | + } |
| 101 | + |
| 102 | + static func expansion( |
| 103 | + of node: AttributeSyntax, |
| 104 | + attachedTo declaration: some DeclGroupSyntax, |
| 105 | + providingExtensionsOf type: some TypeSyntaxProtocol, |
| 106 | + conformingTo protocols: [TypeSyntax], |
| 107 | + in context: some MacroExpansionContext) throws -> [ExtensionDeclSyntax] |
| 108 | + { |
| 109 | + let passCompositePrimaryKey: Bool |
| 110 | + if let arguments = node.arguments, case let .argumentList(argumentList) = arguments, let firstArgument = argumentList.first, argumentList.count == 1, |
| 111 | + firstArgument.label?.text == "passCompositePrimaryKey", let expression = firstArgument.expression.as(BooleanLiteralExprSyntax.self), |
| 112 | + case let .keyword(keyword) = expression.literal.tokenKind, keyword == SwiftSyntax.Keyword.false |
| 113 | + { |
| 114 | + passCompositePrimaryKey = false |
| 115 | + } else { |
| 116 | + passCompositePrimaryKey = true |
| 117 | + } |
| 118 | + |
| 119 | + // make sure this is attached to an enum |
| 120 | + guard let enumDeclaration = declaration as? EnumDeclSyntax else { |
| 121 | + context.diagnose(.init(node: declaration, message: BaseEntryDiagnostic<Attributes>.notAttachedToEnumDeclaration)) |
| 122 | + |
| 123 | + return [] |
| 124 | + } |
| 125 | + |
| 126 | + let requiresProtocolConformance = protocols.reduce(false) { partialResult, protocolSyntax in |
| 127 | + if let identifierTypeSyntax = protocolSyntax.as(IdentifierTypeSyntax.self), identifierTypeSyntax.name.text == Attributes.protocolName { |
| 128 | + return true |
| 129 | + } |
| 130 | + |
| 131 | + return partialResult |
| 132 | + } |
| 133 | + |
| 134 | + // make sure the type is conforming to Sendable |
| 135 | + let hasSendableConformance = declaration.inheritanceClause?.inheritedTypes.reduce(false) { partialResult, inheritedType in |
| 136 | + if let identifierTypeSyntax = inheritedType.type.as(IdentifierTypeSyntax.self), identifierTypeSyntax.name.text == "Sendable" { |
| 137 | + return true |
| 138 | + } |
| 139 | + |
| 140 | + return partialResult |
| 141 | + } ?? false |
| 142 | + |
| 143 | + guard hasSendableConformance else { |
| 144 | + context.diagnose(.init(node: declaration, message: BaseEntryDiagnostic<Attributes>.enumMustHaveSendableConformance)) |
| 145 | + |
| 146 | + return [] |
| 147 | + } |
| 148 | + |
| 149 | + let memberBlock = enumDeclaration.memberBlock.members |
| 150 | + |
| 151 | + let caseMembers: [EnumCaseDeclSyntax] = memberBlock.compactMap { member in |
| 152 | + if let caseMember = member.decl.as(EnumCaseDeclSyntax.self) { |
| 153 | + return caseMember |
| 154 | + } |
| 155 | + |
| 156 | + return nil |
| 157 | + } |
| 158 | + |
| 159 | + // make sure this is attached to an enum |
| 160 | + guard !caseMembers.isEmpty else { |
| 161 | + context.diagnose(.init(node: declaration, message: BaseEntryDiagnostic<Attributes>.enumMustNotHaveZeroCases)) |
| 162 | + |
| 163 | + return [] |
| 164 | + } |
| 165 | + |
| 166 | + let (hasDiagnostics, handleCases, compositePrimaryKeyCases) = self.getCases(caseMembers: caseMembers, context: context, |
| 167 | + passCompositePrimaryKey: passCompositePrimaryKey) |
| 168 | + |
| 169 | + if hasDiagnostics { |
| 170 | + return [] |
| 171 | + } |
| 172 | + |
| 173 | + let type = TypeSyntax(extendedGraphemeClusterLiteral: requiresProtocolConformance ? "\(type.trimmed): \(Attributes.protocolName) " |
| 174 | + : "\(type.trimmed) ") |
| 175 | + let extensionDecl = try ExtensionDeclSyntax( |
| 176 | + extendedType: type, |
| 177 | + memberBlockBuilder: { |
| 178 | + try FunctionDeclSyntax( |
| 179 | + "func handle<Context: \(raw: Attributes.contextType)>(context: Context) throws -> Context.\(raw: Attributes.transformType)") |
| 180 | + { |
| 181 | + SwitchExprSyntax(subject: ExprSyntax(stringLiteral: "self"), cases: handleCases) |
| 182 | + } |
| 183 | + |
| 184 | + if passCompositePrimaryKey { |
| 185 | + try VariableDeclSyntax("var compositePrimaryKey: StandardCompositePrimaryKey?") { |
| 186 | + SwitchExprSyntax(subject: ExprSyntax(stringLiteral: "self"), cases: compositePrimaryKeyCases) |
| 187 | + } |
| 188 | + } |
| 189 | + }) |
| 190 | + |
| 191 | + return [extensionDecl] |
| 192 | + } |
| 193 | +} |
0 commit comments