diff --git a/Sources/Confidence/EventSenderEngine.swift b/Sources/Confidence/EventSenderEngine.swift index e1584da8..3092c6db 100644 --- a/Sources/Confidence/EventSenderEngine.swift +++ b/Sources/Confidence/EventSenderEngine.swift @@ -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, @@ -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) ) } diff --git a/Sources/Confidence/PayloadMerger.swift b/Sources/Confidence/PayloadMerger.swift new file mode 100644 index 00000000..978e5742 --- /dev/null +++ b/Sources/Confidence/PayloadMerger.swift @@ -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 } + } +} diff --git a/Tests/ConfidenceTests/EventSenderEngineTest.swift b/Tests/ConfidenceTests/EventSenderEngineTest.swift index e9576174..07dfa0b2 100644 --- a/Tests/ConfidenceTests/EventSenderEngineTest.swift +++ b/Tests/ConfidenceTests/EventSenderEngineTest.swift @@ -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() } diff --git a/Tests/ConfidenceTests/PayloadMergerTests.swift b/Tests/ConfidenceTests/PayloadMergerTests.swift new file mode 100644 index 00000000..5e63c55e --- /dev/null +++ b/Tests/ConfidenceTests/PayloadMergerTests.swift @@ -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) + } +}