Skip to content

Commit 8ce1dd8

Browse files
committed
Merge branch 'generic-get' into 'master'
Add RedisClient.get generic overload See merge request Mordil/swift-redi-stack!86
2 parents 5c39c83 + 9e5179f commit 8ce1dd8

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Sources/RediStack/Commands/StringCommands.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,30 @@ import NIO
1818

1919
extension RedisClient {
2020
/// Get the value of a key.
21-
/// - Note: This operation only works with string values.
22-
/// The `EventLoopFuture` will fail with a `RedisError` if the value is not a string, such as a Set.
2321
///
2422
/// [https://redis.io/commands/get](https://redis.io/commands/get)
2523
/// - Parameter key: The key to fetch the value from.
2624
/// - Returns: The string value stored at the key provided, otherwise `nil` if the key does not exist.
2725
@inlinable
2826
public func get(_ key: String) -> EventLoopFuture<String?> {
27+
return self.get(key, as: String.self)
28+
}
29+
30+
/// Get the value of a key, converting it to the desired type.
31+
///
32+
/// [https://redis.io/commands/get](https://redis.io/commands/get)
33+
/// - Parameters:
34+
/// - key: The key to fetch the value from.
35+
/// - type: The desired type to convert the stored data to.
36+
/// - Returns: The converted value stored at the key provided, otherwise `nil` if the key does not exist or fails the conversion.
37+
@inlinable
38+
public func get<StoredType: RESPValueConvertible>(
39+
_ key: String,
40+
as type: StoredType.Type
41+
) -> EventLoopFuture<StoredType?> {
2942
let args = [RESPValue(bulk: key)]
3043
return send(command: "GET", with: args)
31-
.map { return $0.string }
44+
.map { return StoredType(fromRESP: $0) }
3245
}
3346

3447
/// Gets the values of all specified keys, using `.null` to represent non-existant values.

Tests/RediStackIntegrationTests/Commands/StringCommandsTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ final class StringCommandsTests: RediStackIntegrationTestCase {
2121

2222
func test_get() throws {
2323
try connection.set(#function, to: "value").wait()
24-
let result = try connection.get(#function).wait()
25-
XCTAssertEqual(result, "value")
24+
let r1 = try connection.get(#function).wait()
25+
XCTAssertEqual(r1, "value")
26+
27+
try connection.set(#function, to: 30).wait()
28+
let r2 = try connection.get(#function, as: Int.self).wait()
29+
XCTAssertEqual(r2, 30)
2630
}
2731

2832
func test_mget() throws {

0 commit comments

Comments
 (0)