Skip to content

Commit

Permalink
refactor: from "message" to "data" (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria authored May 29, 2024
1 parent a9d41fa commit f85dc9b
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}
Expand All @@ -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()
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Confidence/ConfidenceAppLifecycleProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Confidence/ConfidenceEventSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
6 changes: 3 additions & 3 deletions Sources/Confidence/ConfidenceProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Confidence/ConfidenceScreenTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
}
Expand All @@ -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))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Confidence/EventSenderEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)
)
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Confidence/PayloadMerger.swift
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ConfidenceTests/ConfidenceFeatureProviderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 8 additions & 8 deletions Tests/ConfidenceTests/EventSenderEngineTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class EventSenderEngineTest: XCTestCase {
}
try eventSenderEngine.emit(
eventName: "my_event",
message: [
data: [
"a": .init(integer: 0)
],
context: [
Expand Down Expand Up @@ -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)
}
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Tests/ConfidenceTests/Helpers/EventSenderEngineMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/ConfidenceTests/PayloadMergerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}
Expand Down

0 comments on commit f85dc9b

Please sign in to comment.