Skip to content

Commit 19e4aac

Browse files
ayushi2103“Ayushi
andauthored
ETCD Quality-of-life updates (#10)
1) Created docker subdirectory and renamed DOCKERFILE --> Dockerfile, allows running `docker build -t foo .` within docker subdirectory. 2) Installed swiftformat on repo. Co-authored-by: “Ayushi <“[email protected]”>
1 parent 29d6fa5 commit 19e4aac

File tree

11 files changed

+2081
-2080
lines changed

11 files changed

+2081
-2080
lines changed

Sources/ETCD/ETCDClient.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public final class EtcdClient: @unchecked Sendable {
3434
public init(host: String, port: Int, eventLoopGroup: EventLoopGroup) {
3535
self.host = host
3636
self.port = port
37-
self.group = eventLoopGroup
38-
self.connection = ClientConnection.insecure(group: self.group)
37+
group = eventLoopGroup
38+
connection = ClientConnection.insecure(group: group)
3939
.connect(host: host, port: port)
40-
self.client = Etcdserverpb_KVNIOClient(channel: self.connection)
41-
self.watchClient = Etcdserverpb_WatchAsyncClient(channel: self.connection)
40+
client = Etcdserverpb_KVNIOClient(channel: connection)
41+
watchClient = Etcdserverpb_WatchAsyncClient(channel: connection)
4242
}
4343

4444
/// Sets a value for a specified key in the ETCD server.
@@ -53,7 +53,7 @@ public final class EtcdClient: @unchecked Sendable {
5353
let call = client.put(putRequest)
5454
_ = try await call.response.get()
5555
}
56-
56+
5757
/// Sets a value for a specified key in the ETCD server.
5858
///
5959
/// - Parameters:
@@ -71,13 +71,13 @@ public final class EtcdClient: @unchecked Sendable {
7171
let protoRangeRequest = rangeRequest.toProto()
7272
let call = client.range(protoRangeRequest)
7373
let response = try await call.response.get()
74-
74+
7575
guard let kv = response.kvs.first else {
7676
return nil
7777
}
7878
return kv.value
7979
}
80-
80+
8181
/// Delete the value for a range from the ETCD server.
8282
///
8383
/// - Parameter deleteRangeRequest: The rangeRequest to delete.
@@ -86,7 +86,7 @@ public final class EtcdClient: @unchecked Sendable {
8686
let call = client.deleteRange(protoDeleteRangeRequest)
8787
_ = try await call.response.get()
8888
}
89-
89+
9090
/// Puts a value for a specified key in the ETCD server. If the key does not exist, a new key, value pair is created.
9191
///
9292
/// - Parameters:
@@ -95,7 +95,7 @@ public final class EtcdClient: @unchecked Sendable {
9595
public func put(_ key: some Sequence<UInt8>, value: some Sequence<UInt8>) async throws {
9696
try await set(key, value: value)
9797
}
98-
98+
9999
/// Puts a value for a specified key in the ETCD server. If the key does not exist, a new key, value pair is created.
100100
///
101101
/// - Parameters:
@@ -104,7 +104,7 @@ public final class EtcdClient: @unchecked Sendable {
104104
public func put(_ key: String, value: String) async throws {
105105
try await put(key.utf8, value: value.utf8)
106106
}
107-
107+
108108
/// Watches a specified key in the ETCD server.
109109
///
110110
/// - Parameters:
@@ -115,13 +115,13 @@ public final class EtcdClient: @unchecked Sendable {
115115
let watchAsyncSequence = WatchAsyncSequence(grpcAsyncSequence: watchClient.watch(request))
116116
return try await operation(watchAsyncSequence)
117117
}
118-
118+
119119
/// Watches a specified key in the ETCD server.
120120
///
121121
/// - Parameters:
122122
/// - key: The key for which the value is watched. Parameter is of type String.
123123
/// - operation: The operation to be run on the WatchAsyncSequence for the key.
124124
public func watch<Result>(_ key: String, _ operation: (WatchAsyncSequence) async throws -> Result) async throws -> Result {
125-
try await self.watch(key.utf8, operation)
125+
try await watch(key.utf8, operation)
126126
}
127127
}

Sources/ETCD/KeyValue.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public struct KeyValue {
2424
public var lease: Int
2525

2626
init(protoKeyValue: Etcdserverpb_KeyValue) {
27-
self.key = protoKeyValue.key
28-
self.createRevision = Int(protoKeyValue.createRevision)
29-
self.modRevision = Int(protoKeyValue.modRevision)
30-
self.version = Int(protoKeyValue.version)
31-
self.value = protoKeyValue.value
32-
self.lease = Int(protoKeyValue.lease)
27+
key = protoKeyValue.key
28+
createRevision = Int(protoKeyValue.createRevision)
29+
modRevision = Int(protoKeyValue.modRevision)
30+
version = Int(protoKeyValue.version)
31+
value = protoKeyValue.value
32+
lease = Int(protoKeyValue.lease)
3333
}
34-
34+
3535
/// Initialize a new KeyValue.
3636
///
3737
/// - Parameters:

Sources/ETCD/RangeRequest.swift

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public struct RangeRequest {
2020
case ascend = 1
2121
case descend = 2
2222
}
23-
23+
2424
public enum SortTarget: Int {
2525
case key = 0
2626
case version = 1
@@ -42,44 +42,43 @@ public struct RangeRequest {
4242
public var maxModRevision: Int = 0
4343
public var minCreateRevision: Int = 0
4444
public var maxCreateRevision: Int = 0
45-
46-
45+
4746
init(protoRangeRequest: Etcdserverpb_RangeRequest) {
48-
self.key = protoRangeRequest.key
49-
self.rangeEnd = protoRangeRequest.rangeEnd
50-
self.limit = Int(protoRangeRequest.limit)
51-
self.revision = Int(protoRangeRequest.revision)
52-
self.sortOrder = SortOrder(rawValue: protoRangeRequest.sortOrder.rawValue) ?? .none
53-
self.sortTarget = SortTarget(rawValue: protoRangeRequest.sortTarget.rawValue) ?? .key
54-
self.serializable = protoRangeRequest.serializable
55-
self.keysOnly = protoRangeRequest.keysOnly
56-
self.countOnly = protoRangeRequest.countOnly
57-
self.minModRevision = Int(protoRangeRequest.minModRevision)
58-
self.maxModRevision = Int(protoRangeRequest.maxModRevision)
59-
self.minCreateRevision = Int(protoRangeRequest.minCreateRevision)
60-
self.maxCreateRevision = Int(protoRangeRequest.maxCreateRevision)
47+
key = protoRangeRequest.key
48+
rangeEnd = protoRangeRequest.rangeEnd
49+
limit = Int(protoRangeRequest.limit)
50+
revision = Int(protoRangeRequest.revision)
51+
sortOrder = SortOrder(rawValue: protoRangeRequest.sortOrder.rawValue) ?? .none
52+
sortTarget = SortTarget(rawValue: protoRangeRequest.sortTarget.rawValue) ?? .key
53+
serializable = protoRangeRequest.serializable
54+
keysOnly = protoRangeRequest.keysOnly
55+
countOnly = protoRangeRequest.countOnly
56+
minModRevision = Int(protoRangeRequest.minModRevision)
57+
maxModRevision = Int(protoRangeRequest.maxModRevision)
58+
minCreateRevision = Int(protoRangeRequest.minCreateRevision)
59+
maxCreateRevision = Int(protoRangeRequest.maxCreateRevision)
6160
}
62-
61+
6362
public init(key: Data, rangeEnd: Data? = nil) {
6463
self.key = key
6564
self.rangeEnd = rangeEnd
6665
}
67-
66+
6867
func toProto() -> Etcdserverpb_RangeRequest {
6968
var protoRangeRequest = Etcdserverpb_RangeRequest()
70-
protoRangeRequest.key = self.key
71-
protoRangeRequest.rangeEnd = self.rangeEnd ?? Data()
72-
protoRangeRequest.limit = Int64(self.limit)
73-
protoRangeRequest.revision = Int64(self.revision)
74-
protoRangeRequest.sortOrder = Etcdserverpb_RangeRequest.SortOrder(rawValue: self.sortOrder.rawValue) ?? .none
75-
protoRangeRequest.sortTarget = Etcdserverpb_RangeRequest.SortTarget(rawValue: self.sortTarget.rawValue) ?? .key
76-
protoRangeRequest.serializable = self.serializable
77-
protoRangeRequest.keysOnly = self.keysOnly
78-
protoRangeRequest.countOnly = self.countOnly
79-
protoRangeRequest.minModRevision = Int64(self.minModRevision)
80-
protoRangeRequest.maxModRevision = Int64(self.maxModRevision)
81-
protoRangeRequest.minCreateRevision = Int64(self.minCreateRevision)
82-
protoRangeRequest.maxCreateRevision = Int64(self.maxCreateRevision)
69+
protoRangeRequest.key = key
70+
protoRangeRequest.rangeEnd = rangeEnd ?? Data()
71+
protoRangeRequest.limit = Int64(limit)
72+
protoRangeRequest.revision = Int64(revision)
73+
protoRangeRequest.sortOrder = Etcdserverpb_RangeRequest.SortOrder(rawValue: sortOrder.rawValue) ?? .none
74+
protoRangeRequest.sortTarget = Etcdserverpb_RangeRequest.SortTarget(rawValue: sortTarget.rawValue) ?? .key
75+
protoRangeRequest.serializable = serializable
76+
protoRangeRequest.keysOnly = keysOnly
77+
protoRangeRequest.countOnly = countOnly
78+
protoRangeRequest.minModRevision = Int64(minModRevision)
79+
protoRangeRequest.maxModRevision = Int64(maxModRevision)
80+
protoRangeRequest.minCreateRevision = Int64(minCreateRevision)
81+
protoRangeRequest.maxCreateRevision = Int64(maxCreateRevision)
8382
return protoRangeRequest
8483
}
8584
}

Sources/ETCD/WatchAsyncSequence.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public struct WatchAsyncSequence: AsyncSequence {
2424
}
2525

2626
public func makeAsyncIterator() -> AsyncIterator {
27-
.init(grpcIterator: self.grpcAsyncSequence.makeAsyncIterator())
27+
.init(grpcIterator: grpcAsyncSequence.makeAsyncIterator())
2828
}
2929

3030
public struct AsyncIterator: AsyncIteratorProtocol {
@@ -35,7 +35,7 @@ public struct WatchAsyncSequence: AsyncSequence {
3535
}
3636

3737
public mutating func next() async throws -> Element? {
38-
while let response = try await self.grpcIterator?.next() {
38+
while let response = try await grpcIterator?.next() {
3939
if response.created {
4040
// We receive this after setting up the watch and need to wait for the next
4141
// response that contains an event
@@ -45,14 +45,14 @@ public struct WatchAsyncSequence: AsyncSequence {
4545

4646
if response.canceled {
4747
// We got cancelled and have to return nil now
48-
self.grpcIterator = nil
48+
grpcIterator = nil
4949
return nil
5050
}
5151

5252
let events = response.events.map { WatchEvent(protoEvent: $0) }
5353
return events
5454
}
55-
return nil;
55+
return nil
5656
}
5757
}
5858
}

Sources/ETCD/WatchEvent.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public struct WatchEvent {
1919
public var previousKeyValue: KeyValue?
2020

2121
init(protoEvent: Etcdserverpb_Event) {
22-
self.keyValue = KeyValue(protoKeyValue: protoEvent.kv)
22+
keyValue = KeyValue(protoKeyValue: protoEvent.kv)
2323
if let protoPrevKV = protoEvent.hasPrevKv ? protoEvent.prevKv : nil {
24-
self.previousKeyValue = KeyValue(protoKeyValue: protoPrevKV)
24+
previousKeyValue = KeyValue(protoKeyValue: protoPrevKV)
2525
} else {
26-
self.previousKeyValue = nil
26+
previousKeyValue = nil
2727
}
2828
}
29-
29+
3030
/// Struct representing a watch event in etcd.
3131
///
3232
/// - Parameters:

0 commit comments

Comments
 (0)