Skip to content

Commit

Permalink
Added parsing messages without field name
Browse files Browse the repository at this point in the history
  • Loading branch information
Recouse committed Jan 26, 2024
1 parent 2a2b163 commit e3b9936
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
46 changes: 39 additions & 7 deletions Sources/EventSource/ServerMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ public struct ServerMessage {
public var id: String?
public var event: String?
public var data: String?
public var other: [String: String]?
public var time: String?

init(
id: String? = nil,
event: String? = nil,
data: String? = nil,
other: [String: String]? = nil,
time: String? = nil
) {
self.id = id
self.event = event
self.data = data
self.other = other
self.time = time
}

Expand All @@ -39,6 +42,10 @@ public struct ServerMessage {
return false
}

if let other, !other.isEmpty {
return false
}

if let time, !time.isEmpty {
return false
}
Expand All @@ -52,25 +59,41 @@ public struct ServerMessage {
var message = ServerMessage()

for row in rows {
// Skip the line if it is empty or it starts with a colon character
if row.isEmpty, row.first == MessageParser.colon {
continue
}

let keyValue = row.split(separator: MessageParser.colon, maxSplits: 1)
let key = keyValue[0].utf8String.trimmingCharacters(in: .whitespaces)
let value = keyValue[1].utf8String.trimmingCharacters(in: .whitespaces)
let value = keyValue[safe: 1]?.utf8String.trimmingCharacters(in: .whitespaces)

switch key {
case "id":
message.id = value.trimmingCharacters(in: .whitespacesAndNewlines)
message.id = value?.trimmingCharacters(in: .whitespaces)
case "event":
message.event = value.trimmingCharacters(in: .whitespacesAndNewlines)
message.event = value?.trimmingCharacters(in: .whitespaces)
case "data":
if let existingData = message.data {
message.data = existingData + "\n" + value.trimmingCharacters(in: .whitespacesAndNewlines)
message.data = existingData + "\n" + (value?.trimmingCharacters(in: .whitespaces) ?? "")
} else {
message.data = value.trimmingCharacters(in: .whitespacesAndNewlines)
message.data = value?.trimmingCharacters(in: .whitespaces)
}
case "time":
message.time = value.trimmingCharacters(in: .whitespacesAndNewlines)
message.time = value?.trimmingCharacters(in: .whitespaces)
default:
continue
// If the line is not empty but does not contain a color character
// add it to the other fields using the whole line as the field name,
// and the empty string as the field value.
if row.contains(MessageParser.colon) == false {
let string = row.utf8String
if var other = message.other {
other[string] = ""
message.other = other
} else {
message.other = [string: ""]
}
}
}
}

Expand All @@ -87,3 +110,12 @@ fileprivate extension Data {
String(decoding: self, as: UTF8.self)
}
}

fileprivate extension Array {
subscript(safe index: Int) -> Element? {
guard index >= 0, index < endIndex else {
return nil
}
return self[index]
}
}
12 changes: 12 additions & 0 deletions Tests/EventSourceTests/MessageParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ final class MessageParserTests: XCTestCase {
id : 4
event : ping
data : test 4
test 5
message 6
message 6-1
"""
let data = Data(text.utf8)

Expand All @@ -109,5 +114,12 @@ final class MessageParserTests: XCTestCase {
XCTAssertEqual(messages[3].id!, "4")
XCTAssertEqual(messages[3].event!, "ping")
XCTAssertEqual(messages[3].data!, "test 4")

XCTAssertNotNil(messages[4].other)
XCTAssertEqual(messages[4].other!["test 5"], "")

XCTAssertNotNil(messages[5].other)
XCTAssertEqual(messages[5].other!["message 6"], "")
XCTAssertEqual(messages[5].other!["message 6-1"], "")
}
}

0 comments on commit e3b9936

Please sign in to comment.