Skip to content

Commit

Permalink
fix: Add payload merger to merge context and message (#108)
Browse files Browse the repository at this point in the history
* add payload merger to merge context and message

* fixup! add payload merger to merge context and message

* fixup! fixup! add payload merger to merge context and message
  • Loading branch information
vahidlazio authored Apr 30, 2024
1 parent 996b272 commit 3386dd6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
5 changes: 2 additions & 3 deletions Sources/Confidence/EventSenderEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ final class EventSenderEngineImpl: EventSenderEngine {
private let flushPolicies: [FlushPolicy]
private let uploader: ConfidenceClient
private let clientSecret: String
private let payloadMerger: PayloadMerger = PayloadMergerImpl()

init(
clientSecret: String,
Expand Down Expand Up @@ -79,11 +80,9 @@ final class EventSenderEngineImpl: EventSenderEngine {
}

func emit(eventName: String, message: ConfidenceStruct, context: ConfidenceStruct) {
var mutablePayload = context
mutablePayload["message"] = ConfidenceValue(structure: message)
writeReqChannel.send(ConfidenceEvent(
name: eventName,
payload: mutablePayload,
payload: payloadMerger.merge(context: context, message: message),
eventTime: Date.backport.now)
)
}
Expand Down
19 changes: 19 additions & 0 deletions Sources/Confidence/PayloadMerger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Foundation

internal protocol PayloadMerger {
func merge(context: ConfidenceStruct, message: ConfidenceStruct) -> ConfidenceStruct
}

internal struct PayloadMergerImpl: PayloadMerger {
func merge(context: ConfidenceStruct, message: ConfidenceStruct) -> ConfidenceStruct {
var map: ConfidenceStruct = context
map += message
return map
}
}

extension Dictionary {
static func += (lhs: inout Self, rhs: Self) {
lhs.merge(rhs) { _, new in new }
}
}
7 changes: 2 additions & 5 deletions Tests/ConfidenceTests/EventSenderEngineTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ final class EventSenderEngineTest: XCTestCase {
wait(for: [expectation], timeout: 5)
XCTAssertEqual(try XCTUnwrap(uploader.calledRequest)[0].eventDefinition, "my_event")
XCTAssertEqual(try XCTUnwrap(uploader.calledRequest)[0].payload, NetworkStruct(fields: [
"message": .structure(.init(fields: [
"a": .number(0.0),
"message": .number(1.0)
])),
"a": .number(2.0)
"a": .number(0.0),
"message": .number(1.0)
]))
cancellable.cancel()
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/ConfidenceTests/PayloadMergerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import XCTest
@testable import Confidence

class PayloadMergerTests: XCTestCase {
func testMerge() {
let context = ["a": ConfidenceValue(string: "hello"), "b": ConfidenceValue(string: "world")]
let message = ["b": ConfidenceValue(string: "west"), "c": ConfidenceValue(string: "world")]
let expected = [
"a": ConfidenceValue(string: "hello"),
"b": ConfidenceValue(string: "west"),
"c": ConfidenceValue(string: "world")
]
let merged = PayloadMergerImpl().merge(context: context, message: message)
XCTAssertEqual(merged, expected)
}
}

0 comments on commit 3386dd6

Please sign in to comment.