Skip to content

Commit 1ef0f6e

Browse files
authored
Add subscript to XREADMessage for fields (#113)
* Add subscript to XREADMessage for fields * Comment with suggestion for alternative
1 parent 06a06ed commit 1ef0f6e

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

Sources/Valkey/Commands/Custom/StreamCustomCommands.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,41 @@ public struct XREADMessage: RESPTokenDecodable, Sendable {
2828
throw RESPParsingError(code: .unexpectedType, buffer: token.base)
2929
}
3030
}
31+
32+
/// Accesses the value associated with the specified field key in the stream.
33+
///
34+
/// The field collection is an array so subscript access is a O(n) where n is
35+
/// the number of fields.
36+
///
37+
/// Alternatively the user can create a Dictionary if there are a large number of
38+
/// fields and many are accessed
39+
/// ```
40+
/// let fields = Dictionary(uniqueKeysWithValues: message.fields)
41+
/// let field = field["fieldName"]
42+
/// ```
43+
///
44+
/// - Parameter key: The field key to look up.
45+
/// - Returns: The `RESPToken` value associated with the given key, or `nil` if the key does not exist.
46+
public subscript(field key: String) -> RESPToken? {
47+
fields.first(where: { $0.key == key })?.value
48+
}
49+
50+
/// Accesses the values associated with the specified field key as an array of `RESPToken`.
51+
///
52+
/// The field collection is an array so subscript access is a O(n) where n is
53+
/// the number of fields.
54+
///
55+
/// - Parameter key: The field key to retrieve values for.
56+
/// - Returns: An array of `RESPToken` values associated with the given field key.
57+
public subscript(fields key: String) -> [RESPToken] {
58+
fields.compactMap {
59+
if $0.key == key {
60+
$0.value
61+
} else {
62+
nil
63+
}
64+
}
65+
}
3166
}
3267

3368
@_documentation(visibility: internal)

Tests/ValkeyTests/CommandTests.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,13 @@ struct CommandTests {
381381
let stream2 = try #require(result.streams.first { $0.key == "key2" })
382382
#expect(stream1.key == "key1")
383383
#expect(stream1.messages[0].id == "event1")
384-
#expect(stream1.messages[0].fields[0].key == "field1")
385-
#expect(try stream1.messages[0].fields[0].value.decode(as: String.self) == "value1")
384+
#expect(try stream1.messages[0][field: "field1"]?.decode(as: String.self) == "value1")
386385
#expect(stream2.key == "key2")
387386
#expect(stream2.messages[0].id == "event2")
388-
#expect(stream2.messages[0].fields[0].key == "field2")
389-
#expect(try stream2.messages[0].fields[0].value.decode(as: String.self) == "value2")
387+
#expect(try stream2.messages[0][field: "field2"]?.decode(as: String.self) == "value2")
390388
#expect(stream2.messages[1].id == "event3")
391-
#expect(stream2.messages[1].fields[0].key == "field3")
392-
#expect(try stream2.messages[1].fields[0].value.decode(as: String.self) == "value3")
393-
#expect(stream2.messages[1].fields[1].key == "field4")
394-
#expect(try stream2.messages[1].fields[1].value.decode(as: String.self) == "value4")
395-
389+
#expect(try stream2.messages[1][field: "field3"]?.decode(as: String.self) == "value3")
390+
#expect(try stream2.messages[1][field: "field4"]?.decode(as: String.self) == "value4")
396391
}
397392

398393
@Test

0 commit comments

Comments
 (0)