File tree Expand file tree Collapse file tree 2 files changed +39
-9
lines changed
Sources/Valkey/Commands/Custom Expand file tree Collapse file tree 2 files changed +39
-9
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,41 @@ public struct XREADMessage: RESPTokenDecodable, Sendable {
28
28
throw RESPParsingError ( code: . unexpectedType, buffer: token. base)
29
29
}
30
30
}
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
+ }
31
66
}
32
67
33
68
@_documentation ( visibility: internal)
Original file line number Diff line number Diff line change @@ -381,18 +381,13 @@ struct CommandTests {
381
381
let stream2 = try #require( result. streams. first { $0. key == " key2 " } )
382
382
#expect( stream1. key == " key1 " )
383
383
#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 " )
386
385
#expect( stream2. key == " key2 " )
387
386
#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 " )
390
388
#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 " )
396
391
}
397
392
398
393
@Test
You can’t perform that action at this time.
0 commit comments