Skip to content

Commit

Permalink
[REFACTOR] refactor EventData:
Browse files Browse the repository at this point in the history
- remove Codable and Equatable.
- remove ContentType
- add Content to store Codable instance or binary data.
  • Loading branch information
Grady Zhuo committed Sep 17, 2024
1 parent a1360a8 commit 294dc6d
Showing 1 changed file with 51 additions and 54 deletions.
105 changes: 51 additions & 54 deletions Sources/EventStoreDB/Event/EventData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,66 @@
import Foundation
import GRPCEncapsulates

public struct EventData: EventStoreEvent, Codable, Equatable {
enum CodingKeys: String, CodingKey {
case id = "eventId"
case eventType
case data
case contentType
case customMetadata
public struct EventData: EventStoreEvent{
public enum Content : Sendable{
case binary(Data)
case json(Codable & Sendable)

internal var contentType: String {
switch self {
case .binary:
return "application/octet-stream"
case .json:
return "application/json"
}
}

internal var data: Data {
get throws{
switch self {
case .binary(let data):
return data
case .json(let json):
return try JSONEncoder().encode(json)
}
}
}

}

public private(set) var id: UUID
public private(set) var eventType: String
public private(set) var data: Data
public private(set) var contentType: ContentType
public private(set) var payload: Content
public private(set) var customMetadata: Data?

public var metadata: [String: String] = [:]

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

let uuidString = try container.decode(String.self, forKey: .id)

guard let id = UUID(uuidString: uuidString) else {
throw ClientError.eventDataError(message: "Couldn't parsed id from jsonData.")
}

let eventType = try container.decode(String.self, forKey: .eventType)
let data = try container.decode(Data.self, forKey: .data)
let customMetadata = try container.decodeIfPresent(Data.self, forKey: .customMetadata)

self.init(id: id, eventType: eventType, data: data, contentType: .json, customMetadata: customMetadata)
}

init(id: UUID, eventType: String, data: Data, contentType: ContentType, customMetadata: Data?) {

public private(set) var metadata: [String: String]

// public init(id: UUID = .init(), eventType: String, payload: Content, contentType: ContentType, customMetadata: Data? = nil) {
// self.id = id
// self.eventType = eventType
// self.content = content
// self.contentType = contentType
// metadata = [
// "content-type": ContentType.json.rawValue,
// "type": eventType,
// ]
// self.customMetadata = customMetadata
// }

public init(id: UUID = .init(), eventType: String, payload: Content, contentType: ContentType, customMetadata: Data? = nil) {
self.id = id
self.eventType = eventType
self.data = data
self.contentType = contentType
metadata = [
"content-type": ContentType.json.rawValue,
"type": eventType,
]
self.payload = payload
self.customMetadata = customMetadata

self.metadata = [
"content-type": payload.contentType,
"type": eventType
]
}

public init(id: UUID = .init(), eventType: String, payload: Codable, customMetadata: Data? = nil) throws {
let encoder = JSONEncoder()
let data = try encoder.encode(payload)
self.init(id: id, eventType: eventType, data: data, contentType: .json, customMetadata: customMetadata)
}

public init(id: UUID = .init(), eventType: String, data: Data, customMetadata: Data? = nil) throws {
self.init(id: id, eventType: eventType, data: data, contentType: .binary, customMetadata: customMetadata)

public init(id: UUID = .init(), eventType: String, payload: Codable & Sendable, customMetadata: Data? = nil) {
self.init(id: id, eventType: eventType, payload: .json(payload), contentType: .json, customMetadata: customMetadata)
}
}

// MARK: - construction methods

extension EventData {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id
&& lhs.contentType == rhs.contentType
&& lhs.eventType == rhs.eventType
&& lhs.data == rhs.data
}
}

0 comments on commit 294dc6d

Please sign in to comment.