From f85dc9ba6d13ac65260ab288c3ee26dc0af723c5 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Wed, 29 May 2024 13:59:24 +0200 Subject: [PATCH] refactor: from "message" to "data" (#132) --- .../ConfidenceDemoApp/ConfidenceDemoApp.swift | 2 +- README.md | 2 +- Sources/Confidence/Confidence.swift | 6 +++--- .../ConfidenceAppLifecycleProducer.swift | 6 +++--- Sources/Confidence/ConfidenceEventSender.swift | 2 +- Sources/Confidence/ConfidenceProducer.swift | 6 +++--- Sources/Confidence/ConfidenceScreenTracker.swift | 8 ++++---- Sources/Confidence/EventSenderEngine.swift | 6 +++--- Sources/Confidence/PayloadMerger.swift | 8 ++++---- .../ConfidenceFeatureProviderTest.swift | 2 +- .../ConfidenceTests/EventSenderEngineTest.swift | 16 ++++++++-------- .../Helpers/EventSenderEngineMock.swift | 2 +- Tests/ConfidenceTests/PayloadMergerTests.swift | 4 ++-- 13 files changed, 35 insertions(+), 35 deletions(-) diff --git a/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift b/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift index 8a79450f..93493d2a 100644 --- a/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift +++ b/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift @@ -46,7 +46,7 @@ extension ConfidenceDemoApp { try await confidence.fetchAndActivate() confidence.track( eventName: "all-types", - message: [ + data: [ "my_string": ConfidenceValue(string: "hello_from_world"), "my_timestamp": ConfidenceValue(timestamp: Date()), "my_bool": ConfidenceValue(boolean: true), diff --git a/README.md b/README.md index bfb45084..b2cd0512 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ let messageValue = messageFlag.value ### Tracking events The Confidence instance offers APIs to track events, which are uploaded to the Confidence backend: ```swift -confidence.track(eventName: "MyEvent", message: ["field": ConfidenceValue(string("value"))]) +confidence.track(eventName: "MyEvent", data: ["field": ConfidenceValue(string("value"))]) ``` The SDK takes care of storing events in case of offline and retries in case of transient failures. diff --git a/Sources/Confidence/Confidence.swift b/Sources/Confidence/Confidence.swift index ace0e236..2db8b68c 100644 --- a/Sources/Confidence/Confidence.swift +++ b/Sources/Confidence/Confidence.swift @@ -114,10 +114,10 @@ public class Confidence: ConfidenceEventSender { .eraseToAnyPublisher() } - public func track(eventName: String, message: ConfidenceStruct) throws { + public func track(eventName: String, data: ConfidenceStruct) throws { try eventSenderEngine.emit( eventName: eventName, - message: message, + data: data, context: getContext() ) } @@ -130,7 +130,7 @@ public class Confidence: ConfidenceEventSender { return } do { - try self.track(eventName: event.name, message: event.message) + try self.track(eventName: event.name, data: event.data) if event.shouldFlush { eventSenderEngine.flush() } diff --git a/Sources/Confidence/ConfidenceAppLifecycleProducer.swift b/Sources/Confidence/ConfidenceAppLifecycleProducer.swift index 4257e555..10418d81 100644 --- a/Sources/Confidence/ConfidenceAppLifecycleProducer.swift +++ b/Sources/Confidence/ConfidenceAppLifecycleProducer.swift @@ -71,18 +71,18 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence if previousBuild == nil && previousVersion == nil { events.send(Event( name: ConfidenceAppLifecycleProducer.appInstalledEventName, - message: [:], + data: [:], shouldFlush: shouldFlush) ) } else if previousBuild != currentBuild || previousVersion != currentVersion { events.send(Event( name: ConfidenceAppLifecycleProducer.appUpdatedEventName, - message: [:], + data: [:], shouldFlush: shouldFlush) ) } } - events.send(Event(name: eventName, message: [:], shouldFlush: shouldFlush)) + events.send(Event(name: eventName, data: [:], shouldFlush: shouldFlush)) UserDefaults.standard.setValue(currentVersion, forKey: Self.userDefaultVersionNameKey) UserDefaults.standard.setValue(currentBuild, forKey: Self.userDefaultBuildNameKey) diff --git a/Sources/Confidence/ConfidenceEventSender.swift b/Sources/Confidence/ConfidenceEventSender.swift index e5a84f80..f1af53b4 100644 --- a/Sources/Confidence/ConfidenceEventSender.swift +++ b/Sources/Confidence/ConfidenceEventSender.swift @@ -8,7 +8,7 @@ public protocol ConfidenceEventSender: Contextual { Upon return, the event has been correctly stored and will be emitted to the backend according to the configured flushing logic */ - func track(eventName: String, message: ConfidenceStruct) throws + func track(eventName: String, data: ConfidenceStruct) throws /** The ConfidenceProducer can be used to push context changes or event tracking */ diff --git a/Sources/Confidence/ConfidenceProducer.swift b/Sources/Confidence/ConfidenceProducer.swift index 57074309..ca6a7dc9 100644 --- a/Sources/Confidence/ConfidenceProducer.swift +++ b/Sources/Confidence/ConfidenceProducer.swift @@ -9,12 +9,12 @@ public protocol ConfidenceProducer { public struct Event { let name: String - let message: ConfidenceStruct + let data: ConfidenceStruct let shouldFlush: Bool - public init(name: String, message: ConfidenceStruct = [:], shouldFlush: Bool = false) { + public init(name: String, data: ConfidenceStruct = [:], shouldFlush: Bool = false) { self.name = name - self.message = message + self.data = data self.shouldFlush = shouldFlush } } diff --git a/Sources/Confidence/ConfidenceScreenTracker.swift b/Sources/Confidence/ConfidenceScreenTracker.swift index 527ef01d..7afe14c0 100644 --- a/Sources/Confidence/ConfidenceScreenTracker.swift +++ b/Sources/Confidence/ConfidenceScreenTracker.swift @@ -29,11 +29,11 @@ public class ConfidenceScreenTracker: ConfidenceEventProducer { queue: OperationQueue.main) { [weak self] notification in let name = notification.userInfo?[Self.screenName] as? String let messageJson = (notification.userInfo?[Self.messageKey] as? String)?.data(using: .utf8) - var message: ConfidenceStruct = [:] - if let data = messageJson { + var data: ConfidenceStruct = [:] + if let messageData = messageJson { let decoder = JSONDecoder() do { - message = try decoder.decode(ConfidenceStruct.self, from: data) + data = try decoder.decode(ConfidenceStruct.self, from: messageData) } catch { } } @@ -42,7 +42,7 @@ public class ConfidenceScreenTracker: ConfidenceEventProducer { return } if let name = name { - self.events.send(Event(name: name, message: message)) + self.events.send(Event(name: name, data: data)) } } } diff --git a/Sources/Confidence/EventSenderEngine.swift b/Sources/Confidence/EventSenderEngine.swift index 4fa8f98a..9a83e201 100644 --- a/Sources/Confidence/EventSenderEngine.swift +++ b/Sources/Confidence/EventSenderEngine.swift @@ -8,7 +8,7 @@ protocol FlushPolicy { } protocol EventSenderEngine { - func emit(eventName: String, message: ConfidenceStruct, context: ConfidenceStruct) throws + func emit(eventName: String, data: ConfidenceStruct, context: ConfidenceStruct) throws func shutdown() func flush() } @@ -119,10 +119,10 @@ final class EventSenderEngineImpl: EventSenderEngine { semaphore.signal() } - func emit(eventName: String, message: ConfidenceStruct, context: ConfidenceStruct) throws { + func emit(eventName: String, data: ConfidenceStruct, context: ConfidenceStruct) throws { writeReqChannel.send(ConfidenceEvent( name: eventName, - payload: try payloadMerger.merge(context: context, message: message), + payload: try payloadMerger.merge(context: context, data: data), eventTime: Date.backport.now) ) } diff --git a/Sources/Confidence/PayloadMerger.swift b/Sources/Confidence/PayloadMerger.swift index 840bcf0d..a2a0d831 100644 --- a/Sources/Confidence/PayloadMerger.swift +++ b/Sources/Confidence/PayloadMerger.swift @@ -1,15 +1,15 @@ import Foundation internal protocol PayloadMerger { - func merge(context: ConfidenceStruct, message: ConfidenceStruct) throws -> ConfidenceStruct + func merge(context: ConfidenceStruct, data: ConfidenceStruct) throws -> ConfidenceStruct } internal struct PayloadMergerImpl: PayloadMerger { - func merge(context: ConfidenceStruct, message: ConfidenceStruct) throws -> ConfidenceStruct { - guard message["context"] == nil else { + func merge(context: ConfidenceStruct, data: ConfidenceStruct) throws -> ConfidenceStruct { + guard data["context"] == nil else { throw ConfidenceError.invalidContextInMessage } - var map: ConfidenceStruct = message + var map: ConfidenceStruct = data map["context"] = ConfidenceValue.init(structure: context) return map } diff --git a/Tests/ConfidenceTests/ConfidenceFeatureProviderTest.swift b/Tests/ConfidenceTests/ConfidenceFeatureProviderTest.swift index e167a886..d400e83c 100644 --- a/Tests/ConfidenceTests/ConfidenceFeatureProviderTest.swift +++ b/Tests/ConfidenceTests/ConfidenceFeatureProviderTest.swift @@ -576,7 +576,7 @@ class ConfidenceFeatureProviderTest: XCTestCase { .build() XCTAssertThrowsError( - try confidence.track(eventName: "test", message: ["context": ConfidenceValue(string: "test")]) + try confidence.track(eventName: "test", data: ["context": ConfidenceValue(string: "test")]) ) { error in XCTAssertEqual(error as? ConfidenceError, ConfidenceError.invalidContextInMessage) } diff --git a/Tests/ConfidenceTests/EventSenderEngineTest.swift b/Tests/ConfidenceTests/EventSenderEngineTest.swift index ec571dfd..c2a78e04 100644 --- a/Tests/ConfidenceTests/EventSenderEngineTest.swift +++ b/Tests/ConfidenceTests/EventSenderEngineTest.swift @@ -68,7 +68,7 @@ final class EventSenderEngineTest: XCTestCase { } try eventSenderEngine.emit( eventName: "my_event", - message: [ + data: [ "a": .init(integer: 0) ], context: [ @@ -100,7 +100,7 @@ final class EventSenderEngineTest: XCTestCase { writeQueue: writeQueue ) - try eventSenderEngine.emit(eventName: "Hello", message: [:], context: [:]) + try eventSenderEngine.emit(eventName: "Hello", data: [:], context: [:]) // TODO: We need to wait for writeReqChannel to complete to make this test meaningful XCTAssertNil(uploaderMock.calledRequest) } @@ -119,7 +119,7 @@ final class EventSenderEngineTest: XCTestCase { flushPolicies: [ImmidiateFlushPolicy()], writeQueue: writeQueue ) - try eventSenderEngine.emit(eventName: "testEvent", message: ConfidenceStruct(), context: ConfidenceStruct()) + try eventSenderEngine.emit(eventName: "testEvent", data: ConfidenceStruct(), context: ConfidenceStruct()) let expectation = expectation(description: "events batched") storageMock.eventsRemoved{ expectation.fulfill() @@ -144,7 +144,7 @@ final class EventSenderEngineTest: XCTestCase { writeQueue: writeQueue ) - try eventSenderEngine.emit(eventName: "testEvent", message: ConfidenceStruct(), context: ConfidenceStruct()) + try eventSenderEngine.emit(eventName: "testEvent", data: ConfidenceStruct(), context: ConfidenceStruct()) writeQueue.sync { XCTAssertEqual(storageMock.isEmpty(), false) @@ -161,10 +161,10 @@ final class EventSenderEngineTest: XCTestCase { writeQueue: writeQueue ) - try eventSenderEngine.emit(eventName: "Hello", message: [:], context: [:]) - try eventSenderEngine.emit(eventName: "Hello", message: [:], context: [:]) - try eventSenderEngine.emit(eventName: "Hello", message: [:], context: [:]) - try eventSenderEngine.emit(eventName: "Hello", message: [:], context: [:]) + try eventSenderEngine.emit(eventName: "Hello", data: [:], context: [:]) + try eventSenderEngine.emit(eventName: "Hello", data: [:], context: [:]) + try eventSenderEngine.emit(eventName: "Hello", data: [:], context: [:]) + try eventSenderEngine.emit(eventName: "Hello", data: [:], context: [:]) writeQueue.sync { diff --git a/Tests/ConfidenceTests/Helpers/EventSenderEngineMock.swift b/Tests/ConfidenceTests/Helpers/EventSenderEngineMock.swift index 7ffb63dc..adbd1efb 100644 --- a/Tests/ConfidenceTests/Helpers/EventSenderEngineMock.swift +++ b/Tests/ConfidenceTests/Helpers/EventSenderEngineMock.swift @@ -2,7 +2,7 @@ import Foundation @testable import Confidence class EventSenderEngineMock: EventSenderEngine { - func emit(eventName: String, message: ConfidenceStruct, context: ConfidenceStruct) { + func emit(eventName: String, data: ConfidenceStruct, context: ConfidenceStruct) { // NO-OP } diff --git a/Tests/ConfidenceTests/PayloadMergerTests.swift b/Tests/ConfidenceTests/PayloadMergerTests.swift index 29e59ac0..8d06fc2f 100644 --- a/Tests/ConfidenceTests/PayloadMergerTests.swift +++ b/Tests/ConfidenceTests/PayloadMergerTests.swift @@ -13,7 +13,7 @@ class PayloadMergerTests: XCTestCase { "b": ConfidenceValue(string: "world"), ]) ] - let merged = try PayloadMergerImpl().merge(context: context, message: message) + let merged = try PayloadMergerImpl().merge(context: context, data: message) XCTAssertEqual(merged, expected) } @@ -24,7 +24,7 @@ class PayloadMergerTests: XCTestCase { "context": ConfidenceValue(string: "world") // simple value context is lost ] XCTAssertThrowsError( - try PayloadMergerImpl().merge(context: context, message: message) + try PayloadMergerImpl().merge(context: context, data: message) ) { error in XCTAssertEqual(error as? ConfidenceError, ConfidenceError.invalidContextInMessage) }