Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed PubSubMessage having to contain UTF-8 String as payload #10

Merged
merged 3 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 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,30 @@ 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))
Comment on lines -163 to -168

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was kind of trying to make this as covering as possible, so "message", "pmessage", "smessage" all in one case, but I could admit that this is somewhat hard to read/understand.

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())

let actual_payload_data: Data
fdgogogo marked this conversation as resolved.
Show resolved Hide resolved
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)))
}
}