Skip to content

Commit

Permalink
Refactor 'pmessage' parsing to correctly handle patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
fdgogogo committed Dec 27, 2023
1 parent 6849d4c commit 3c5773f
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions Sources/SwiftyRedis/PubSub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,34 +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 = array.popLast()

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

case "message", "smessage":
channel = try String(array.popLast())
if case .BulkString(let data) = array.popLast() {
type = .message(pattern: nil, payload: data)
} else {
let payload_data: Data
if case .BulkString(let data) = payload {
payload_data = data
} else {
throw RedisError.make_invalid_type_error(detail: "Response type (\(value)) is not convertible to PubSubMessage")
}
type = .message(pattern: nil, payload: payload_data)
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
if case .BulkString(let data) = array.popLast() {
type = .message(pattern: pattern, payload: data)
} else {
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

0 comments on commit 3c5773f

Please sign in to comment.