Skip to content

Commit 727878a

Browse files
ayushi2103“Ayushi
andauthored
ETCD Client Put Functionality (#6)
* Add put function for ETCDClient * Update test --------- Co-authored-by: “Ayushi <“[email protected]”>
1 parent 72ac4a7 commit 727878a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Sources/ETCD/ETCDClient.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,23 @@ public final class EtcdClient: @unchecked Sendable {
102102
public func delete(_ key: String) async throws {
103103
return try await delete(key.utf8)
104104
}
105+
106+
/// Puts a value for a specified key in the ETCD server. If the key does not exist, a new key, value pair is created.
107+
///
108+
/// - Parameters:
109+
/// - key: The key for which the value is to be put. Parameter is of type Sequence<UInt8>.
110+
/// - value: The ETCD value to put for the key. Parameter is of type Sequence<UInt8>.
111+
public func put(_ key: some Sequence<UInt8>, value: some Sequence<UInt8>) async throws {
112+
try await set(key, value: value)
113+
}
114+
115+
/// Puts a value for a specified key in the ETCD server. If the key does not exist, a new key, value pair is created.
116+
///
117+
/// - Parameters:
118+
/// - key: The key for which the value is to be put. Parameter is of type String.
119+
/// - value: The ETCD value to put for the key. Parameter is of type String.
120+
public func put(_ key: String, value: String) async throws {
121+
try await put(key.utf8, value: value.utf8)
122+
}
123+
105124
}

Tests/ETCDTests/ETCDTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,21 @@ final class EtcdClientTests: XCTestCase {
6262
fetchedValue = try await etcdClient.get(key)
6363
XCTAssertNil(fetchedValue)
6464
}
65+
66+
func testUpdateExistingKey() async throws {
67+
let key = "testKey"
68+
let value = "testValue"
69+
try await etcdClient.set(key, value: value)
70+
71+
let fetchedValue = try await etcdClient.get(key)
72+
XCTAssertNotNil(fetchedValue)
73+
XCTAssertEqual(String(data: fetchedValue!, encoding: .utf8), value)
74+
75+
let updatedValue = "updatedValue"
76+
try await etcdClient.put(key, value: updatedValue)
77+
78+
let fetchedUpdatedValue = try await etcdClient.get(key)
79+
XCTAssertNotNil(fetchedUpdatedValue)
80+
XCTAssertEqual(String(data: fetchedUpdatedValue!, encoding: .utf8), updatedValue)
81+
}
6582
}

0 commit comments

Comments
 (0)