-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Macro-based schema discovery proof of concept
- Loading branch information
Showing
11 changed files
with
306 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// swift-tools-version: 5.9 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
import CompilerPluginSupport | ||
|
||
let package = Package( | ||
name: "RealmMacro", | ||
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)], | ||
products: [ | ||
.library( | ||
name: "RealmMacro", | ||
targets: ["RealmMacro"] | ||
), | ||
.executable( | ||
name: "RealmMacroClient", | ||
targets: ["RealmMacroClient"] | ||
), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b"), | ||
], | ||
targets: [ | ||
.macro( | ||
name: "RealmMacroMacros", | ||
dependencies: [ | ||
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"), | ||
.product(name: "SwiftCompilerPlugin", package: "swift-syntax") | ||
] | ||
), | ||
.target(name: "RealmMacro", dependencies: ["RealmMacroMacros"]), | ||
.executableTarget(name: "RealmMacroClient", dependencies: ["RealmMacro"]), | ||
.testTarget( | ||
name: "RealmMacroTests", | ||
dependencies: [ | ||
"RealmMacroMacros", | ||
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"), | ||
] | ||
), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@attached(conformance) | ||
@attached(member, names: named(_rlmProperties)) | ||
//@attached(memberAttribute) | ||
public macro RealmModel() -> () = #externalMacro(module: "RealmMacroMacros", type: "RealmObjectMacro") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import RealmMacro |
120 changes: 120 additions & 0 deletions
120
RealmMacro/Sources/RealmMacroMacros/RealmMacroMacro.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import SwiftCompilerPlugin | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
|
||
public func expansion( | ||
of node: AttributeSyntax, | ||
attachedTo declaration: some DeclGroupSyntax, | ||
providingAttributesFor member: some DeclSyntaxProtocol, | ||
in context: some MacroExpansionContext | ||
) throws -> [AttributeSyntax] { | ||
return [] | ||
guard let property = member.as(VariableDeclSyntax.self), property.bindings.count == 1 else { | ||
return [] | ||
} | ||
|
||
if let attributes = property.attributes { | ||
for attr in attributes { | ||
if case let .attribute(attr) = attr { | ||
if attr.attributeName.as(SimpleTypeIdentifierSyntax.self)?.name.text == "Ignored" { | ||
return [] | ||
} | ||
} | ||
} | ||
} | ||
|
||
let binding = property.bindings.first! | ||
switch binding.accessor { | ||
case .none: | ||
break | ||
case .accessors(let node): | ||
for accessor in node.accessors { | ||
switch accessor.accessorKind.tokenKind { | ||
case .keyword(.get), .keyword(.set): | ||
return [] | ||
default: | ||
break | ||
} | ||
} | ||
break | ||
case .getter: | ||
return [] | ||
} | ||
|
||
return [ | ||
AttributeSyntax( | ||
attributeName: SimpleTypeIdentifierSyntax(name: .identifier("Persisted")) | ||
) | ||
.with(\.leadingTrivia, [.newlines(1), .spaces(2)]) | ||
] | ||
} | ||
|
||
public struct RealmObjectMacro: MemberMacro, ConformanceMacro { | ||
public static func expansion( | ||
of node: AttributeSyntax, | ||
providingConformancesOf declaration: some DeclGroupSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] { | ||
return [(TypeSyntax("RealmSwift._RealmObjectSchemaDiscoverable"), nil)] | ||
} | ||
|
||
public static func expansion( | ||
of node: AttributeSyntax, | ||
providingMembersOf declaration: some DeclGroupSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> [DeclSyntax] { | ||
guard let declaration = declaration.as(ClassDeclSyntax.self) else { fatalError() } | ||
let className = declaration.identifier | ||
let properties = declaration.memberBlock.members.compactMap { (decl) -> (String, String, AttributeSyntax)? in | ||
guard let property = decl.decl.as(VariableDeclSyntax.self), property.bindings.count == 1 else { | ||
return nil | ||
} | ||
guard let attributes = property.attributes else { return nil } | ||
let persistedAttr = attributes.compactMap { attr in | ||
if case let .attribute(attr) = attr { | ||
if attr.attributeName.as(SimpleTypeIdentifierSyntax.self)?.name.text == "Persisted" { | ||
return attr | ||
} | ||
} | ||
return nil | ||
}.first | ||
guard let persistedAttr else { return nil } | ||
|
||
let binding = property.bindings.first! | ||
guard let identifier = binding.pattern.as(IdentifierPatternSyntax.self) else { return nil } | ||
guard let typeAnnotation = binding.typeAnnotation else { return nil } | ||
let name = identifier.identifier.text | ||
let type = typeAnnotation.type.trimmedDescription | ||
return (name, type, persistedAttr) | ||
} | ||
|
||
let rlmProperties = properties.map { (name, type, persistedAttr) in | ||
let expr = ExprSyntax("RLMProperty(name: \(literal: name), type: \(raw: type).self, keyPath: \\\(className).\(raw: name))") | ||
var functionCall = expr.as(FunctionCallExprSyntax.self)! | ||
|
||
if let argument = persistedAttr.argument, case let .argumentList(argList) = argument { | ||
var argumentList = Array(functionCall.argumentList) | ||
argumentList[argumentList.count - 1].trailingComma = ", " | ||
argumentList.append(contentsOf: argList) | ||
functionCall.argumentList = TupleExprElementListSyntax(argumentList) | ||
} | ||
return functionCall.as(ExprSyntax.self)! | ||
} | ||
return [""" | ||
static var _rlmProperties: [RLMProperty] = \(ArrayExprSyntax { | ||
for property in rlmProperties { | ||
ArrayElementSyntax(expression: property) | ||
} | ||
}) | ||
"""] | ||
} | ||
} | ||
|
||
@main | ||
struct RealmMacroPlugin: CompilerPlugin { | ||
let providingMacros: [Macro.Type] = [ | ||
RealmObjectMacro.self, | ||
] | ||
} |
Oops, something went wrong.