From 3c5773f840e29965c690b6451c335b890ba26c2c Mon Sep 17 00:00:00 2001 From: Jiaan Fang Date: Wed, 27 Dec 2023 20:22:19 +0800 Subject: [PATCH] Refactor 'pmessage' parsing to correctly handle patterns --- Sources/SwiftyRedis/PubSub.swift | 38 ++++++++++++++------------------ 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/Sources/SwiftyRedis/PubSub.swift b/Sources/SwiftyRedis/PubSub.swift index 53ce784b..6addac13 100644 --- a/Sources/SwiftyRedis/PubSub.swift +++ b/Sources/SwiftyRedis/PubSub.swift @@ -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)\")")