Skip to content

Commit 785b46d

Browse files
committed
Add response types for KEYS and SCAN commands
Signed-off-by: Natan Rolnik <[email protected]>
1 parent 9b1ec4a commit 785b46d

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// This source file is part of the valkey-swift project
3+
// Copyright (c) 2025 the valkey-swift project authors
4+
//
5+
// See LICENSE.txt for license information
6+
// SPDX-License-Identifier: Apache-2.0
7+
//
8+
import NIOCore
9+
10+
extension SCAN {
11+
public struct Response: RESPTokenDecodable, Sendable {
12+
public let cursor: Int
13+
public let keys: [ValkeyKey]
14+
15+
public init(fromRESP token: RESPToken) throws {
16+
let (cursor, keys) = try token.decodeArrayElements(as: (Int, [ValkeyKey]).self)
17+
self.cursor = cursor
18+
self.keys = keys
19+
}
20+
}
21+
}
22+
23+
extension KEYS {
24+
public typealias Response = [ValkeyKey]
25+
}

Sources/Valkey/Commands/GenericCommands.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,6 @@ public struct EXPIRETIME: ValkeyCommand {
324324
/// Returns all key names that match a pattern.
325325
@_documentation(visibility: internal)
326326
public struct KEYS: ValkeyCommand {
327-
public typealias Response = RESPToken.Array
328-
329327
@inlinable public static var name: String { "KEYS" }
330328

331329
public var pattern: String
@@ -740,8 +738,6 @@ public struct RESTORE<SerializedValue: RESPStringRenderable>: ValkeyCommand {
740738
/// Iterates over the key names in the database.
741739
@_documentation(visibility: internal)
742740
public struct SCAN: ValkeyCommand {
743-
public typealias Response = RESPToken.Array
744-
745741
@inlinable public static var name: String { "SCAN" }
746742

747743
public var cursor: Int
@@ -1168,7 +1164,7 @@ extension ValkeyClientProtocol {
11681164
/// - Complexity: O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length.
11691165
/// - Response: [Array]: List of keys matching pattern.
11701166
@inlinable
1171-
public func keys(pattern: String) async throws -> RESPToken.Array {
1167+
public func keys(pattern: String) async throws -> KEYS.Response {
11721168
try await execute(KEYS(pattern: pattern))
11731169
}
11741170

@@ -1433,7 +1429,7 @@ extension ValkeyClientProtocol {
14331429
/// - Complexity: O(1) for every call. O(N) for a complete iteration, including enough command calls for the cursor to return back to 0. N is the number of elements inside the collection.
14341430
/// - Response: [Array]: Cursor and scan response in array form.
14351431
@inlinable
1436-
public func scan(cursor: Int, pattern: String? = nil, count: Int? = nil, type: String? = nil) async throws -> RESPToken.Array {
1432+
public func scan(cursor: Int, pattern: String? = nil, count: Int? = nil, type: String? = nil) async throws -> SCAN.Response {
14371433
try await execute(SCAN(cursor: cursor, pattern: pattern, count: count, type: type))
14381434
}
14391435

Sources/Valkey/Documentation.docc/getting-started.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ You can use the `add-dependency` command:
1212

1313
```bash
1414
swift package add-dependency \
15-
https://github.com/valkey-io/valkey-swift --from: 0.1.0
15+
https://github.com/valkey-io/valkey-swift --from: 0.3.0
1616
```
1717

1818
or edit Package.swift directly:
1919
```swift
2020
dependencies: [
2121
.package(url: "https://github.com/valkey-io/valkey-swift",
22-
from: "0.1.0"),
22+
from: "0.3.0"),
2323
]
2424
```
2525

@@ -58,7 +58,7 @@ You can either run them using a Task group, for example:
5858

5959
```swift
6060
let valkeyClient = ValkeyClient(.hostname("localhost", port: 6379), logger: logger)
61-
try await withThrowingTaskgroup(of: Void.self) { group in
61+
try await withThrowingTaskGroup(of: Void.self) { group in
6262
group.addTask {
6363
// run connection pool in the background
6464
await valkeyClient.run()

Sources/_ValkeyCommandsBuilder/ValkeyCommandsRender.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ private let disableResponseCalculationCommands: Set<String> = [
2121
"GEODIST",
2222
"GEOPOS",
2323
"GEOSEARCH",
24-
"ROLE",
24+
"KEYS",
2525
"LMOVE",
2626
"LMPOP",
27+
"ROLE",
28+
"SCAN",
2729
"SSCAN",
2830
"XAUTOCLAIM",
2931
"XCLAIM",
@@ -36,6 +38,7 @@ private let disableResponseCalculationCommands: Set<String> = [
3638
"ZPOPMAX",
3739
"ZPOPMIN",
3840
]
41+
3942
/// List of subscribe commands, which have their own implementation in code
4043
let subscribeFunctions: Set<String> = [
4144
"SUBSCRIBE",
@@ -45,6 +48,7 @@ let subscribeFunctions: Set<String> = [
4548
"PUNSUBSCRIBE",
4649
"SUNSUBSCRIBE",
4750
]
51+
4852
/// List of commands that should only be available from ValkeyConnection
4953
let connectionOnlyFunctionNames: Set<String> = [
5054
"MULTI",

0 commit comments

Comments
 (0)