Skip to content

Commit bae1116

Browse files
authored
Cleanup PostgresBackendMessage (#384)
1 parent dcf78b0 commit bae1116

19 files changed

+57
-179
lines changed

Sources/PostgresNIO/New/Connection State Machine/ConnectionStateMachine.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,18 +1258,7 @@ struct AuthContext: Equatable, CustomDebugStringConvertible {
12581258

12591259
enum PasswordAuthencationMode: Equatable {
12601260
case cleartext
1261-
case md5(salt: (UInt8, UInt8, UInt8, UInt8))
1262-
1263-
static func ==(lhs: Self, rhs: Self) -> Bool {
1264-
switch (lhs, rhs) {
1265-
case (.cleartext, .cleartext):
1266-
return true
1267-
case (.md5(let lhs), .md5(let rhs)):
1268-
return lhs == rhs
1269-
default:
1270-
return false
1271-
}
1272-
}
1261+
case md5(salt: UInt32)
12731262
}
12741263

12751264
extension ConnectionStateMachine.State: CustomDebugStringConvertible {

Sources/PostgresNIO/New/Messages/Authentication.swift

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import NIOCore
22

33
extension PostgresBackendMessage {
44

5-
enum Authentication: PayloadDecodable {
5+
enum Authentication: PayloadDecodable, Hashable {
66
case ok
77
case kerberosV5
8-
case md5(salt: (UInt8, UInt8, UInt8, UInt8))
8+
case md5(salt: UInt32)
99
case plaintext
1010
case scmCredential
1111
case gss
@@ -26,7 +26,7 @@ extension PostgresBackendMessage {
2626
case 3:
2727
return .plaintext
2828
case 5:
29-
guard let salt = buffer.readMultipleIntegers(endianness: .big, as: (UInt8, UInt8, UInt8, UInt8).self) else {
29+
guard let salt = buffer.readInteger(as: UInt32.self) else {
3030
throw PSQLPartialDecodingError.expectedAtLeastNRemainingBytes(4, actual: buffer.readableBytes)
3131
}
3232
return .md5(salt: salt)
@@ -61,37 +61,6 @@ extension PostgresBackendMessage {
6161
}
6262
}
6363

64-
extension PostgresBackendMessage.Authentication: Equatable {
65-
static func ==(lhs: Self, rhs: Self) -> Bool {
66-
switch (lhs, rhs) {
67-
case (.ok, .ok):
68-
return true
69-
case (.kerberosV5, .kerberosV5):
70-
return true
71-
case (.md5(let lhs), .md5(let rhs)):
72-
return lhs == rhs
73-
case (.plaintext, .plaintext):
74-
return true
75-
case (.scmCredential, .scmCredential):
76-
return true
77-
case (.gss, .gss):
78-
return true
79-
case (.sspi, .sspi):
80-
return true
81-
case (.gssContinue(let lhs), .gssContinue(let rhs)):
82-
return lhs == rhs
83-
case (.sasl(let lhs), .sasl(let rhs)):
84-
return lhs == rhs
85-
case (.saslContinue(let lhs), .saslContinue(let rhs)):
86-
return lhs == rhs
87-
case (.saslFinal(let lhs), .saslFinal(let rhs)):
88-
return lhs == rhs
89-
default:
90-
return false
91-
}
92-
}
93-
}
94-
9564
extension PostgresBackendMessage.Authentication: CustomDebugStringConvertible {
9665
var debugDescription: String {
9766
switch self {

Sources/PostgresNIO/New/Messages/BackendKeyData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import NIOCore
22

33
extension PostgresBackendMessage {
44

5-
struct BackendKeyData: PayloadDecodable, Equatable {
5+
struct BackendKeyData: PayloadDecodable, Hashable {
66
let processID: Int32
77
let secretKey: Int32
88

Sources/PostgresNIO/New/Messages/DataRow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import NIOCore
99
/// Not putting `DataRow` in ``PSQLBackendMessage`` is our way to trick
1010
/// the Swift compiler
1111
@usableFromInline
12-
struct DataRow: Sendable, PostgresBackendMessage.PayloadDecodable, Equatable {
12+
struct DataRow: Sendable, PostgresBackendMessage.PayloadDecodable, Hashable {
1313
@usableFromInline
1414
var columnCount: Int16
1515
@usableFromInline

Sources/PostgresNIO/New/Messages/ErrorResponse.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ extension PostgresBackendMessage {
8080
case routine = 0x52 /// R
8181
}
8282

83-
struct ErrorResponse: PSQLMessageNotice, PayloadDecodable, Equatable {
83+
struct ErrorResponse: PSQLMessageNotice, PayloadDecodable, Hashable {
8484
let fields: [PostgresBackendMessage.Field: String]
8585

8686
init(fields: [PostgresBackendMessage.Field: String]) {
8787
self.fields = fields
8888
}
8989
}
9090

91-
struct NoticeResponse: PSQLMessageNotice, PayloadDecodable, Equatable {
91+
struct NoticeResponse: PSQLMessageNotice, PayloadDecodable, Hashable {
9292
let fields: [PostgresBackendMessage.Field: String]
9393

9494
init(fields: [PostgresBackendMessage.Field: String]) {

Sources/PostgresNIO/New/Messages/NotificationResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import NIOCore
22

33
extension PostgresBackendMessage {
44

5-
struct NotificationResponse: PayloadDecodable, Equatable {
5+
struct NotificationResponse: PayloadDecodable, Hashable {
66
let backendPID: Int32
77
let channel: String
88
let payload: String

Sources/PostgresNIO/New/Messages/ParameterDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import NIOCore
22

33
extension PostgresBackendMessage {
44

5-
struct ParameterDescription: PayloadDecodable, Equatable {
5+
struct ParameterDescription: PayloadDecodable, Hashable {
66
/// Specifies the object ID of the parameter data type.
77
var dataTypes: [PostgresDataType]
88

Sources/PostgresNIO/New/Messages/ParameterStatus.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import NIOCore
22

33
extension PostgresBackendMessage {
44

5-
struct ParameterStatus: PayloadDecodable, Equatable {
5+
struct ParameterStatus: PayloadDecodable, Hashable {
66
/// The name of the run-time parameter being reported.
77
var parameter: String
88

Sources/PostgresNIO/New/Messages/ReadyForQuery.swift

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
import NIOCore
22

33
extension PostgresBackendMessage {
4-
enum TransactionState: PayloadDecodable, RawRepresentable {
5-
typealias RawValue = UInt8
6-
7-
case idle
8-
case inTransaction
9-
case inFailedTransaction
10-
11-
init?(rawValue: UInt8) {
12-
switch rawValue {
13-
case UInt8(ascii: "I"):
14-
self = .idle
15-
case UInt8(ascii: "T"):
16-
self = .inTransaction
17-
case UInt8(ascii: "E"):
18-
self = .inFailedTransaction
19-
default:
20-
return nil
21-
}
22-
}
4+
enum TransactionState: UInt8, PayloadDecodable, Hashable {
5+
case idle = 73 // ascii: I
6+
case inTransaction = 84 // ascii: T
7+
case inFailedTransaction = 69 // ascii: E
238

24-
var rawValue: Self.RawValue {
25-
switch self {
26-
case .idle:
27-
return UInt8(ascii: "I")
28-
case .inTransaction:
29-
return UInt8(ascii: "T")
30-
case .inFailedTransaction:
31-
return UInt8(ascii: "E")
32-
}
33-
}
34-
359
static func decode(from buffer: inout ByteBuffer) throws -> Self {
3610
let value = try buffer.throwingReadInteger(as: UInt8.self)
3711
guard let state = Self.init(rawValue: value) else {

Sources/PostgresNIO/New/Messages/RowDescription.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import NIOCore
99
/// Not putting `DataRow` in ``PSQLBackendMessage`` is our way to trick
1010
/// the Swift compiler.
1111
@usableFromInline
12-
struct RowDescription: PostgresBackendMessage.PayloadDecodable, Sendable, Equatable {
12+
struct RowDescription: PostgresBackendMessage.PayloadDecodable, Sendable, Hashable {
1313
/// Specifies the object ID of the parameter data type.
1414
@usableFromInline
1515
var columns: [Column]
1616

1717
@usableFromInline
18-
struct Column: Equatable, Sendable {
18+
struct Column: Hashable, Sendable {
1919
/// The field name.
2020
@usableFromInline
2121
var name: String

0 commit comments

Comments
 (0)