Skip to content

Commit 34a57f2

Browse files
authored
Fix enum associated value decoding (#220)
* Revert "Test array type columns in EnumTableTests (#216)" This reverts commit 1447ea2. * Enum decoding fixes We had a bug in enum decoding that resulted in the decoder missing a few columns along the way.
1 parent 2e3cb76 commit 34a57f2

File tree

4 files changed

+55
-203
lines changed

4 files changed

+55
-203
lines changed

Package.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ let package = Package(
156156
swiftLanguageModes: [.v6]
157157
)
158158

159-
// NB: For local testing in Xcode:
160-
// if true {
161-
if ProcessInfo.processInfo.environment["SPI_GENERATE_DOCS"] != nil {
159+
if ProcessInfo.processInfo.environment["SPI_GENERATE_DOCS"] != nil
160+
|| ProcessInfo.processInfo.environment["CI"] != nil
161+
// NB: For local testing in Xcode:
162+
// || true
163+
{
162164
package.traits.insert(
163165
.default(
164166
enabledTraits: [

Sources/StructuredQueriesMacros/TableMacro.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ extension TableMacro: ExtensionMacro {
527527
"""
528528
} else if declaration.is(EnumDeclSyntax.self) {
529529
var decodings: [String] = []
530+
var decodingAssignments: [String] = []
530531
for member in declaration.memberBlock.members {
531532
guard let caseDecl = member.decl.as(EnumCaseDeclSyntax.self) else { continue }
532533
guard
@@ -685,7 +686,12 @@ extension TableMacro: ExtensionMacro {
685686
let decodedType = columnQueryValueType.asNonOptionalType()
686687
decodings.append(
687688
"""
688-
if let \(identifier) = try decoder.decode(\(decodedType).self) {
689+
let \(identifier) = try decoder.decode(\(decodedType).self)
690+
"""
691+
)
692+
decodingAssignments.append(
693+
"""
694+
if let \(identifier) {
689695
self = .\(identifier)(\(identifier))
690696
}
691697
"""
@@ -694,7 +700,8 @@ extension TableMacro: ExtensionMacro {
694700
initDecoder = """
695701
696702
public \(nonisolated)init(decoder: inout some \(moduleName).QueryDecoder) throws {
697-
\(raw: decodings.joined(separator: " else ")) else {
703+
\(raw: decodings.joined(separator: "\n"))
704+
\(raw: decodingAssignments.joined(separator: " else ")) else {
698705
throw \(moduleName).QueryDecodingError.missingRequiredColumn
699706
}
700707
}

Tests/StructuredQueriesMacrosTests/TableMacroTests.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,9 +2575,11 @@ extension SnapshotTests {
25752575
"posts"
25762576
}
25772577
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
2578-
if let photo = try decoder.decode(Photo.self) {
2578+
let photo = try decoder.decode(Photo.self)
2579+
let note = try decoder.decode(String.self)
2580+
if let photo {
25792581
self = .photo(photo)
2580-
} else if let note = try decoder.decode(String.self) {
2582+
} else if let note {
25812583
self = .note(note)
25822584
} else {
25832585
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
@@ -2682,9 +2684,11 @@ extension SnapshotTests {
26822684
"posts"
26832685
}
26842686
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
2685-
if let photo = try decoder.decode(Photo.self) {
2687+
let photo = try decoder.decode(Photo.self)
2688+
let note = try decoder.decode(String.self)
2689+
if let photo {
26862690
self = .photo(photo)
2687-
} else if let note = try decoder.decode(String.self) {
2691+
} else if let note {
26882692
self = .note(note)
26892693
} else {
26902694
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
@@ -2785,9 +2789,11 @@ extension SnapshotTests {
27852789
"posts"
27862790
}
27872791
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
2788-
if let photo = try decoder.decode(Photo.self) {
2792+
let photo = try decoder.decode(Photo.self)
2793+
let note = try decoder.decode(String.self)
2794+
if let photo {
27892795
self = .photo(photo)
2790-
} else if let note = try decoder.decode(String.self) {
2796+
} else if let note {
27912797
self = .note(note)
27922798
} else {
27932799
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
@@ -2873,9 +2879,11 @@ extension SnapshotTests {
28732879
"posts"
28742880
}
28752881
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
2876-
if let photo = try decoder.decode(Photo.self) {
2882+
let photo = try decoder.decode(Photo.self)
2883+
let note = try decoder.decode(String.self)
2884+
if let photo {
28772885
self = .photo(photo)
2878-
} else if let note = try decoder.decode(String.self) {
2886+
} else if let note {
28792887
self = .note(note)
28802888
} else {
28812889
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
@@ -2947,7 +2955,8 @@ extension SnapshotTests {
29472955
"posts"
29482956
}
29492957
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
2950-
if let note = try decoder.decode(String.self) {
2958+
let note = try decoder.decode(String.self)
2959+
if let note {
29512960
self = .note(note)
29522961
} else {
29532962
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn
@@ -3019,7 +3028,8 @@ extension SnapshotTests {
30193028
"posts"
30203029
}
30213030
public nonisolated init(decoder: inout some StructuredQueriesCore.QueryDecoder) throws {
3022-
if let timestamp = try decoder.decode(Date.UnixTimeRepresentation.self) {
3031+
let timestamp = try decoder.decode(Date.UnixTimeRepresentation.self)
3032+
if let timestamp {
30233033
self = .timestamp(timestamp)
30243034
} else {
30253035
throw StructuredQueriesCore.QueryDecodingError.missingRequiredColumn

0 commit comments

Comments
 (0)