Skip to content

Commit 05bd99f

Browse files
committed
Variadic Generics
Reduce Run tests Even better Use variadic generics Keep stuff
1 parent dbf9c2e commit 05bd99f

File tree

6 files changed

+233
-9
lines changed

6 files changed

+233
-9
lines changed

[email protected]

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// swift-tools-version:5.9
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "postgres-nio",
6+
platforms: [
7+
.macOS(.v10_15),
8+
.iOS(.v13),
9+
.watchOS(.v6),
10+
.tvOS(.v13),
11+
],
12+
products: [
13+
.library(name: "PostgresNIO", targets: ["PostgresNIO"]),
14+
],
15+
dependencies: [
16+
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.2"),
17+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.44.0"),
18+
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.13.1"),
19+
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.22.1"),
20+
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
21+
.package(url: "https://github.com/apple/swift-metrics.git", from: "2.0.0"),
22+
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.4"),
23+
],
24+
targets: [
25+
.target(
26+
name: "PostgresNIO",
27+
dependencies: [
28+
.product(name: "Atomics", package: "swift-atomics"),
29+
.product(name: "Crypto", package: "swift-crypto"),
30+
.product(name: "Logging", package: "swift-log"),
31+
.product(name: "Metrics", package: "swift-metrics"),
32+
.product(name: "NIO", package: "swift-nio"),
33+
.product(name: "NIOCore", package: "swift-nio"),
34+
.product(name: "NIOPosix", package: "swift-nio"),
35+
.product(name: "NIOTransportServices", package: "swift-nio-transport-services"),
36+
.product(name: "NIOTLS", package: "swift-nio"),
37+
.product(name: "NIOSSL", package: "swift-nio-ssl"),
38+
.product(name: "NIOFoundationCompat", package: "swift-nio"),
39+
],
40+
swiftSettings: [.enableExperimentalFeature("VariadicGenerics")]
41+
),
42+
.testTarget(
43+
name: "PostgresNIOTests",
44+
dependencies: [
45+
.target(name: "PostgresNIO"),
46+
.product(name: "NIOEmbedded", package: "swift-nio"),
47+
.product(name: "NIOTestUtils", package: "swift-nio"),
48+
],
49+
swiftSettings: [.enableExperimentalFeature("VariadicGenerics")]
50+
),
51+
.testTarget(
52+
name: "IntegrationTests",
53+
dependencies: [
54+
.target(name: "PostgresNIO"),
55+
.product(name: "NIOTestUtils", package: "swift-nio"),
56+
],
57+
swiftSettings: [.enableExperimentalFeature("VariadicGenerics")]
58+
),
59+
]
60+
)

Sources/PostgresNIO/New/PostgresRow-multi-decode.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// NOTE: THIS FILE IS AUTO-GENERATED BY dev/generate-postgresrow-multi-decode.sh
22

3+
#if compiler(<5.9)
34
extension PostgresRow {
45
@inlinable
56
@_alwaysEmitIntoClient
@@ -1171,3 +1172,4 @@ extension PostgresRow {
11711172
try self.decode((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14).self, context: .default, file: file, line: line)
11721173
}
11731174
}
1175+
#endif

Sources/PostgresNIO/New/PostgresRowSequence-multi-decode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// NOTE: THIS FILE IS AUTO-GENERATED BY dev/generate-postgresrowsequence-multi-decode.sh
22

3-
#if canImport(_Concurrency)
3+
#if compiler(<5.9)
44
extension AsyncSequence where Element == PostgresRow {
55
@inlinable
66
@_alwaysEmitIntoClient
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#if compiler(>=5.9)
2+
#if hasFeature(VariadicGenerics)
3+
extension PostgresRow {
4+
5+
// --- snip TODO: Remove once bug is fixed, that disallows tuples of one
6+
@inlinable
7+
public func decode<Column: PostgresDecodable>(
8+
_: Column.Type,
9+
file: String = #fileID,
10+
line: Int = #line
11+
) throws -> (Column) {
12+
try self.decode(Column.self, context: .default, file: file, line: line)
13+
}
14+
15+
@inlinable
16+
public func decode<Column: PostgresDecodable>(
17+
_: Column.Type,
18+
context: PostgresDecodingContext<some PostgresJSONDecoder>,
19+
file: String = #fileID,
20+
line: Int = #line
21+
) throws -> (Column) {
22+
precondition(self.columns.count >= 1)
23+
let columnIndex = 0
24+
var cellIterator = self.data.makeIterator()
25+
var cellData = cellIterator.next().unsafelyUnwrapped
26+
var columnIterator = self.columns.makeIterator()
27+
let column = columnIterator.next().unsafelyUnwrapped
28+
let swiftTargetType: Any.Type = Column.self
29+
30+
do {
31+
let r0 = try Column._decodeRaw(from: &cellData, type: column.dataType, format: column.format, context: context)
32+
33+
return (r0)
34+
} catch let code as PostgresDecodingError.Code {
35+
throw PostgresDecodingError(
36+
code: code,
37+
columnName: column.name,
38+
columnIndex: columnIndex,
39+
targetType: swiftTargetType,
40+
postgresType: column.dataType,
41+
postgresFormat: column.format,
42+
postgresData: cellData,
43+
file: file,
44+
line: line
45+
)
46+
}
47+
}
48+
// --- snap TODO: Remove once bug is fixed, that disallows tuples of one
49+
50+
// @inlinable // <--- commenting this in, crashes the compiler
51+
public func decode<each Column: PostgresDecodable>(
52+
_ columnType: (repeat each Column).Type,
53+
context: PostgresDecodingContext<some PostgresJSONDecoder>,
54+
file: String = #fileID,
55+
line: Int = #line
56+
) throws -> (repeat each Column) {
57+
var columnIndex = 0
58+
var cellIterator = self.data.makeIterator()
59+
var columnIterator = self.columns.makeIterator()
60+
61+
return (
62+
repeat try Self.decodeNextColumn(
63+
(each Column).self,
64+
cellIterator: &cellIterator,
65+
columnIterator: &columnIterator,
66+
columnIndex: &columnIndex,
67+
context: context,
68+
file: file,
69+
line: line
70+
)
71+
)
72+
}
73+
74+
@inlinable
75+
static func decodeNextColumn<Column: PostgresDecodable>(
76+
_ columnType: Column.Type,
77+
cellIterator: inout IndexingIterator<DataRow>,
78+
columnIterator: inout IndexingIterator<[RowDescription.Column]>,
79+
columnIndex: inout Int,
80+
context: PostgresDecodingContext<some PostgresJSONDecoder>,
81+
file: String,
82+
line: Int
83+
) throws -> Column {
84+
defer { columnIndex += 1 }
85+
86+
let column = columnIterator.next().unsafelyUnwrapped
87+
var cellData = cellIterator.next().unsafelyUnwrapped
88+
do {
89+
return try Column._decodeRaw(from: &cellData, type: column.dataType, format: column.format, context: context)
90+
} catch let code as PostgresDecodingError.Code {
91+
throw PostgresDecodingError(
92+
code: code,
93+
columnName: column.name,
94+
columnIndex: columnIndex,
95+
targetType: Column.self,
96+
postgresType: column.dataType,
97+
postgresFormat: column.format,
98+
postgresData: cellData,
99+
file: file,
100+
line: line
101+
)
102+
}
103+
}
104+
105+
// @inlinable // <--- commenting this in, crashes the compiler
106+
public func decode<each Column: PostgresDecodable>(
107+
_ columnType: (repeat each Column).Type,
108+
file: String = #fileID,
109+
line: Int = #line
110+
) throws -> (repeat each Column) {
111+
try self.decode(columnType, context: .default, file: file, line: line)
112+
}
113+
}
114+
115+
extension AsyncSequence where Element == PostgresRow {
116+
@inlinable
117+
public func decode<Column: PostgresDecodable>(
118+
_: Column.Type,
119+
context: PostgresDecodingContext<some PostgresJSONDecoder>,
120+
file: String = #fileID,
121+
line: Int = #line
122+
) -> AsyncThrowingMapSequence<Self, (Column)> {
123+
self.map { row in
124+
try row.decode(Column.self, context: context, file: file, line: line)
125+
}
126+
}
127+
128+
@inlinable
129+
public func decode<Column: PostgresDecodable>(
130+
_: Column.Type,
131+
file: String = #fileID,
132+
line: Int = #line
133+
) -> AsyncThrowingMapSequence<Self, (Column)> {
134+
self.decode(Column.self, context: .default, file: file, line: line)
135+
}
136+
137+
#if false // commented out since, the AsyncSequence map currently crashes the compiler
138+
public func decode<each Column: PostgresDecodable>(
139+
_ columnType: (repeat each Column).Type,
140+
context: PostgresDecodingContext<some PostgresJSONDecoder>,
141+
file: String = #fileID,
142+
line: Int = #line
143+
) throws -> AsyncThrowingMapSequence<Self, (repeat each Column)> {
144+
self.map { row in
145+
try row.decode(columnType, context: context, file: file, line: line)
146+
}
147+
}
148+
149+
public func decode<each Column: PostgresDecodable>(
150+
_ columnType: (repeat each Column).Type,
151+
file: String = #fileID,
152+
line: Int = #line
153+
) throws -> AsyncThrowingMapSequence<Self, (repeat each Column)> {
154+
try self.decode(columnType, context: .default, file: file, line: line)
155+
}
156+
#endif // AsyncSequence extension
157+
}
158+
159+
#endif // hasFeature
160+
#endif // compiler(>=5.9)

Tests/IntegrationTests/AsyncTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ final class AsyncPostgresConnectionTests: XCTestCase {
3737
try await withTestConnection(on: eventLoop) { connection in
3838
let rows = try await connection.query("SELECT generate_series(\(start), \(end));", logger: .psqlTest)
3939
var counter = 0
40-
for try await element in rows.decode(Int.self, context: .default) {
40+
for try await row in rows {
41+
let element = try row.decode(Int.self)
4142
XCTAssertEqual(element, counter + 1)
4243
counter += 1
4344
}
@@ -200,7 +201,8 @@ final class AsyncPostgresConnectionTests: XCTestCase {
200201
try await withTestConnection(on: eventLoop) { connection in
201202
let rows = try await connection.query("SELECT generate_series(\(start), \(end));", logger: .psqlTest)
202203
var counter = 1
203-
for try await element in rows.decode(Int.self, context: .default) {
204+
for try await row in rows {
205+
let element = try row.decode(Int.self, context: .default)
204206
XCTAssertEqual(element, counter)
205207
counter += 1
206208
}

Tests/PostgresNIOTests/New/PostgresRowSequenceTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ final class PostgresRowSequenceTests: XCTestCase {
6060

6161
var counter = 0
6262
for try await row in rowSequence {
63-
XCTAssertEqual(try row.decode(Int.self, context: .default), counter)
63+
XCTAssertEqual(try row.decode(Int.self), counter)
6464
counter += 1
6565

6666
if counter == 64 {
@@ -142,7 +142,7 @@ final class PostgresRowSequenceTests: XCTestCase {
142142

143143
var counter = 0
144144
for try await row in rowSequence {
145-
XCTAssertEqual(try row.decode(Int.self, context: .default), counter)
145+
XCTAssertEqual(try row.decode(Int.self), counter)
146146
counter += 1
147147
}
148148

@@ -172,7 +172,7 @@ final class PostgresRowSequenceTests: XCTestCase {
172172

173173
var counter = 0
174174
for try await row in rowSequence {
175-
XCTAssertEqual(try row.decode(Int.self, context: .default), counter)
175+
XCTAssertEqual(try row.decode(Int.self), counter)
176176
counter += 1
177177
}
178178

@@ -233,7 +233,7 @@ final class PostgresRowSequenceTests: XCTestCase {
233233
}
234234

235235
let row1 = try await rowIterator.next()
236-
XCTAssertEqual(try row1?.decode(Int.self, context: .default), 0)
236+
XCTAssertEqual(try row1?.decode(Int.self), 0)
237237

238238
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
239239
stream.receive(completion: .success("SELECT 1"))
@@ -267,7 +267,7 @@ final class PostgresRowSequenceTests: XCTestCase {
267267
}
268268

269269
let row1 = try await rowIterator.next()
270-
XCTAssertEqual(try row1?.decode(Int.self, context: .default), 0)
270+
XCTAssertEqual(try row1?.decode(Int.self), 0)
271271

272272
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
273273
stream.receive(completion: .failure(PSQLError.connectionClosed))
@@ -436,7 +436,7 @@ final class PostgresRowSequenceTests: XCTestCase {
436436
var counter = 1
437437
for _ in 0..<(2 * messagePerChunk - 1) {
438438
let row = try await rowIterator.next()
439-
XCTAssertEqual(try row?.decode(Int.self, context: .default), counter)
439+
XCTAssertEqual(try row?.decode(Int.self), counter)
440440
counter += 1
441441
}
442442

0 commit comments

Comments
 (0)