Skip to content

Commit

Permalink
Add ConfidenceContextProvider protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Apr 9, 2024
1 parent 29890f7 commit 7947fdf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
34 changes: 15 additions & 19 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import Foundation

public class Confidence: ConfidenceEventSender {
private let parent: ConfidenceEventSender?
private var localContext: ConfidenceStruct

public var context: ConfidenceStruct {
get {
var mergedContext = parent?.context ?? [:]
localContext.forEach { entry in
mergedContext.updateValue(entry.value, forKey: entry.key)
}
return mergedContext
}
set {
self.localContext = newValue
}
}
private let parent: ConfidenceContextProvider?
private var context: ConfidenceStruct
public let clientSecret: String
public var timeout: TimeInterval
public var region: ConfidenceRegion
Expand All @@ -33,7 +20,7 @@ public class Confidence: ConfidenceEventSender {
self.timeout = timeout
self.region = region
self.initializationStrategy = initializationStrategy
self.localContext = context
self.context = context
self.parent = parent
}

Expand All @@ -42,16 +29,25 @@ public class Confidence: ConfidenceEventSender {
print("Sending \(definition) - Targeting key: \(payload)")
}


public func getContext() -> ConfidenceStruct {
var mergedContext = parent?.getContext() ?? [:]
self.context.forEach { entry in
mergedContext.updateValue(entry.value, forKey: entry.key)
}
return mergedContext
}

public func updateContextEntry(key: String, value: ConfidenceValue) {
localContext[key] = value
context[key] = value
}

public func removeContextEntry(key: String) {
localContext.removeValue(forKey: key)
context.removeValue(forKey: key)
}

public func clearContext() {
localContext = [:]
context = [:]
}

public func withContext(_ context: ConfidenceStruct) -> Self {
Expand Down
6 changes: 6 additions & 0 deletions Sources/Confidence/ConfidenceContextProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

/// A Contextual implementer returns the current context
public protocol ConfidenceContextProvider {
func getContext() -> ConfidenceStruct
}
4 changes: 1 addition & 3 deletions Sources/Confidence/Contextual.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import Foundation

/// A Contextual implementer maintains context data and can create child instances
/// that can still access their parent's data
public protocol Contextual {
var context: ConfidenceStruct { get set }

public protocol Contextual: ConfidenceContextProvider {
func updateContextEntry(key: String, value: ConfidenceValue)
func removeContextEntry(key: String)
func clearContext()
Expand Down
14 changes: 7 additions & 7 deletions Tests/ConfidenceTests/ConfidenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class ConfidenceTests: XCTestCase {
"k1": ConfidenceValue(string: "v1"),
"k2": ConfidenceValue(string: "v2")
]
XCTAssertEqual(confidenceChild.context, expected)
XCTAssertEqual(confidenceChild.getContext(), expected)
}

func testWithContextUpdateParent() {
Expand All @@ -39,7 +39,7 @@ final class ConfidenceTests: XCTestCase {
"k2": ConfidenceValue(string: "v2"),
"k3": ConfidenceValue(string: "v3"),
]
XCTAssertEqual(confidenceChild.context, expected)
XCTAssertEqual(confidenceChild.getContext(), expected)
}

func testUpdateContextWithOverride() {
Expand All @@ -56,7 +56,7 @@ final class ConfidenceTests: XCTestCase {
let expected = [
"k1": ConfidenceValue(string: "v3"),
]
XCTAssertEqual(confidence.context, expected)
XCTAssertEqual(confidence.getContext(), expected)
}

func testWithContextUpdateParentWithoutOverride() {
Expand All @@ -77,7 +77,7 @@ final class ConfidenceTests: XCTestCase {
"k1": ConfidenceValue(string: "v1"),
"k2": ConfidenceValue(string: "v2"),
]
XCTAssertEqual(confidenceChild.context, expected)
XCTAssertEqual(confidenceChild.getContext(), expected)
}

func testWithContextUpdateChildWithOverride() {
Expand All @@ -98,7 +98,7 @@ final class ConfidenceTests: XCTestCase {
"k1": ConfidenceValue(string: "v1"),
"k2": ConfidenceValue(string: "v4"),
]
XCTAssertEqual(confidenceChild.context, expected)
XCTAssertEqual(confidenceChild.getContext(), expected)
}

func testRemoveContextEntry() {
Expand All @@ -116,7 +116,7 @@ final class ConfidenceTests: XCTestCase {
let expected = [
"k1": ConfidenceValue(string: "v1")
]
XCTAssertEqual(confidence.context, expected)
XCTAssertEqual(confidence.getContext(), expected)
}

func testClearContext() {
Expand All @@ -132,6 +132,6 @@ final class ConfidenceTests: XCTestCase {
)
confidence.clearContext()
let expected: ConfidenceStruct = [:]
XCTAssertEqual(confidence.context, expected)
XCTAssertEqual(confidence.getContext(), expected)
}
}

0 comments on commit 7947fdf

Please sign in to comment.