Skip to content

Commit 67ca68b

Browse files
committed
Use StructuredQueriesCore module name in macro expansions. (#42)
1 parent e56c2ae commit 67ca68b

File tree

11 files changed

+513
-247
lines changed

11 files changed

+513
-247
lines changed

Sources/StructuredQueriesCore/PrimaryKeyed.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ public protocol PrimaryKeyedTable: Table where TableColumns: PrimaryKeyedTableDe
99
// A type representing a draft to be saved to a table with a primary key.
1010
public protocol TableDraft: Table {
1111
/// A type that represents the table with a primary key.
12-
associatedtype PrimaryTable: StructuredQueriesCore.PrimaryKeyedTable
13-
where PrimaryTable.Draft == Self
12+
associatedtype PrimaryTable: PrimaryKeyedTable where PrimaryTable.Draft == Self
1413

1514
/// Creates a draft from a primary keyed table.
1615
init(_ primaryTable: PrimaryTable)

Sources/StructuredQueriesCore/Seeds.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public struct Seeds: Sequence {
9292

9393
return insertBatch(firstType)
9494
} else {
95-
func insertBatch<T: StructuredQueriesCore.Table>(_: T.Type) -> SQLQueryExpression<Void> {
95+
func insertBatch<T: Table>(_: T.Type) -> SQLQueryExpression<Void> {
9696
let batch = Array(seeds.lazy.prefix { $0 is T }.compactMap { $0 as? T })
9797
defer { seeds.removeFirst(batch.count) }
9898
return SQLQueryExpression(T.insert(batch))

Sources/StructuredQueriesMacros/BindMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public enum BindMacro: ExpressionMacro {
99
) -> ExprSyntax {
1010
guard let argument = node.arguments.first?.expression else { fatalError() }
1111
let `as` = node.arguments.dropFirst().first
12-
return "StructuredQueries.BindQueryExpression(\(argument)\(raw: `as`.map { ", \($0)" } ?? ""))"
12+
return "\(moduleName).BindQueryExpression(\(argument)\(raw: `as`.map { ", \($0)" } ?? ""))"
1313
}
1414
}

Sources/StructuredQueriesMacros/Internal/Support.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SwiftSyntax
22
import SwiftSyntaxBuilder
33

4-
let moduleName: TokenSyntax = "StructuredQueries"
4+
let moduleName: TokenSyntax = "StructuredQueriesCore"
55

66
extension String {
77
func trimmingBackticks() -> String {

Sources/StructuredQueriesMacros/SQLMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,6 @@ public enum SQLMacro: ExpressionMacro {
207207
)
208208
)
209209
}
210-
return "StructuredQueries.SQLQueryExpression(\(argument))"
210+
return "\(moduleName).SQLQueryExpression(\(argument))"
211211
}
212212
}

Tests/StructuredQueriesMacrosTests/BindMacroTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension SnapshotTests {
1212
"""#
1313
} expansion: {
1414
#"""
15-
\(date) < StructuredQueries.BindQueryExpression(Date())
15+
\(date) < StructuredQueriesCore.BindQueryExpression(Date())
1616
"""#
1717
}
1818
}
@@ -24,7 +24,7 @@ extension SnapshotTests {
2424
"""#
2525
} expansion: {
2626
#"""
27-
\(date) < StructuredQueries.BindQueryExpression(Date(), as: Date.ISO8601Representation.self)
27+
\(date) < StructuredQueriesCore.BindQueryExpression(Date(), as: Date.ISO8601Representation.self)
2828
"""#
2929
}
3030
}

Tests/StructuredQueriesMacrosTests/SQLMacroTests.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension SnapshotTests {
1212
"""
1313
} expansion: {
1414
"""
15-
StructuredQueries.SQLQueryExpression("CURRENT_TIMESTAMP")
15+
StructuredQueriesCore.SQLQueryExpression("CURRENT_TIMESTAMP")
1616
"""
1717
}
1818
}
@@ -90,7 +90,7 @@ extension SnapshotTests {
9090
"""
9191
} expansion: {
9292
"""
93-
StructuredQueries.SQLQueryExpression("'('")
93+
StructuredQueriesCore.SQLQueryExpression("'('")
9494
"""
9595
}
9696
assertMacro {
@@ -99,7 +99,7 @@ extension SnapshotTests {
9999
"""
100100
} expansion: {
101101
"""
102-
StructuredQueries.SQLQueryExpression("[it's fine]")
102+
StructuredQueriesCore.SQLQueryExpression("[it's fine]")
103103
"""
104104
}
105105
}
@@ -169,7 +169,7 @@ extension SnapshotTests {
169169
"""
170170
} expansion: {
171171
"""
172-
StructuredQueries.SQLQueryExpression(#""text" = 'hello?'"#)
172+
StructuredQueriesCore.SQLQueryExpression(#""text" = 'hello?'"#)
173173
"""
174174
}
175175
assertMacro {
@@ -178,7 +178,7 @@ extension SnapshotTests {
178178
"""
179179
} expansion: {
180180
"""
181-
StructuredQueries.SQLQueryExpression(#""text" = 'hello?1'"#)
181+
StructuredQueriesCore.SQLQueryExpression(#""text" = 'hello?1'"#)
182182
"""
183183
}
184184
assertMacro {
@@ -187,7 +187,7 @@ extension SnapshotTests {
187187
"""
188188
} expansion: {
189189
"""
190-
StructuredQueries.SQLQueryExpression(#""text" = 'hello:hi'"#)
190+
StructuredQueriesCore.SQLQueryExpression(#""text" = 'hello:hi'"#)
191191
"""
192192
}
193193
assertMacro {
@@ -196,7 +196,7 @@ extension SnapshotTests {
196196
"""
197197
} expansion: {
198198
"""
199-
StructuredQueries.SQLQueryExpression(#""text" = 'hello@hi'"#)
199+
StructuredQueriesCore.SQLQueryExpression(#""text" = 'hello@hi'"#)
200200
"""
201201
}
202202
assertMacro {
@@ -205,7 +205,7 @@ extension SnapshotTests {
205205
"""
206206
} expansion: {
207207
"""
208-
StructuredQueries.SQLQueryExpression(#""text" = 'hello$hi'"#)
208+
StructuredQueriesCore.SQLQueryExpression(#""text" = 'hello$hi'"#)
209209
"""
210210
}
211211
}
@@ -228,7 +228,7 @@ extension SnapshotTests {
228228
"""#
229229
} expansion: {
230230
#"""
231-
StructuredQueries.SQLQueryExpression("'\(raw: 42)'")
231+
StructuredQueriesCore.SQLQueryExpression("'\(raw: 42)'")
232232
"""#
233233
}
234234
}
@@ -240,7 +240,7 @@ extension SnapshotTests {
240240
"""#
241241
} expansion: {
242242
#"""
243-
StructuredQueries.SQLQueryExpression("'\(raw: 42)'")
243+
StructuredQueriesCore.SQLQueryExpression("'\(raw: 42)'")
244244
"""#
245245
}
246246
}
@@ -252,7 +252,7 @@ extension SnapshotTests {
252252
"""#
253253
} expansion: {
254254
#"""
255-
StructuredQueries.SQLQueryExpression("\($0.dueDate) < date('now', '-\(raw: monthsAgo) months')")
255+
StructuredQueriesCore.SQLQueryExpression("\($0.dueDate) < date('now', '-\(raw: monthsAgo) months')")
256256
"""#
257257
}
258258
}
@@ -264,7 +264,7 @@ extension SnapshotTests {
264264
"""#
265265
} expansion: {
266266
"""
267-
StructuredQueries.SQLQueryExpression("''")
267+
StructuredQueriesCore.SQLQueryExpression("''")
268268
"""
269269
}
270270
assertMacro {
@@ -277,7 +277,7 @@ extension SnapshotTests {
277277
"""#
278278
} expansion: {
279279
#"""
280-
StructuredQueries.SQLQueryExpression(
280+
StructuredQueriesCore.SQLQueryExpression(
281281
"""
282282
""
283283
""")
@@ -289,7 +289,7 @@ extension SnapshotTests {
289289
"""#
290290
} expansion: {
291291
"""
292-
StructuredQueries.SQLQueryExpression("[]")
292+
StructuredQueriesCore.SQLQueryExpression("[]")
293293
"""
294294
}
295295
}
@@ -305,7 +305,7 @@ extension SnapshotTests {
305305
"""#
306306
} expansion: {
307307
#"""
308-
StructuredQueries.SQLQueryExpression(
308+
StructuredQueriesCore.SQLQueryExpression(
309309
"""
310310
SELECT 1 AS "a ""real"" one"
311311
""")
@@ -356,7 +356,7 @@ extension SnapshotTests {
356356
"""
357357
} expansion: {
358358
"""
359-
StructuredQueries.SQLQueryExpression("json_extract(notes, '$.body')")
359+
StructuredQueriesCore.SQLQueryExpression("json_extract(notes, '$.body')")
360360
"""
361361
}
362362
}

Tests/StructuredQueriesMacrosTests/SelectionMacroTests.swift

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ extension SnapshotTests {
2020
let team: Team
2121
}
2222
23-
extension PlayerAndTeam: StructuredQueries.QueryRepresentable {
24-
public struct Columns: StructuredQueries.QueryExpression {
23+
extension PlayerAndTeam: StructuredQueriesCore.QueryRepresentable {
24+
public struct Columns: StructuredQueriesCore.QueryExpression {
2525
public typealias QueryValue = PlayerAndTeam
26-
public let queryFragment: StructuredQueries.QueryFragment
26+
public let queryFragment: StructuredQueriesCore.QueryFragment
2727
public init(
28-
player: some StructuredQueries.QueryExpression<Player>,
29-
team: some StructuredQueries.QueryExpression<Team>
28+
player: some StructuredQueriesCore.QueryExpression<Player>,
29+
team: some StructuredQueriesCore.QueryExpression<Team>
3030
) {
3131
self.queryFragment = """
3232
\(player.queryFragment) AS "player", \(team.queryFragment) AS "team"
3333
"""
3434
}
3535
}
36-
public init(decoder: inout some StructuredQueries.QueryDecoder) throws {
36+
public init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
3737
let player = try decoder.decode(Player.self)
3838
let team = try decoder.decode(Team.self)
3939
guard let player else {
@@ -82,20 +82,20 @@ extension SnapshotTests {
8282
var listTitle: String?
8383
}
8484
85-
extension ReminderTitleAndListTitle: StructuredQueries.QueryRepresentable {
86-
public struct Columns: StructuredQueries.QueryExpression {
85+
extension ReminderTitleAndListTitle: StructuredQueriesCore.QueryRepresentable {
86+
public struct Columns: StructuredQueriesCore.QueryExpression {
8787
public typealias QueryValue = ReminderTitleAndListTitle
88-
public let queryFragment: StructuredQueries.QueryFragment
88+
public let queryFragment: StructuredQueriesCore.QueryFragment
8989
public init(
90-
reminderTitle: some StructuredQueries.QueryExpression<String>,
91-
listTitle: some StructuredQueries.QueryExpression<String?>
90+
reminderTitle: some StructuredQueriesCore.QueryExpression<String>,
91+
listTitle: some StructuredQueriesCore.QueryExpression<String?>
9292
) {
9393
self.queryFragment = """
9494
\(reminderTitle.queryFragment) AS "reminderTitle", \(listTitle.queryFragment) AS "listTitle"
9595
"""
9696
}
9797
}
98-
public init(decoder: inout some StructuredQueries.QueryDecoder) throws {
98+
public init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
9999
let reminderTitle = try decoder.decode(String.self)
100100
let listTitle = try decoder.decode(String.self)
101101
guard let reminderTitle else {
@@ -123,19 +123,74 @@ extension SnapshotTests {
123123
var date: Date
124124
}
125125
126-
extension ReminderDate: StructuredQueries.QueryRepresentable {
127-
public struct Columns: StructuredQueries.QueryExpression {
126+
extension ReminderDate: StructuredQueriesCore.QueryRepresentable {
127+
public struct Columns: StructuredQueriesCore.QueryExpression {
128128
public typealias QueryValue = ReminderDate
129-
public let queryFragment: StructuredQueries.QueryFragment
129+
public let queryFragment: StructuredQueriesCore.QueryFragment
130130
public init(
131-
date: some StructuredQueries.QueryExpression<Date.ISO8601Representation>
131+
date: some StructuredQueriesCore.QueryExpression<Date.ISO8601Representation>
132132
) {
133133
self.queryFragment = """
134134
\(date.queryFragment) AS "date"
135135
"""
136136
}
137137
}
138-
public init(decoder: inout some StructuredQueries.QueryDecoder) throws {
138+
public init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
139+
let date = try decoder.decode(Date.ISO8601Representation.self)
140+
guard let date else {
141+
throw QueryDecodingError.missingRequiredColumn
142+
}
143+
self.date = date
144+
}
145+
}
146+
"""#
147+
}
148+
}
149+
150+
@Test func dateDiagnostic() {
151+
assertMacro {
152+
"""
153+
@Selection struct ReminderDate {
154+
var date: Date
155+
}
156+
"""
157+
} diagnostics: {
158+
"""
159+
@Selection struct ReminderDate {
160+
var date: Date
161+
┬─────────────
162+
╰─ 🛑 'Date' column requires a query representation
163+
✏️ Insert '@Column(as: Date.ISO8601Representation.self)'
164+
✏️ Insert '@Column(as: Date.UnixTimeRepresentation.self)'
165+
✏️ Insert '@Column(as: Date.JulianDayRepresentation.self)'
166+
}
167+
"""
168+
} fixes: {
169+
"""
170+
@Selection struct ReminderDate {
171+
@Column(as: Date.ISO8601Representation.self)
172+
var date: Date
173+
}
174+
"""
175+
} expansion: {
176+
#"""
177+
struct ReminderDate {
178+
var date: Date
179+
}
180+
181+
extension ReminderDate: StructuredQueriesCore.QueryRepresentable {
182+
public struct Columns: StructuredQueriesCore.QueryExpression {
183+
public typealias QueryValue = ReminderDate
184+
public let queryFragment: StructuredQueriesCore.QueryFragment
185+
public init(
186+
date: some StructuredQueriesCore.QueryExpression<Date.ISO8601Representation>
187+
) {
188+
self.queryFragment = """
189+
\(date.queryFragment) AS "date"
190+
"""
191+
}
192+
}
193+
public init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
139194
let date = try decoder.decode(Date.ISO8601Representation.self)
140195
guard let date else {
141196
throw QueryDecodingError.missingRequiredColumn

0 commit comments

Comments
 (0)