Skip to content

Commit eef513d

Browse files
aim2120adam-fowler
andauthored
Fix LMOVE empty source array bug (#185)
* Fix LMOVE empty source array bug Signed-off-by: Annalise Mariottini <[email protected]> * Override LMOVE Response in code generator Signed-off-by: Adam Fowler <[email protected]> --------- Signed-off-by: Annalise Mariottini <[email protected]> Signed-off-by: Adam Fowler <[email protected]> Co-authored-by: Adam Fowler <[email protected]>
1 parent 4827bbf commit eef513d

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

Sources/Valkey/Commands/Custom/ListCustomCommands.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
// SPDX-License-Identifier: Apache-2.0
77
//
88

9+
import NIOCore
10+
11+
extension LMOVE {
12+
public typealias Response = ByteBuffer?
13+
}
14+
915
extension LMPOP {
1016
/// - Returns: One of the following
1117
/// * [Null]: If no element could be popped.

Sources/Valkey/Commands/ListCommands.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ public struct LMOVE: ValkeyCommand {
312312
}
313313
}
314314
}
315-
public typealias Response = ByteBuffer
316315

317316
@inlinable public static var name: String { "LMOVE" }
318317

@@ -794,7 +793,7 @@ extension ValkeyClientProtocol {
794793
/// - Response: [String]: The element being popped and pushed.
795794
@inlinable
796795
@discardableResult
797-
public func lmove(source: ValkeyKey, destination: ValkeyKey, wherefrom: LMOVE.Wherefrom, whereto: LMOVE.Whereto) async throws -> ByteBuffer {
796+
public func lmove(source: ValkeyKey, destination: ValkeyKey, wherefrom: LMOVE.Wherefrom, whereto: LMOVE.Whereto) async throws -> LMOVE.Response {
798797
try await execute(LMOVE(source: source, destination: destination, wherefrom: wherefrom, whereto: whereto))
799798
}
800799

Sources/_ValkeyCommandsBuilder/ValkeyCommandsRender.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ private let disableResponseCalculationCommands: Set<String> = [
1616
"GEOPOS",
1717
"GEOSEARCH",
1818
"ROLE",
19+
"LMOVE",
1920
"LMPOP",
2021
"SPOP",
2122
"SSCAN",

Tests/IntegrationTests/ValkeyTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,38 @@ struct GeneratedCommands {
361361
}
362362
}
363363

364+
@available(valkeySwift 1.0, *)
365+
@Test
366+
func testLMOVE() async throws {
367+
var logger = Logger(label: "Valkey")
368+
logger.logLevel = .trace
369+
try await withValkeyConnection(.hostname(valkeyHostname, port: 6379), logger: logger) { connection in
370+
try await withKey(connection: connection) { key in
371+
try await withKey(connection: connection) { key2 in
372+
let rtEmpty = try await connection.lmove(source: key, destination: key2, wherefrom: .right, whereto: .left)
373+
#expect(rtEmpty == nil)
374+
try await connection.lpush(key, elements: ["a"])
375+
try await connection.lpush(key, elements: ["b"])
376+
try await connection.lpush(key, elements: ["c"])
377+
try await connection.lpush(key, elements: ["d"])
378+
let list1Before = try await connection.lrange(key, start: 0, stop: -1).decode(as: [String].self)
379+
#expect(list1Before == ["d", "c", "b", "a"])
380+
let list2Before = try await connection.lrange(key2, start: 0, stop: -1).decode(as: [String].self)
381+
#expect(list2Before == [])
382+
for expectedValue in ["a", "b", "c", "d"] {
383+
var rt = try #require(try await connection.lmove(source: key, destination: key2, wherefrom: .right, whereto: .left))
384+
let value = rt.readString(length: 1)
385+
#expect(value == expectedValue)
386+
}
387+
let list1After = try await connection.lrange(key, start: 0, stop: -1).decode(as: [String].self)
388+
#expect(list1After == [])
389+
let list2After = try await connection.lrange(key2, start: 0, stop: -1).decode(as: [String].self)
390+
#expect(list2After == ["d", "c", "b", "a"])
391+
}
392+
}
393+
}
394+
}
395+
364396
@available(valkeySwift 1.0, *)
365397
@Test("Test command error is thrown")
366398
func testCommandError() async throws {

0 commit comments

Comments
 (0)