-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add payload merger to merge context and message (#108)
* 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
1 parent
996b272
commit 3386dd6
Showing
4 changed files
with
39 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |