Skip to content

Commit fb977e6

Browse files
committed
[Async] Initial port
1 parent ffb4fb4 commit fb977e6

File tree

14 files changed

+137
-108
lines changed

14 files changed

+137
-108
lines changed

Sources/PostgreSQL/Message/Backend.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,35 @@ enum BackendMessage {
3636
case commandComplete(CommandComplete)
3737
case error(Error)
3838

39-
init(from stream: StreamReader) throws {
40-
let rawType = try stream.read(UInt8.self)
39+
static func decode(from stream: StreamReader) async throws -> Self {
40+
let rawType = try await stream.read(UInt8.self)
4141
guard let messageType = RawType(rawValue: rawType) else {
4242
fatalError("unknown message type: \(rawType)")
4343
}
44-
self = try stream.withSubStreamReader(
44+
return try await stream.withSubStreamReader(
4545
sizedBy: Int32.self,
4646
includingHeader: true)
4747
{ stream in
4848
switch messageType {
4949
case .authentication:
50-
return .authentication(try .init(from: stream))
50+
return .authentication(try await .decode(from: stream))
5151
case .parameterStatus:
52-
return .parameterStatus(try .init(from: stream))
52+
return .parameterStatus(try await .decode(from: stream))
5353
case .backendKeyData:
54-
return .backendKeyData(try .init(from: stream))
54+
return .backendKeyData(try await .decode(from: stream))
5555
case .readyForQuery:
56-
return .readyForQuery(try .init(from: stream))
56+
return .readyForQuery(try await .decode(from: stream))
5757
case .rowDescription:
58-
return .rowDescription(try .init(from: stream))
58+
return .rowDescription(try await .decode(from: stream))
5959
case .dataRow:
60-
return .dataRow(try .init(from: stream))
60+
return .dataRow(try await .decode(from: stream))
6161
case .commandComplete:
62-
return .commandComplete(try .init(from: stream))
62+
return .commandComplete(try await .decode(from: stream))
6363
case .errorResponse:
64-
return .error(try .init(from: stream))
64+
return .error(try await .decode(from: stream))
6565
default:
6666
print("type: \(rawType) size: \(stream.limit)")
67-
print(try stream.readUntilEnd())
67+
print(try await stream.readUntilEnd())
6868
fatalError()
6969
}
7070
}

Sources/PostgreSQL/Message/Backend/Authentication.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ extension BackendMessage {
2020
case saslFinal = 12
2121
}
2222

23-
init(from stream: SubStreamReader) throws {
23+
static func decode(from stream: SubStreamReader) async throws -> Self {
2424
guard stream.limit == 4 else {
2525
fatalError("Authentication: invalid size")
2626
}
27-
let rawType = try stream.read(UInt32.self)
27+
let rawType = try await stream.read(UInt32.self)
2828
guard let status = RawType(rawValue: rawType) else {
2929
fatalError("Authentication: unknown status \(rawType)")
3030
}
3131
switch status {
32-
case .ok: self = .ok
32+
case .ok: return.ok
3333
// case .clearTextPassword: self = .clearTextPassword
3434
// case .md5Password: self = .md5Password
3535
default: fatalError("Authentication: unsupported method: \(status)")

Sources/PostgreSQL/Message/Backend/BackendKeyData.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ extension BackendMessage {
55
let processId: Int
66
let secretKey: Int
77

8-
init(from stream: SubStreamReader) throws {
8+
static func decode(from stream: SubStreamReader) async throws -> Self {
99
guard stream.limit == 8 else {
1010
fatalError("BackendKeyData: invalid size")
1111
}
12-
self.processId = Int(try stream.read(Int32.self))
13-
self.secretKey = Int(try stream.read(Int32.self))
12+
let processId = Int(try await stream.read(Int32.self))
13+
let secretKey = Int(try await stream.read(Int32.self))
14+
return .init(processId: processId, secretKey: secretKey)
1415
}
1516
}
1617
}

Sources/PostgreSQL/Message/Backend/CommandComplete.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ extension BackendMessage {
44
struct CommandComplete {
55
let tag: String
66

7-
init(from stream: SubStreamReader) throws {
8-
self.tag = try stream.readCString()
7+
static func decode(from stream: SubStreamReader) async throws -> Self {
8+
return .init(tag: try await stream.readCString())
99
}
1010
}
1111
}

Sources/PostgreSQL/Message/Backend/Error.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ extension BackendMessage {
88
let type: FieldType
99
let value: String
1010

11-
init(from stream: SubStreamReader) throws {
12-
let rawFieldType = Int(try stream.read(UInt8.self))
13-
self.type = Field.FieldType(rawValue: rawFieldType)
14-
self.value = try stream.readCString()
11+
static func decode(
12+
from stream: SubStreamReader
13+
) async throws -> Self {
14+
let rawFieldType = Int(try await stream.read(UInt8.self))
15+
let type = Field.FieldType(rawValue: rawFieldType)
16+
let value = try await stream.readCString()
17+
return .init(type: type, value: value)
1518
}
1619

1720
enum FieldType {
@@ -37,17 +40,17 @@ extension BackendMessage {
3740
}
3841
}
3942

40-
init(from stream: SubStreamReader) throws {
43+
static func decode(from stream: SubStreamReader) async throws -> Self {
4144
var fields = [Field]()
4245
while !stream.isEmpty {
4346
// end of error message
44-
guard try stream.peek() != 0 else {
45-
try stream.consume(count: 1)
47+
guard try await stream.peek() != 0 else {
48+
try await stream.consume(count: 1)
4649
break
4750
}
48-
fields.append(try .init(from: stream))
51+
fields.append(try await .decode(from: stream))
4952
}
50-
self.fields = fields
53+
return .init(fields: fields)
5154
}
5255
}
5356
}

Sources/PostgreSQL/Message/Backend/ParameterStatus.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ extension BackendMessage {
55
let name: String
66
let value: String
77

8-
init(from stream: SubStreamReader) throws {
9-
self.name = try stream.readCString()
10-
self.value = try stream.readCString()
8+
static func decode(from stream: SubStreamReader) async throws -> Self {
9+
let name = try await stream.readCString()
10+
let value = try await stream.readCString()
11+
return .init(name: name, value: value)
1112
}
1213
}
1314
}

Sources/PostgreSQL/Message/Backend/ReadyForQuery.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ extension BackendMessage {
66
case transaction = 84 // "T"
77
case error = 69 // "E"
88

9-
init(from stream: SubStreamReader) throws {
9+
static func decode(from stream: SubStreamReader) async throws -> Self {
1010
guard stream.limit == 1 else {
1111
fatalError("TransactionStatus: invalid size")
1212
}
13-
let rawStatus = try stream.read(Int8.self)
13+
let rawStatus = try await stream.read(Int8.self)
1414
guard let status = TransactionStatus(rawValue: rawStatus) else {
1515
fatalError("TransactionStatus: invalid status")
1616
}
17-
self = status
17+
return status
1818
}
1919
}
2020
}
2121

2222
extension BackendMessage.TransactionStatus: CustomStringConvertible {
23-
var description: String {
23+
public var description: String {
2424
switch self {
2525
case .idle: return "idle (not in a transaction block)"
2626
case .transaction: return "in a transaction block"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import Stream
22

33
protocol StreamEncodable {
4-
func encode(to stream: StreamWriter) throws
4+
func encode(to stream: StreamWriter) async throws
55
}
66

77
protocol StreamDecodable {
8-
func decode(from stream: StreamReader) throws
8+
func decode(from stream: StreamReader) async throws
99
}

Sources/PostgreSQL/Message/Frontend.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ enum FrontendMessage: StreamEncodable {
2020
case startup(Startup)
2121
case query(Query)
2222

23-
func encode(to stream: StreamWriter) throws {
23+
func encode(to stream: StreamWriter) async throws {
2424
switch self {
2525
case .startup(let message):
26-
try message.encode(to: stream)
26+
try await message.encode(to: stream)
2727
case .query(let message):
28-
try stream.write(UInt8(RawType.query.rawValue))
29-
try stream.withSubStreamWriter(
28+
try await stream.write(UInt8(RawType.query.rawValue))
29+
try await stream.withSubStreamWriter(
3030
sizedBy: Int32.self,
3131
includingHeader: true,
3232
task: message.encode)
@@ -54,17 +54,17 @@ enum FrontendMessage: StreamEncodable {
5454
self.replication = replication
5555
}
5656

57-
func encode(to stream: StreamWriter) throws {
58-
try stream.withSubStreamWriter(
57+
func encode(to stream: StreamWriter) async throws {
58+
try await stream.withSubStreamWriter(
5959
sizedBy: Int32.self,
6060
includingHeader: true)
6161
{ stream in
62-
try stream.write(PostgreSQL.protocolVersion)
63-
try stream.write(cString: "user")
64-
try stream.write(cString: user)
65-
try stream.write(cString: database ?? "")
62+
try await stream.write(PostgreSQL.protocolVersion)
63+
try await stream.write(cString: "user")
64+
try await stream.write(cString: user)
65+
try await stream.write(cString: database ?? "")
6666
}
67-
try stream.flush()
67+
try await stream.flush()
6868
}
6969
}
7070
}

Sources/PostgreSQL/Message/Frontend/DataRow.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import Stream
33
public struct DataRow {
44
public let values: [String]
55

6-
init(from stream: SubStreamReader) throws {
6+
static func decode(from stream: SubStreamReader) async throws -> Self {
77
var values = [String]()
88

9-
var fieldsCount = Int(try stream.read(Int16.self))
9+
var fieldsCount = Int(try await stream.read(Int16.self))
1010
while fieldsCount > 0 {
11-
let length = Int(try stream.read(Int32.self))
12-
values.append(try stream.read(count: length, as: String.self))
11+
let length = Int(try await stream.read(Int32.self))
12+
values.append(try await stream.read(count: length, as: String.self))
1313
fieldsCount -= 1
1414
}
1515

16-
self.values = values
16+
return .init(values: values)
1717
}
1818
}
1919

0 commit comments

Comments
 (0)