Skip to content

Commit

Permalink
[Remove] EventData from JSON data, it should be rewrite for good idea.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grady Zhuo committed Jun 22, 2024
1 parent 010d405 commit 472d904
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 35 deletions.
33 changes: 1 addition & 32 deletions Sources/EventStoreDB/Event/EventData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,41 +67,10 @@ public struct EventData: EventStoreEvent, Codable, Equatable {
// MARK: - construction methods

extension EventData {
public static func events(fromJSONString jsonString: String, encoding: String.Encoding = .utf8, customMetadata _: Data? = nil) throws -> [Self] {
guard let data = jsonString.data(using: encoding) else {
throw ClientError.eventDataError(message: "The jsonString can't be encoded into binary data. \(jsonString)")
}

return try events(fromJSONData: data)
}

public static func event(fromJSONString jsonString: String, encoding: String.Encoding = .utf8, customMetadata _: [String: Codable] = [:]) throws -> Self {
guard let data = jsonString.data(using: encoding) else {
throw ClientError.eventDataError(message: "The jsonString can't be encoded into binary data. \(jsonString)")
}

return try event(fromJSONData: data)
}

public static func events(fromJSONData jsonData: Data, encoding _: String.Encoding = .utf8, customMetadata: Data? = nil) throws -> [Self] {
let decoder = JSONDecoder()
return try decoder.decode([Self].self, from: jsonData).map {
var event = $0
event.customMetadata = customMetadata
return event
}
}

public static func event(fromJSONData jsonData: Data, encoding _: String.Encoding = .utf8, customMetadata: Data? = nil) throws -> Self {
let decoder = JSONDecoder()
var event = try decoder.decode(Self.self, from: jsonData)
event.customMetadata = customMetadata
return event
}

public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id
&& lhs.contentType == rhs.contentType
&& lhs.eventType == rhs.eventType
&& lhs.data == rhs.data
}
}
6 changes: 3 additions & 3 deletions Tests/EventStoreDBTests/EventStoreDBTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ final class EventStoreDBTests: XCTestCase {
// Defining Test Cases and Test Methods
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods

let jsonFileURL = Bundle.module.url(forResource: "multiple-events", withExtension: "json")
let jsonData = try Data(contentsOf: jsonFileURL!)
_ = try EventData.events(fromJSONData: jsonData)
// let jsonFileURL = Bundle.module.url(forResource: "multiple-events", withExtension: "json")
// let jsonData = try Data(contentsOf: jsonFileURL!)
// _ = try EventData.events(fromJSONData: jsonData)

// try XCTAssertEqual(events, [
// .init(id: .init(uuidString: "fbf4b1a1-b4a3-4dfe-a01f-ec52c34e16e4")!, eventType: "event-type", data: "test".data(using: .utf8)!, contentType: .json, customMetadata: nil),
Expand Down

0 comments on commit 472d904

Please sign in to comment.