diff --git a/Sources/RediStack/Commands/SortedSetCommands.swift b/Sources/RediStack/Commands/SortedSetCommands.swift index 81766393..f638d68a 100644 --- a/Sources/RediStack/Commands/SortedSetCommands.swift +++ b/Sources/RediStack/Commands/SortedSetCommands.swift @@ -560,18 +560,21 @@ extension RedisClient { /// - Parameters: /// - key: The key identifying the sorted set in Redis. /// - count: The max number of elements to pop from the set. + /// - scoreIsFirst: Indicates whether the score is in the first position. /// - Returns: A list of elements popped from the sorted set with their associated score. - public func zpopmin(from key: RedisKey, max count: Int) -> EventLoopFuture<[(RESPValue, Double)]> { - _zpop(command: "ZPOPMIN", count, key) + public func zpopmin(from key: RedisKey, max count: Int, scoreIsFirst: Bool = false) -> EventLoopFuture<[(RESPValue, Double)]> { + _zpop(command: "ZPOPMIN", count, key, scoreIsFirst: scoreIsFirst) } /// Removes the element from a sorted set with the lowest score. /// /// See [https://redis.io/commands/zpopmin](https://redis.io/commands/zpopmin) - /// - Parameter key: The key identifying the sorted set in Redis. + /// - Parameters: + /// - key: The key identifying the sorted set in Redis. + /// - scoreIsFirst: Indicates whether the score is in the first position. /// - Returns: The element and its associated score that was popped from the sorted set, or `nil` if set was empty. - public func zpopmin(from key: RedisKey) -> EventLoopFuture<(RESPValue, Double)?> { - _zpop(command: "ZPOPMIN", nil, key) + public func zpopmin(from key: RedisKey, scoreIsFirst: Bool = false) -> EventLoopFuture<(RESPValue, Double)?> { + _zpop(command: "ZPOPMIN", nil, key, scoreIsFirst: scoreIsFirst) .map { $0.count > 0 ? $0[0] : nil } } @@ -581,25 +584,29 @@ extension RedisClient { /// - Parameters: /// - key: The key identifying the sorted set in Redis. /// - count: The max number of elements to pop from the set. + /// - scoreIsFirst: Indicates whether the score is in the first position. /// - Returns: A list of elements popped from the sorted set with their associated score. - public func zpopmax(from key: RedisKey, max count: Int) -> EventLoopFuture<[(RESPValue, Double)]> { - _zpop(command: "ZPOPMAX", count, key) + public func zpopmax(from key: RedisKey, max count: Int, scoreIsFirst: Bool = false) -> EventLoopFuture<[(RESPValue, Double)]> { + _zpop(command: "ZPOPMAX", count, key, scoreIsFirst: scoreIsFirst) } /// Removes the element from a sorted set with the highest score. /// /// See [https://redis.io/commands/zpopmax](https://redis.io/commands/zpopmax) - /// - Parameter key: The key identifying the sorted set in Redis. + /// - Parameters: + /// - key: The key identifying the sorted set in Redis. + /// - scoreIsFirst: Indicates whether the score is in the first position. /// - Returns: The element and its associated score that was popped from the sorted set, or `nil` if set was empty. - public func zpopmax(from key: RedisKey) -> EventLoopFuture<(RESPValue, Double)?> { - _zpop(command: "ZPOPMAX", nil, key) + public func zpopmax(from key: RedisKey, scoreIsFirst: Bool = false) -> EventLoopFuture<(RESPValue, Double)?> { + _zpop(command: "ZPOPMAX", nil, key, scoreIsFirst: scoreIsFirst) .map { $0.count > 0 ? $0[0] : nil } } func _zpop( command: String, _ count: Int?, - _ key: RedisKey + _ key: RedisKey, + scoreIsFirst: Bool ) -> EventLoopFuture<[(RESPValue, Double)]> { var args: [RESPValue] = [.init(from: key)] @@ -611,7 +618,7 @@ extension RedisClient { return send(command: command, with: args) .tryConverting(to: [RESPValue].self) - .flatMapThrowing { try Self._mapSortedSetResponse($0, scoreIsFirst: true) } + .flatMapThrowing { try Self._mapSortedSetResponse($0, scoreIsFirst: scoreIsFirst) } } } diff --git a/Tests/RediStackIntegrationTests/Commands/SortedSetCommandsTests.swift b/Tests/RediStackIntegrationTests/Commands/SortedSetCommandsTests.swift index ee221fd0..af75d49f 100644 --- a/Tests/RediStackIntegrationTests/Commands/SortedSetCommandsTests.swift +++ b/Tests/RediStackIntegrationTests/Commands/SortedSetCommandsTests.swift @@ -555,6 +555,10 @@ extension SortedSetCommandsTests { XCTAssertEqual(elements.count, 5) XCTAssertEqual(elements, elements.sorted(by: <)) + + // test when score is not first + let value = try self.connection.zpopmin(from: #function, scoreIsFirst: false).wait() + XCTAssertEqual(value?.1, 1) } func test_zrevrange_realworld() throws {