Skip to content

Commit 04d82e6

Browse files
authored
Merge pull request #12 from PureSwift/feature/macros
Add Macros
2 parents ed4e1ff + a9bc033 commit 04d82e6

File tree

13 files changed

+613
-180
lines changed

13 files changed

+613
-180
lines changed

.github/workflows/swift.yml

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,37 @@
11
name: Swift
2-
32
on: [push]
4-
53
jobs:
6-
build:
7-
name: Build
8-
strategy:
9-
matrix:
10-
swift: [6.0.3]
11-
os: [ubuntu-20.04, macos-latest]
12-
runs-on: ${{ matrix.os }}
4+
5+
macos:
6+
name: macOS
7+
runs-on: macos-15
138
steps:
14-
- name: Install Swift
15-
uses: slashmo/[email protected]
16-
with:
17-
version: ${{ matrix.swift }}
189
- name: Checkout
19-
uses: actions/checkout@v2
10+
uses: actions/checkout@v4
2011
- name: Swift Version
2112
run: swift --version
2213
- name: Build (Debug)
2314
run: swift build -c debug
2415
- name: Build (Release)
2516
run: swift build -c release
26-
27-
test:
28-
name: Test
17+
- name: Test (Debug)
18+
run: swift test -c debug
19+
20+
linux:
21+
name: Linux
2922
strategy:
3023
matrix:
31-
swift: [6.0.3]
32-
os: [ubuntu-20.04, macos-latest]
33-
runs-on: ${{ matrix.os }}
24+
container: ["swift:6.0.3", "swiftlang/swift:nightly"]
25+
runs-on: ubuntu-latest
26+
container: ${{ matrix.container }}-jammy
3427
steps:
35-
- name: Install Swift
36-
uses: slashmo/[email protected]
37-
with:
38-
version: ${{ matrix.swift }}
3928
- name: Checkout
40-
uses: actions/checkout@v2
29+
uses: actions/checkout@v4
4130
- name: Swift Version
4231
run: swift --version
32+
- name: Build (Debug)
33+
run: swift build -c debug
34+
- name: Build (Release)
35+
run: swift build -c release
4336
- name: Test (Debug)
44-
run: swift test --configuration debug
37+
run: swift test -c debug

Package.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// swift-tools-version:6.0
22
import PackageDescription
3+
import CompilerPluginSupport
34

45
let package = Package(
56
name: "CoreModel",
@@ -23,9 +24,18 @@ let package = Package(
2324
]
2425
)
2526
],
27+
dependencies: [
28+
.package(
29+
url: "https://github.com/apple/swift-syntax.git",
30+
from: "600.0.1"
31+
)
32+
],
2633
targets: [
2734
.target(
28-
name: "CoreModel"
35+
name: "CoreModel",
36+
dependencies: [
37+
"CoreModelMacros"
38+
]
2939
),
3040
.target(
3141
name: "CoreDataModel",
@@ -36,10 +46,24 @@ let package = Package(
3646
.swiftLanguageMode(.v5)
3747
]
3848
),
49+
.macro(
50+
name: "CoreModelMacros",
51+
dependencies: [
52+
.product(
53+
name: "SwiftSyntaxMacros",
54+
package: "swift-syntax"
55+
),
56+
.product(
57+
name: "SwiftCompilerPlugin",
58+
package: "swift-syntax"
59+
)
60+
]
61+
),
3962
.testTarget(
4063
name: "CoreModelTests",
4164
dependencies: [
4265
"CoreModel",
66+
"CoreModelMacros",
4367
.byName(
4468
name: "CoreDataModel",
4569
condition: .when(platforms: [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS])

Sources/CoreModel/Macros.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Macros.swift
3+
// CoreModel
4+
//
5+
// Created by Alsey Coleman Miller on 6/17/25.
6+
//
7+
8+
@attached(member, names: arbitrary)
9+
public macro Entity(_ name: String? = nil) = #externalMacro(
10+
module: "CoreModelMacros",
11+
type: "EntityMacro"
12+
)
13+
14+
@attached(peer)
15+
public macro Attribute(_ type: AttributeType? = nil) = #externalMacro(
16+
module: "CoreModelMacros",
17+
type: "AttributeMacro"
18+
)
19+
20+
@attached(peer)
21+
public macro Relationship<T: CoreModel.Entity>(destination: T.Type, inverse: T.CodingKeys) = #externalMacro(
22+
module: "CoreModelMacros",
23+
type: "RelationshipMacro"
24+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Attribute.swift
3+
// CoreModel
4+
//
5+
// Created by Alsey Coleman Miller on 6/17/25.
6+
//
7+
8+
import Foundation
9+
import SwiftSyntax
10+
import SwiftSyntaxBuilder
11+
import SwiftSyntaxMacros
12+
13+
public struct AttributeMacro: PeerMacro {
14+
public static func expansion(
15+
of node: AttributeSyntax,
16+
providingPeersOf declaration: some DeclSyntaxProtocol,
17+
in context: some MacroExpansionContext
18+
) throws -> [DeclSyntax] {
19+
20+
// Tag only, logic handled in EntityMacro
21+
return []
22+
}
23+
}
24+
25+
internal func inferAttributeType(from type: String) -> String? {
26+
switch type {
27+
case "String": return ".string"
28+
case "Data": return ".data"
29+
case "Bool": return ".bool"
30+
case "Int16": return ".int16"
31+
case "Int32": return ".int32"
32+
case "Int64": return ".int64"
33+
case "Int": return ".int64"
34+
case "Float": return ".float"
35+
case "Double": return ".double"
36+
case "Date": return ".date"
37+
case "UUID": return ".uuid"
38+
case "URL": return ".url"
39+
case "Decimal": return ".decimal"
40+
default: return nil
41+
}
42+
}

0 commit comments

Comments
 (0)