|
| 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 | +/// Sorted set entry |
| 11 | +@_documentation(visibility: internal) |
| 12 | +public struct HashEntry: RESPTokenDecodable, Sendable { |
| 13 | + public let field: ByteBuffer |
| 14 | + public let value: ByteBuffer |
| 15 | + |
| 16 | + init(field: ByteBuffer, value: ByteBuffer) { |
| 17 | + self.field = field |
| 18 | + self.value = value |
| 19 | + } |
| 20 | + |
| 21 | + public init(fromRESP token: RESPToken) throws { |
| 22 | + switch token.value { |
| 23 | + case .array(let array): |
| 24 | + (self.field, self.value) = try array.decodeElements() |
| 25 | + default: |
| 26 | + throw RESPDecodeError.tokenMismatch(expected: [.array], token: token) |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +extension HSCAN { |
| 32 | + public struct Response: RESPTokenDecodable, Sendable { |
| 33 | + public struct Members: RESPTokenDecodable, Sendable { |
| 34 | + /// List of members and possibly scores. |
| 35 | + public let elements: RESPToken.Array |
| 36 | + |
| 37 | + public init(fromRESP token: RESPToken) throws { |
| 38 | + self.elements = try token.decode(as: RESPToken.Array.self) |
| 39 | + } |
| 40 | + |
| 41 | + /// if HSCAN was called with the `NOVALUES` parameter use this |
| 42 | + /// function to get an array of fields |
| 43 | + public func withoutValues() throws -> [ByteBuffer] { |
| 44 | + try self.elements.decode(as: [ByteBuffer].self) |
| 45 | + } |
| 46 | + |
| 47 | + /// if HSCAN was called without the `NOVALUES` parameter use this |
| 48 | + /// function to get an array of fields and values |
| 49 | + public func withValues() throws -> [HashEntry] { |
| 50 | + var array: [HashEntry] = [] |
| 51 | + for respElement in try self.elements.asMap() { |
| 52 | + let field = try ByteBuffer(fromRESP: respElement.key) |
| 53 | + let value = try ByteBuffer(fromRESP: respElement.value) |
| 54 | + array.append(.init(field: field, value: value)) |
| 55 | + } |
| 56 | + return array |
| 57 | + } |
| 58 | + } |
| 59 | + /// Cursor to use in next call to HSCAN |
| 60 | + public let cursor: Int |
| 61 | + /// Sorted set members |
| 62 | + public let members: Members |
| 63 | + |
| 64 | + public init(fromRESP token: RESPToken) throws { |
| 65 | + (self.cursor, self.members) = try token.decodeArrayElements() |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments