Skip to content

Commit 2b7a95d

Browse files
committed
Add Entity protocol extension for @Entity macro
1 parent 04d82e6 commit 2b7a95d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Sources/CoreModel/Macros.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
@attached(member, names: arbitrary)
9+
@attached(extension, conformances: CoreModel.Entity)
910
public macro Entity(_ name: String? = nil) = #externalMacro(
1011
module: "CoreModelMacros",
1112
type: "EntityMacro"

Sources/CoreModelMacros/Entity.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import SwiftSyntaxMacros
1313
/// `@Entity` macro
1414
///
1515
/// Adds protocol conformance and implementaion for entity name.
16-
public struct EntityMacro: MemberMacro {
16+
public struct EntityMacro: MemberMacro, ExtensionMacro {
1717

1818
public static var expansionNames: [String] {
1919
[
@@ -23,6 +23,34 @@ public struct EntityMacro: MemberMacro {
2323
]
2424
}
2525

26+
// Add protocol conformance via extension
27+
public static func expansion(
28+
of node: AttributeSyntax,
29+
attachedTo declaration: some DeclGroupSyntax,
30+
providingExtensionsOf type: some TypeSyntaxProtocol,
31+
conformingTo protocols: [TypeSyntax],
32+
in context: some MacroExpansionContext
33+
) throws -> [ExtensionDeclSyntax] {
34+
let extensionDecl = ExtensionDeclSyntax(
35+
leadingTrivia: nil,
36+
attributes: [],
37+
modifiers: [],
38+
extensionKeyword: .keyword(.extension),
39+
extendedType: TypeSyntax(type),
40+
inheritanceClause: InheritanceClauseSyntax(
41+
colon: .colonToken(trailingTrivia: .space),
42+
inheritedTypes: InheritedTypeListSyntax {
43+
InheritedTypeSyntax(
44+
type: TypeSyntax(stringLiteral: "CoreModel.Entity")
45+
)
46+
}
47+
),
48+
genericWhereClause: nil,
49+
memberBlock: MemberBlockSyntax(members: [])
50+
)
51+
return [extensionDecl]
52+
}
53+
2654
public static func expansion(
2755
of node: AttributeSyntax,
2856
providingMembersOf declaration: some DeclGroupSyntax,

0 commit comments

Comments
 (0)