Skip to content

Commit f0ffd8c

Browse files
authored
Table macro fix for primary key trivia (#51)
* wip * wip
1 parent c851cb4 commit f0ffd8c

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

Sources/StructuredQueriesMacros/TableMacro.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ extension TableMacro: ExtensionMacro {
3434
var diagnostics: [Diagnostic] = []
3535

3636
// NB: A compiler bug prevents us from applying the '@_Draft' macro directly
37-
var draftBindings: [(PatternBindingSyntax, queryOutputType: TypeSyntax?)] = []
37+
var draftBindings: [
38+
(PatternBindingSyntax, queryOutputType: TypeSyntax?, optionalize: Bool)
39+
] = []
3840
// NB: End of workaround
3941

4042
var draftProperties: [DeclSyntax] = []
@@ -223,12 +225,8 @@ extension TableMacro: ExtensionMacro {
223225
)
224226
}
225227

226-
// NB: A compiled bug prevents us from applying the '@_Draft' macro directly
227-
if identifier == primaryKey?.identifier {
228-
draftBindings.append((binding.optionalized(), columnQueryOutputType))
229-
} else {
230-
draftBindings.append((binding, columnQueryOutputType))
231-
}
228+
// NB: A compiler bug prevents us from applying the '@_Draft' macro directly
229+
draftBindings.append((binding, columnQueryOutputType, identifier == primaryKey?.identifier))
232230
// NB: End of workaround
233231

234232
var assignedType: String? {
@@ -459,8 +457,12 @@ extension TableMacro: ExtensionMacro {
459457
.compactMap(\.memberBlock.members.trimmed)
460458
var memberwiseArguments: [PatternBindingSyntax] = []
461459
var memberwiseAssignments: [TokenSyntax] = []
462-
for (binding, queryOutputType) in draftBindings {
463-
let argument = binding.trimmed.annotated(queryOutputType).rewritten(selfRewriter)
460+
for (binding, queryOutputType, optionalize) in draftBindings {
461+
var argument = binding.trimmed
462+
if optionalize {
463+
argument = argument.optionalized()
464+
}
465+
argument = argument.annotated(queryOutputType).rewritten(selfRewriter)
464466
if argument.typeAnnotation == nil {
465467
let identifier =
466468
(argument.pattern.as(IdentifierPatternSyntax.self)?.identifier.trimmedDescription)

Tests/StructuredQueriesMacrosTests/TableMacroTests.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,5 +1703,75 @@ extension SnapshotTests {
17031703
"""#
17041704
}
17051705
}
1706+
1707+
@Test func commentAfterOptionalID() {
1708+
assertMacro {
1709+
"""
1710+
@Table
1711+
struct Reminder {
1712+
let id: Int? // TODO: Migrate to UUID
1713+
var title = ""
1714+
}
1715+
"""
1716+
} expansion: {
1717+
#"""
1718+
struct Reminder {
1719+
let id: Int? // TODO: Migrate to UUID
1720+
var title = ""
1721+
}
1722+
1723+
extension Reminder: StructuredQueriesCore.Table, StructuredQueriesCore.PrimaryKeyedTable {
1724+
public struct TableColumns: StructuredQueriesCore.TableDefinition, StructuredQueriesCore.PrimaryKeyedTableDefinition {
1725+
public typealias QueryValue = Reminder
1726+
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int?>("id", keyPath: \QueryValue.id)
1727+
public let title = StructuredQueriesCore.TableColumn<QueryValue, Swift.String>("title", keyPath: \QueryValue.title, default: "")
1728+
public var primaryKey: StructuredQueriesCore.TableColumn<QueryValue, Int?> {
1729+
self.id
1730+
}
1731+
public static var allColumns: [any StructuredQueriesCore.TableColumnExpression] {
1732+
[QueryValue.columns.id, QueryValue.columns.title]
1733+
}
1734+
}
1735+
public struct Draft: StructuredQueriesCore.TableDraft {
1736+
public typealias PrimaryTable = Reminder
1737+
@Column(primaryKey: false)
1738+
let id: Int?
1739+
var title = ""
1740+
public struct TableColumns: StructuredQueriesCore.TableDefinition {
1741+
public typealias QueryValue = Reminder.Draft
1742+
public let id = StructuredQueriesCore.TableColumn<QueryValue, Int?>("id", keyPath: \QueryValue.id)
1743+
public let title = StructuredQueriesCore.TableColumn<QueryValue, Swift.String>("title", keyPath: \QueryValue.title, default: "")
1744+
public static var allColumns: [any StructuredQueriesCore.TableColumnExpression] {
1745+
[QueryValue.columns.id, QueryValue.columns.title]
1746+
}
1747+
}
1748+
public static let columns = TableColumns()
1749+
public static let tableName = Reminder.tableName
1750+
public init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
1751+
self.id = try decoder.decode(Int.self)
1752+
self.title = try decoder.decode(Swift.String.self) ?? ""
1753+
}
1754+
public init(_ other: Reminder) {
1755+
self.id = other.id
1756+
self.title = other.title
1757+
}
1758+
public init(
1759+
id: Int? = nil,
1760+
title: Swift.String = ""
1761+
) {
1762+
self.id = id
1763+
self.title = title
1764+
}
1765+
}
1766+
public static let columns = TableColumns()
1767+
public static let tableName = "reminders"
1768+
public init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
1769+
self.id = try decoder.decode(Int.self)
1770+
self.title = try decoder.decode(Swift.String.self) ?? ""
1771+
}
1772+
}
1773+
"""#
1774+
}
1775+
}
17061776
}
17071777
}

0 commit comments

Comments
 (0)