|
| 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 | +// PolymorphicOperationReturnTypeMacro.swift |
| 14 | +// DynamoDBTablesMacros |
| 15 | +// |
| 16 | + |
| 17 | +import SwiftDiagnostics |
| 18 | +import SwiftSyntax |
| 19 | +import SwiftSyntaxMacros |
| 20 | + |
| 21 | +private struct Attributes: CoreMacroAttributes { |
| 22 | + static let macroName: String = "PolymorphicOperationReturnType" |
| 23 | + |
| 24 | + static let protocolName: String = "PolymorphicOperationReturnType" |
| 25 | +} |
| 26 | + |
| 27 | +private struct BasicDiagnosticMessage: DiagnosticMessage { |
| 28 | + let message: String |
| 29 | + let diagnosticID: MessageID |
| 30 | + let severity: SwiftDiagnostics.DiagnosticSeverity = .error |
| 31 | + |
| 32 | + init(message: String, rawValue: String) { |
| 33 | + self.message = message |
| 34 | + self.diagnosticID = MessageID(domain: "PolymorphicOperationReturnTypeMacro", id: rawValue) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +public enum PolymorphicOperationReturnTypeMacro: ExtensionMacro { |
| 39 | + public static func expansion( |
| 40 | + of node: AttributeSyntax, |
| 41 | + attachedTo declaration: some DeclGroupSyntax, |
| 42 | + providingExtensionsOf type: some TypeSyntaxProtocol, |
| 43 | + conformingTo protocols: [TypeSyntax], |
| 44 | + in context: some MacroExpansionContext) throws -> [ExtensionDeclSyntax] |
| 45 | + { |
| 46 | + // make sure this is attached to an enum |
| 47 | + guard let enumDeclaration = declaration as? EnumDeclSyntax else { |
| 48 | + context.diagnose(.init(node: declaration, message: BaseEntryDiagnostic<Attributes>.notAttachedToEnumDeclaration)) |
| 49 | + |
| 50 | + return [] |
| 51 | + } |
| 52 | + |
| 53 | + let databaseItemType: String |
| 54 | + let standardDatabaseType = "StandardTypedDatabaseItem" |
| 55 | + if let arguments = node.arguments, case let .argumentList(argumentList) = arguments, let firstArgument = argumentList.first, argumentList.count == 1, |
| 56 | + firstArgument.label?.text == "databaseItemType", let expression = firstArgument.expression.as(StringLiteralExprSyntax.self) |
| 57 | + { |
| 58 | + databaseItemType = expression.representedLiteralValue ?? standardDatabaseType |
| 59 | + } else { |
| 60 | + databaseItemType = standardDatabaseType |
| 61 | + } |
| 62 | + |
| 63 | + let requiresProtocolConformance = protocols.reduce(false) { partialResult, protocolSyntax in |
| 64 | + if let identifierTypeSyntax = protocolSyntax.as(IdentifierTypeSyntax.self), identifierTypeSyntax.name.text == Attributes.protocolName { |
| 65 | + return true |
| 66 | + } |
| 67 | + |
| 68 | + return partialResult |
| 69 | + } |
| 70 | + |
| 71 | + let memberBlock = enumDeclaration.memberBlock.members |
| 72 | + |
| 73 | + let caseMembers: [EnumCaseDeclSyntax] = memberBlock.compactMap { member in |
| 74 | + if let caseMember = member.decl.as(EnumCaseDeclSyntax.self) { |
| 75 | + return caseMember |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | + } |
| 80 | + |
| 81 | + // make sure this is attached to an enum |
| 82 | + guard !caseMembers.isEmpty else { |
| 83 | + context.diagnose(.init(node: declaration, message: BaseEntryDiagnostic<Attributes>.enumMustNotHaveZeroCases)) |
| 84 | + |
| 85 | + return [] |
| 86 | + } |
| 87 | + |
| 88 | + let (hasDiagnostics, handleCases) = self.getCases(caseMembers: caseMembers, context: context, databaseItemType: databaseItemType) |
| 89 | + |
| 90 | + if hasDiagnostics { |
| 91 | + return [] |
| 92 | + } |
| 93 | + |
| 94 | + let type = TypeSyntax(extendedGraphemeClusterLiteral: requiresProtocolConformance ? "\(type.trimmed): \(Attributes.protocolName) " |
| 95 | + : "\(type.trimmed) ") |
| 96 | + let extensionDecl = try ExtensionDeclSyntax( |
| 97 | + extendedType: type, |
| 98 | + memberBlockBuilder: { |
| 99 | + try TypeAliasDeclSyntax("typealias AttributesType = StandardPrimaryKeyAttributes") |
| 100 | + try TypeAliasDeclSyntax("typealias TimeToLiveAttributesType = StandardTimeToLiveAttributes") |
| 101 | + |
| 102 | + let casesArray = ArrayExprSyntax( |
| 103 | + leftSquare: .leftSquareToken(), |
| 104 | + elements: handleCases, |
| 105 | + rightSquare: .rightSquareToken()) |
| 106 | + |
| 107 | + try VariableDeclSyntax( |
| 108 | + """ |
| 109 | + static let types: [(Codable.Type, PolymorphicOperationReturnOption<AttributesType, Self, TimeToLiveAttributesType>)] = |
| 110 | + \(casesArray) |
| 111 | + """) |
| 112 | + }) |
| 113 | + |
| 114 | + return [extensionDecl] |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +extension PolymorphicOperationReturnTypeMacro { |
| 119 | + private static func getCases(caseMembers: [EnumCaseDeclSyntax], context: some MacroExpansionContext, databaseItemType: String) |
| 120 | + -> (hasDiagnostics: Bool, handleCases: ArrayElementListSyntax) |
| 121 | + { |
| 122 | + var handleCases: ArrayElementListSyntax = [] |
| 123 | + var hasDiagnostics = false |
| 124 | + for caseMember in caseMembers { |
| 125 | + for element in caseMember.elements { |
| 126 | + // ensure that the enum case only has one parameter |
| 127 | + guard let parameters = element.parameterClause?.parameters, let parameterType = parameters.first?.type.as(IdentifierTypeSyntax.self), |
| 128 | + parameters.count == 1 |
| 129 | + else { |
| 130 | + context.diagnose(.init(node: element, message: BaseEntryDiagnostic<Attributes>.enumCasesMustHaveASingleParameter)) |
| 131 | + hasDiagnostics = true |
| 132 | + // do nothing for this case |
| 133 | + continue |
| 134 | + } |
| 135 | + |
| 136 | + guard parameterType.name.text == databaseItemType, |
| 137 | + let firstArgumentType = parameterType.genericArgumentClause?.arguments.first?.argument |
| 138 | + else { |
| 139 | + let message = "PolymorphicOperationReturnTypeMacro decorated enum cases parameter must be of \(databaseItemType) type." |
| 140 | + context.diagnose(.init(node: element, message: BasicDiagnosticMessage(message: message, rawValue: "enumCasesMustBeOfTheExpectedType"))) |
| 141 | + hasDiagnostics = true |
| 142 | + // do nothing for this case |
| 143 | + continue |
| 144 | + } |
| 145 | + |
| 146 | + let handleCaseSyntax = ArrayElementSyntax( |
| 147 | + expression: ExprSyntax(""" |
| 148 | + ( |
| 149 | + \(firstArgumentType).self, .init { .\(element.name)($0) } |
| 150 | + ) |
| 151 | + """), |
| 152 | + trailingComma: .commaToken()) |
| 153 | + |
| 154 | + handleCases.append(handleCaseSyntax) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return (hasDiagnostics, handleCases) |
| 159 | + } |
| 160 | +} |
0 commit comments