Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ let package = Package(
swiftLanguageModes: [.v6]
)

// NB: For local testing in Xcode:
// if true {
if ProcessInfo.processInfo.environment["SPI_GENERATE_DOCS"] != nil {
if ProcessInfo.processInfo.environment["SPI_GENERATE_DOCS"] != nil
|| ProcessInfo.processInfo.environment["CI"] != nil
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's ensure enum tests are actually running in CI.

// NB: For local testing in Xcode:
// || true
{
package.traits.insert(
.default(
enabledTraits: [
Expand Down
11 changes: 9 additions & 2 deletions Sources/StructuredQueriesMacros/TableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ extension TableMacro: ExtensionMacro {
"""
} else if declaration.is(EnumDeclSyntax.self) {
var decodings: [String] = []
var decodingAssignments: [String] = []
for member in declaration.memberBlock.members {
guard let caseDecl = member.decl.as(EnumCaseDeclSyntax.self) else { continue }
guard
Expand Down Expand Up @@ -685,7 +686,12 @@ extension TableMacro: ExtensionMacro {
let decodedType = columnQueryValueType.asNonOptionalType()
decodings.append(
"""
if let \(identifier) = try decoder.decode(\(decodedType).self) {
let \(identifier) = try decoder.decode(\(decodedType).self)
"""
)
decodingAssignments.append(
"""
if let \(identifier) {
self = .\(identifier)(\(identifier))
}
"""
Expand All @@ -694,7 +700,8 @@ extension TableMacro: ExtensionMacro {
initDecoder = """

public \(nonisolated)init(decoder: inout some \(moduleName).QueryDecoder) throws {
\(raw: decodings.joined(separator: " else ")) else {
\(raw: decodings.joined(separator: "\n"))
\(raw: decodingAssignments.joined(separator: " else ")) else {
throw \(moduleName).QueryDecodingError.missingRequiredColumn
}
}
Expand Down
30 changes: 20 additions & 10 deletions Tests/StructuredQueriesMacrosTests/TableMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2575,9 +2575,11 @@ extension SnapshotTests {
"posts"
}
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
if let photo = try decoder.decode(Photo.self) {
let photo = try decoder.decode(Photo.self)
let note = try decoder.decode(String.self)
Comment on lines +2578 to +2579
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to call decoder.decode for every type so that the query decoder's cursor advances to the proper column offset.

if let photo {
self = .photo(photo)
} else if let note = try decoder.decode(String.self) {
} else if let note {
self = .note(note)
} else {
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
Expand Down Expand Up @@ -2682,9 +2684,11 @@ extension SnapshotTests {
"posts"
}
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
if let photo = try decoder.decode(Photo.self) {
let photo = try decoder.decode(Photo.self)
let note = try decoder.decode(String.self)
if let photo {
self = .photo(photo)
} else if let note = try decoder.decode(String.self) {
} else if let note {
self = .note(note)
} else {
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
Expand Down Expand Up @@ -2785,9 +2789,11 @@ extension SnapshotTests {
"posts"
}
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
if let photo = try decoder.decode(Photo.self) {
let photo = try decoder.decode(Photo.self)
let note = try decoder.decode(String.self)
if let photo {
self = .photo(photo)
} else if let note = try decoder.decode(String.self) {
} else if let note {
self = .note(note)
} else {
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
Expand Down Expand Up @@ -2873,9 +2879,11 @@ extension SnapshotTests {
"posts"
}
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
if let photo = try decoder.decode(Photo.self) {
let photo = try decoder.decode(Photo.self)
let note = try decoder.decode(String.self)
if let photo {
self = .photo(photo)
} else if let note = try decoder.decode(String.self) {
} else if let note {
self = .note(note)
} else {
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
Expand Down Expand Up @@ -2947,7 +2955,8 @@ extension SnapshotTests {
"posts"
}
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
if let note = try decoder.decode(String.self) {
let note = try decoder.decode(String.self)
if let note {
self = .note(note)
} else {
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
Expand Down Expand Up @@ -3019,7 +3028,8 @@ extension SnapshotTests {
"posts"
}
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
if let timestamp = try decoder.decode(Date.UnixTimeRepresentation.self) {
let timestamp = try decoder.decode(Date.UnixTimeRepresentation.self)
if let timestamp {
self = .timestamp(timestamp)
} else {
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
Expand Down
Loading