Skip to content

Commit 550bde6

Browse files
Make Error types Hashable + Equatable (#69)
* Make Error types Hashable + Equatable Modifications: * `KafkaError`: make `Equatable` * `KafkaAcknowledgedMessageError`: make `Equatable` + `Hashable` * Review Franz Modifications: * remove duplicate `Equatable` conformances, `Hashable` implies `Equatable` * only take the backing `code` into account for `Hashable` and `Equatable` conformance of the `Kafka*Error` types * * remove redundant `Equatable` annotations
1 parent 1173b07 commit 550bde6

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

Sources/SwiftKafka/Configuration/KafkaConsumerConfiguration.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import Crdkafka
1616
import struct Foundation.UUID
1717

18-
public struct KafkaConsumerConfiguration: Hashable, Equatable {
18+
public struct KafkaConsumerConfiguration: Hashable {
1919
// MARK: - SwiftKafka-specific Config properties
2020

2121
/// The backpressure strategy to be used for message consumption.
@@ -474,8 +474,8 @@ public struct KafkaConsumerConfiguration: Hashable, Equatable {
474474

475475
extension KafkaSharedConfiguration {
476476
/// A struct representing different back pressure strategies for consuming messages in ``KafkaConsumer``.
477-
public struct BackPressureStrategy: Hashable, Equatable {
478-
enum _BackPressureStrategy: Hashable, Equatable {
477+
public struct BackPressureStrategy: Hashable {
478+
enum _BackPressureStrategy: Hashable {
479479
case watermark(low: Int, high: Int)
480480
}
481481

@@ -498,8 +498,8 @@ extension KafkaSharedConfiguration {
498498
}
499499

500500
/// A struct representing the different Kafka message consumption strategies.
501-
public struct ConsumptionStrategy: Hashable, Equatable {
502-
enum _ConsumptionStrategy: Hashable, Equatable {
501+
public struct ConsumptionStrategy: Hashable {
502+
enum _ConsumptionStrategy: Hashable {
503503
case partition(topic: String, partition: KafkaPartition, offset: Int)
504504
case group(groupID: String, topics: [String])
505505
}
@@ -535,7 +535,7 @@ extension KafkaSharedConfiguration {
535535
}
536536

537537
/// Available actions to take when there is no initial offset in offset store / offset is out of range.
538-
public struct AutoOffsetReset: Hashable, Equatable, CustomStringConvertible {
538+
public struct AutoOffsetReset: Hashable, CustomStringConvertible {
539539
public let description: String
540540

541541
/// Automatically reset the offset to the smallest offset.

Sources/SwiftKafka/Configuration/KafkaProducerConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
public struct KafkaProducerConfiguration: Hashable, Equatable {
15+
public struct KafkaProducerConfiguration: Hashable {
1616
var dictionary: [String: String] = [:]
1717

1818
// MARK: - Producer-specific Config Properties

Sources/SwiftKafka/Configuration/KafkaSharedConfiguration.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/// Collection of `enum` types used in the configuration structs this library provides.
1616
public enum KafkaSharedConfiguration {
1717
/// Available debug contexts to enable.
18-
public struct DebugOption: Hashable, Equatable, CustomStringConvertible {
18+
public struct DebugOption: Hashable, CustomStringConvertible {
1919
public let description: String
2020

2121
public static let generic = DebugOption(description: "generic")
@@ -38,7 +38,7 @@ public enum KafkaSharedConfiguration {
3838
}
3939

4040
/// Available IP address families.
41-
public struct IPAddressFamily: Hashable, Equatable, CustomStringConvertible {
41+
public struct IPAddressFamily: Hashable, CustomStringConvertible {
4242
public let description: String
4343

4444
/// Use any IP address family.
@@ -50,7 +50,7 @@ public enum KafkaSharedConfiguration {
5050
}
5151

5252
/// Protocol used to communicate with brokers.
53-
public struct SecurityProtocol: Hashable, Equatable, CustomStringConvertible {
53+
public struct SecurityProtocol: Hashable, CustomStringConvertible {
5454
public let description: String
5555

5656
/// Send messages as plaintext (no security protocol used).
@@ -64,7 +64,7 @@ public enum KafkaSharedConfiguration {
6464
}
6565

6666
/// Available SASL mechanisms that can be used for authentication.
67-
public struct SASLMechanism: Hashable, Equatable, CustomStringConvertible {
67+
public struct SASLMechanism: Hashable, CustomStringConvertible {
6868
public let description: String
6969

7070
/// Use the GSSAPI mechanism.

Sources/SwiftKafka/Configuration/KafkaTopicConfiguration.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
/// Used to configure new topics created by the ``KafkaProducer``.
16-
public struct KafkaTopicConfiguration: Hashable, Equatable {
16+
public struct KafkaTopicConfiguration: Hashable {
1717
var dictionary: [String: String] = [:]
1818

1919
/// This field indicates the number of acknowledgements the leader broker must receive from ISR brokers before responding to the request: 0=Broker does not send any response/ack to client, -1 or all=Broker will block until message is committed by all in sync replicas (ISRs). If there are less than min.insync.replicas (broker configuration) in the ISR set the produce request will fail.
@@ -89,7 +89,7 @@ public struct KafkaTopicConfiguration: Hashable, Equatable {
8989

9090
extension KafkaSharedConfiguration {
9191
/// Partitioner. Computes the partition that a message is stored in.
92-
public struct Partitioner: Hashable, Equatable, CustomStringConvertible {
92+
public struct Partitioner: Hashable, CustomStringConvertible {
9393
public let description: String
9494

9595
/// Random distribution.
@@ -109,7 +109,7 @@ extension KafkaSharedConfiguration {
109109
}
110110

111111
/// Process to compress and decompress data.
112-
public struct CompressionCodec: Hashable, Equatable, CustomStringConvertible {
112+
public struct CompressionCodec: Hashable, CustomStringConvertible {
113113
public let description: String
114114

115115
/// No compression.

Sources/SwiftKafka/KafkaAcknowledgedMessageError.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import Crdkafka
1616

1717
/// Error caused by the Kafka cluster when trying to process a message produced by ``KafkaProducer``.
18+
///
19+
/// - Note: `Hashable` conformance only considers the underlying ``KafkaAcknowledgedMessageError/error``'s
20+
/// ``KafkaError/code``.
1821
public struct KafkaAcknowledgedMessageError: Error, CustomStringConvertible {
1922
/// Identifier of the message that caused the error.
2023
public var messageID: UInt
@@ -62,3 +65,15 @@ public struct KafkaAcknowledgedMessageError: Error, CustomStringConvertible {
6265
)
6366
}
6467
}
68+
69+
// MARK: - KafkaAcknowledgedMessageError + Hashable
70+
71+
extension KafkaAcknowledgedMessageError: Hashable {
72+
public static func == (lhs: KafkaAcknowledgedMessageError, rhs: KafkaAcknowledgedMessageError) -> Bool {
73+
return lhs.error == rhs.error
74+
}
75+
76+
public func hash(into hasher: inout Hasher) {
77+
hasher.combine(self.error)
78+
}
79+
}

Sources/SwiftKafka/KafkaError.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
import Crdkafka
1616

17-
public struct KafkaError: Error, Hashable, CustomStringConvertible {
17+
/// An error that can occur on `Kafka` operations
18+
///
19+
/// - Note: `Hashable` conformance only considers the ``KafkaError/code``.
20+
public struct KafkaError: Error, CustomStringConvertible {
1821
private var backing: Backing
1922

2023
/// Represents the kind of error that was encountered.
@@ -192,6 +195,8 @@ extension KafkaError {
192195
}
193196
}
194197

198+
// MARK: - KafkaError + Backing
199+
195200
extension KafkaError {
196201
final class Backing: Hashable {
197202
var code: KafkaError.ErrorCode
@@ -228,3 +233,15 @@ extension KafkaError {
228233
}
229234
}
230235
}
236+
237+
// MARK: - KafkaError + Hashable
238+
239+
extension KafkaError: Hashable {
240+
public static func == (lhs: KafkaError, rhs: KafkaError) -> Bool {
241+
return lhs.backing == rhs.backing
242+
}
243+
244+
public func hash(into hasher: inout Hasher) {
245+
hasher.combine(self.backing)
246+
}
247+
}

0 commit comments

Comments
 (0)