|
| 1 | +import SwiftCompilerPlugin |
| 2 | +import SwiftSyntax |
| 3 | +import SwiftSyntaxBuilder |
| 4 | +import SwiftSyntaxMacros |
| 5 | + |
| 6 | +public func expansion( |
| 7 | + of node: AttributeSyntax, |
| 8 | + attachedTo declaration: some DeclGroupSyntax, |
| 9 | + providingAttributesFor member: some DeclSyntaxProtocol, |
| 10 | + in context: some MacroExpansionContext |
| 11 | +) throws -> [AttributeSyntax] { |
| 12 | + return [] |
| 13 | + guard let property = member.as(VariableDeclSyntax.self), property.bindings.count == 1 else { |
| 14 | + return [] |
| 15 | + } |
| 16 | + |
| 17 | + if let attributes = property.attributes { |
| 18 | + for attr in attributes { |
| 19 | + if case let .attribute(attr) = attr { |
| 20 | + if attr.attributeName.as(SimpleTypeIdentifierSyntax.self)?.name.text == "Ignored" { |
| 21 | + return [] |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + let binding = property.bindings.first! |
| 28 | + switch binding.accessor { |
| 29 | + case .none: |
| 30 | + break |
| 31 | + case .accessors(let node): |
| 32 | + for accessor in node.accessors { |
| 33 | + switch accessor.accessorKind.tokenKind { |
| 34 | + case .keyword(.get), .keyword(.set): |
| 35 | + return [] |
| 36 | + default: |
| 37 | + break |
| 38 | + } |
| 39 | + } |
| 40 | + break |
| 41 | + case .getter: |
| 42 | + return [] |
| 43 | + } |
| 44 | + |
| 45 | + return [ |
| 46 | + AttributeSyntax( |
| 47 | + attributeName: SimpleTypeIdentifierSyntax(name: .identifier("Persisted")) |
| 48 | + ) |
| 49 | + .with(\.leadingTrivia, [.newlines(1), .spaces(2)]) |
| 50 | + ] |
| 51 | +} |
| 52 | + |
| 53 | +public struct RealmObjectMacro: MemberMacro, ConformanceMacro { |
| 54 | + public static func expansion( |
| 55 | + of node: AttributeSyntax, |
| 56 | + providingConformancesOf declaration: some DeclGroupSyntax, |
| 57 | + in context: some MacroExpansionContext |
| 58 | + ) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] { |
| 59 | + return [(TypeSyntax("RealmSwift._RealmObjectSchemaDiscoverable"), nil)] |
| 60 | + } |
| 61 | + |
| 62 | + public static func expansion( |
| 63 | + of node: AttributeSyntax, |
| 64 | + providingMembersOf declaration: some DeclGroupSyntax, |
| 65 | + in context: some MacroExpansionContext |
| 66 | + ) throws -> [DeclSyntax] { |
| 67 | + guard let declaration = declaration.as(ClassDeclSyntax.self) else { fatalError() } |
| 68 | + let className = declaration.identifier |
| 69 | + let properties = declaration.memberBlock.members.compactMap { (decl) -> (String, String, AttributeSyntax)? in |
| 70 | + guard let property = decl.decl.as(VariableDeclSyntax.self), property.bindings.count == 1 else { |
| 71 | + return nil |
| 72 | + } |
| 73 | + guard let attributes = property.attributes else { return nil } |
| 74 | + let persistedAttr = attributes.compactMap { attr in |
| 75 | + if case let .attribute(attr) = attr { |
| 76 | + if attr.attributeName.as(SimpleTypeIdentifierSyntax.self)?.name.text == "Persisted" { |
| 77 | + return attr |
| 78 | + } |
| 79 | + } |
| 80 | + return nil |
| 81 | + }.first |
| 82 | + guard let persistedAttr else { return nil } |
| 83 | + |
| 84 | + let binding = property.bindings.first! |
| 85 | + guard let identifier = binding.pattern.as(IdentifierPatternSyntax.self) else { return nil } |
| 86 | + guard let typeAnnotation = binding.typeAnnotation else { return nil } |
| 87 | + let name = identifier.identifier.text |
| 88 | + let type = typeAnnotation.type.trimmedDescription |
| 89 | + return (name, type, persistedAttr) |
| 90 | + } |
| 91 | + |
| 92 | + let rlmProperties = properties.map { (name, type, persistedAttr) in |
| 93 | + let expr = ExprSyntax("RLMProperty(name: \(literal: name), type: \(raw: type).self, keyPath: \\\(className).\(raw: name))") |
| 94 | + var functionCall = expr.as(FunctionCallExprSyntax.self)! |
| 95 | + |
| 96 | + if let argument = persistedAttr.argument, case let .argumentList(argList) = argument { |
| 97 | + var argumentList = Array(functionCall.argumentList) |
| 98 | + argumentList[argumentList.count - 1].trailingComma = ", " |
| 99 | + argumentList.append(contentsOf: argList) |
| 100 | + functionCall.argumentList = TupleExprElementListSyntax(argumentList) |
| 101 | + } |
| 102 | + return functionCall.as(ExprSyntax.self)! |
| 103 | + } |
| 104 | + return [""" |
| 105 | +
|
| 106 | + static var _rlmProperties: [RLMProperty] = \(ArrayExprSyntax { |
| 107 | + for property in rlmProperties { |
| 108 | + ArrayElementSyntax(expression: property) |
| 109 | + } |
| 110 | + }) |
| 111 | + """] |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +@main |
| 116 | +struct RealmMacroPlugin: CompilerPlugin { |
| 117 | + let providingMacros: [Macro.Type] = [ |
| 118 | + RealmObjectMacro.self, |
| 119 | + ] |
| 120 | +} |
0 commit comments