Skip to content

Commit

Permalink
Merge pull request #10 from fdgogogo/main
Browse files Browse the repository at this point in the history
Fixed PubSubMessage having to contain UTF-8 String as payload
  • Loading branch information
michaelvanstraten authored Jan 7, 2024
2 parents f4cdb34 + 81c06a2 commit 58e8134
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
26 changes: 17 additions & 9 deletions Sources/SwiftyRedis/PubSub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public struct PubSubMessage: FromRedisValue {
The `pattern` parameter represents the pattern of the channel if it was a pattern subscription,
otherwise it's `nil`. The payload parameter represents the actual message payload.
*/
case message(pattern: String?, payload: String)
case message(pattern: String?, payload: Data)
/**
Indicates that we successfully subscribed to a channel.
The `number_of_channels` parameter represents the total number of channels we are currently subscribed to.
Expand Down Expand Up @@ -157,21 +157,29 @@ public struct PubSubMessage: FromRedisValue {
array = array.reversed()
let message_type = try String(array.popLast())

channel = try String(array.popLast())

switch message_type {
case "message", "pmessage", "smessage":
let payload = try String(array.popLast())

if let actual_payload = array.popLast() {
channel = payload
type = try .message(pattern: channel, payload: String(actual_payload))
case "message", "smessage":
channel = try String(array.popLast())
if case .BulkString(let data) = array.popLast() {
type = .message(pattern: nil, payload: data)
} else {
throw RedisError.make_invalid_type_error(detail: "Response type (\(value)) is not convertible to PubSubMessage")
}
case "pmessage":
let pattern = try String(array.popLast())
channel = try String(array.popLast())

if case .BulkString(let data) = array.popLast() {
type = .message(pattern: pattern, payload: data)
} else {
type = .message(pattern: nil, payload: payload)
throw RedisError.make_invalid_type_error(detail: "Response type (\(value)) is not convertible to PubSubMessage")
}
case "subscribe", "psubscribe", "ssubscribe":
channel = try String(array.popLast())
type = try .subscribe(number_of_channels: Int(array.popLast()))
case "unsubscribe", "punsubscribe", "sunsubscribe":
channel = try String(array.popLast())
type = try .unsubscribe(number_of_channels: Int(array.popLast()))
default:
throw RedisError.make_invalid_type_error(detail: "Unexprected PubSubMessage type (\"\(message_type)\")")
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftyRedisTests/PubSubConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class PubSubConnectionTests: XCTestCase {
switch result {
case let .success(message):
XCTAssertEqual(message.channel, "channel1")
XCTAssertEqual(message.type, .message(pattern: nil, payload: "Hello, World!"))
XCTAssertEqual(message.type, .message(pattern: nil, payload: Data("Hello, World!".utf8)))
case let .failure(error):
XCTFail("Received error: \(error)")
}
Expand All @@ -59,6 +59,6 @@ final class PubSubConnectionTests: XCTestCase {

// Verify the parsed PubSubMessage properties
XCTAssertEqual(pubSubMessage.channel, "channel1")
XCTAssertEqual(pubSubMessage.type, .message(pattern: nil, payload: "Hello, World!"))
XCTAssertEqual(pubSubMessage.type, .message(pattern: nil, payload: Data("Hello, World!".utf8)))
}
}

0 comments on commit 58e8134

Please sign in to comment.