Skip to content

Commit

Permalink
fix(major): Change KV API to indicate value for not existing key. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ser-0xff authored Jul 17, 2024
1 parent e912d47 commit f5f2330
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
15 changes: 10 additions & 5 deletions Sources/ConsulServiceDiscovery/Consul.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,23 @@ public final class Consul: Sendable {
/// - Parameters
/// - key: specifies the path of the key
/// - datacenter: Specifies the datacenter to query. This will default to the datacenter of the agent being queried.
/// - Returns: EventLoopFuture<Value> to deliver result
/// - Returns: EventLoopFuture<Value?> to deliver result, future will sucess with 'nil' if key does not exist
/// [apidoc]: https://developer.hashicorp.com/consul/api-docs/kv#read-key
///
public func valueForKey(_ key: String, inDatacenter datacenter: String? = nil) -> EventLoopFuture<Value> {
public func valueForKey(_ key: String, inDatacenter datacenter: String? = nil) -> EventLoopFuture<Value?> {
struct ResponseHandler: ConsulResponseHandler {
private let promise: EventLoopPromise<Value>
private let promise: EventLoopPromise<Value?>

init(_ promise: EventLoopPromise<Value>) {
init(_ promise: EventLoopPromise<Value?>) {
self.promise = promise
}

func processResponse(_ buffer: ByteBuffer, withIndex: Int?) {
if buffer.readableBytes == 0 {
promise.succeed(nil)
return
}

guard let bytes = buffer.getBytes(at: buffer.readerIndex, length: buffer.readableBytes) else {
fatalError("Internal error: bytes unexpectedly nil")
}
Expand Down Expand Up @@ -369,7 +374,7 @@ public final class Consul: Sendable {
components.queryItems = queryItems
}

let promise = impl.makePromise(of: Value.self)
let promise = impl.makePromise(of: Value?.self)
if let requestURI = components.string {
impl.request(method: .GET, uri: requestURI, body: nil, handler: ResponseHandler(promise))
} else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/ConsulServiceDiscovery/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public struct NodeService: Hashable, Decodable, Sendable {

public struct Value: Hashable, Decodable, Sendable {
public let flags: Int?
public let key: String?
public let key: String
public let value: String?
public let createIndex: Int?
public let modifyIndex: Int?
Expand Down
16 changes: 14 additions & 2 deletions Tests/ConsulServiceDiscoveryTests/ConsulTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@testable import ConsulServiceDiscovery
import NIOPosix
import class NIOCore.EventLoopFuture
import XCTest

final class ConsulTests: XCTestCase {
Expand Down Expand Up @@ -94,9 +95,9 @@ final class ConsulTests: XCTestCase {
XCTAssertTrue(keys2.contains(where: { $0 == testKey }))

let future3 = consul.kv.valueForKey(testKey)
let value = try future3.wait()
let value = try XCTUnwrap(future3.wait(), "value is unexpectedly empty")

let valueValue = try XCTUnwrap(value.value, "value is unexpectedly empty")
let valueValue = try XCTUnwrap(value.value, "value.value is unexpectedly empty")
XCTAssertEqual(valueValue, testValue)

let future4 = consul.kv.removeValue(forKey: testKey)
Expand All @@ -108,4 +109,15 @@ final class ConsulTests: XCTestCase {

try consul.syncShutdown()
}

func testKVDoesNotExist() throws {
let consul = Consul()

let testKey = "test-key-does-not-exist"
let future1 = consul.kv.valueForKey(testKey)
let value = try future1.wait()
XCTAssertEqual(value, nil)

try consul.syncShutdown()
}
}

0 comments on commit f5f2330

Please sign in to comment.