Skip to content

Commit 2b55564

Browse files
author
Guilherme Souza
authored
Fix ChannelTopic init from string and add unit tests (#5)
1 parent a65d04c commit 2b55564

File tree

7 files changed

+34
-63
lines changed

7 files changed

+34
-63
lines changed

Sources/Realtime/Defaults.swift

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public enum ChannelState: String {
7070
/// Represents the different events that can be sent through
7171
/// a channel regarding a Channel's lifecycle or
7272
/// that can be registered to be notified of.
73-
public enum ChannelEvent {
73+
public enum ChannelEvent: RawRepresentable {
7474
case heartbeat
7575
case join
7676
case leave
@@ -85,7 +85,7 @@ public enum ChannelEvent {
8585

8686
case channelReply(String)
8787

88-
public var raw: String {
88+
public var rawValue: String {
8989
switch self {
9090
case .heartbeat: return "heartbeat"
9191
case .join: return "phx_join"
@@ -103,8 +103,8 @@ public enum ChannelEvent {
103103
}
104104
}
105105

106-
public init?(from type: String) {
107-
switch type.lowercased() {
106+
public init?(rawValue: String) {
107+
switch rawValue.lowercased() {
108108
case "heartbeat": self = .heartbeat
109109
case "phx_join": self = .join
110110
case "phx_leave": self = .leave
@@ -124,30 +124,20 @@ public enum ChannelEvent {
124124
switch event {
125125
case .join, .leave, .reply, .error, .close: return true
126126
case .heartbeat, .all, .insert, .update, .delete, .channelReply: return false
127-
// Most likely new events will be about notification
128-
// not about lifecycle.
129-
@unknown default: return false
130127
}
131128
}
132129
}
133130

134-
extension ChannelEvent: Equatable {
135-
public static func ==(lhs: ChannelEvent, rhs: ChannelEvent) -> Bool {
136-
return lhs.raw == rhs.raw
137-
}
138-
}
139-
140-
/// Represents the different topic
141-
// a channel can subscribe to.
142-
public enum ChannelTopic {
131+
/// Represents the different topic a channel can subscribe to.
132+
public enum ChannelTopic: RawRepresentable, Equatable {
143133
case all
144134
case schema(_ schema: String)
145135
case table(_ table: String, schema: String)
146136
case column(_ column: String, value: String, table: String, schema: String)
147137

148138
case heartbeat
149139

150-
public var raw: String {
140+
public var rawValue: String {
151141
switch self {
152142
case .all: return "realtime:*"
153143
case .schema(let name): return "realtime:\(name)"
@@ -157,13 +147,13 @@ public enum ChannelTopic {
157147
}
158148
}
159149

160-
public init?(from type: String) {
161-
if type == "realtime:*" || type == "*" {
150+
public init?(rawValue: String) {
151+
if rawValue == "realtime:*" || rawValue == "*" {
162152
self = .all
163-
} else if type == "phoenix" {
153+
} else if rawValue == "phoenix" {
164154
self = .heartbeat
165155
} else {
166-
let parts = type.split(separator: ":")
156+
let parts = rawValue.replacingOccurrences(of: "realtime:", with: "").split(separator: ":")
167157
switch parts.count {
168158
case 1:
169159
self = .schema(String(parts[0]))
@@ -183,9 +173,3 @@ public enum ChannelTopic {
183173
}
184174
}
185175
}
186-
187-
extension ChannelTopic {
188-
public static func ==(lhs: ChannelTopic, rhs: ChannelTopic) -> Bool {
189-
return lhs.raw == rhs.raw
190-
}
191-
}

Sources/Realtime/Message.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public class Message {
6767
let event = json["event"] as? String,
6868
let payload = json["payload"] as? [String: Any]
6969
{
70-
self.topic = ChannelTopic(from: topic) ?? .all
71-
self.event = ChannelEvent(from: event) ?? .all
70+
self.topic = ChannelTopic(rawValue: topic) ?? .all
71+
self.event = ChannelEvent(rawValue: event) ?? .all
7272
self.payload = payload
7373
} else {
7474
return nil

Sources/Realtime/RealtimeClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ public class RealtimeClient: TransportDelegate {
502502
{
503503
let callback: (() throws -> Void) = {
504504
var body: [String: Any] = [
505-
"topic": topic.raw,
506-
"event": event.raw,
505+
"topic": topic.rawValue,
506+
"event": event.rawValue,
507507
"payload": payload,
508508
]
509509

Tests/LinuxMain.swift

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import XCTest
2+
3+
@testable import Realtime
4+
5+
final class ChannelTopicTests: XCTestCase {
6+
7+
func testRawValue() {
8+
XCTAssertEqual(ChannelTopic.all, ChannelTopic(rawValue: "realtime:*"))
9+
XCTAssertEqual(ChannelTopic.all, ChannelTopic(rawValue: "*"))
10+
XCTAssertEqual(ChannelTopic.schema("public"), ChannelTopic(rawValue: "realtime:public"))
11+
XCTAssertEqual(
12+
ChannelTopic.table("users", schema: "public"), ChannelTopic(rawValue: "realtime:public:users")
13+
)
14+
XCTAssertEqual(
15+
ChannelTopic.column("email", value: "mail@supabase.io", table: "users", schema: "public"),
16+
ChannelTopic(rawValue: "realtime:public:users:email=eq.mail@supabase.io"))
17+
XCTAssertEqual(ChannelTopic.heartbeat, ChannelTopic(rawValue: "phoenix"))
18+
}
19+
}

Tests/RealtimeTests/RealtimeTests.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ final class RealtimeTests: XCTestCase {
4747
}
4848
}
4949

50-
func testTopicSerialization() {
51-
XCTAssertEqual(ChannelTopic.all.raw, "realtime:*")
52-
XCTAssertEqual(ChannelTopic.schema("public").raw,
53-
"realtime:public")
54-
XCTAssertEqual(ChannelTopic.table("users", schema: "public").raw,
55-
"realtime:public:users")
56-
XCTAssertEqual(ChannelTopic.column("id", value: "99", table: "users", schema: "public").raw,
57-
"realtime:public:users:id=eq.99")
58-
}
59-
6050
func testChannelCreation() {
6151
let client = RealtimeClient(endPoint: "\(Self.supabaseUrl())/realtime/v1", params: ["apikey": Self.supabaseKey()])
6252
let allChanges = client.channel(.all)
@@ -118,10 +108,4 @@ final class RealtimeTests: XCTestCase {
118108
}
119109
}
120110
}
121-
122-
static var allTests = [
123-
("testConnection", testConnection),
124-
("testTopicSerialization", testTopicSerialization),
125-
("testChannelCreation", testChannelCreation),
126-
]
127111
}

Tests/RealtimeTests/XCTestManifests.swift

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)