Skip to content

Commit

Permalink
Implement withContext
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Apr 9, 2024
1 parent 8de4b78 commit 907dd0b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
40 changes: 31 additions & 9 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
import Foundation

public class Confidence: ConfidenceEventSender {
public var context: ConfidenceStruct
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
}
}
public let clientSecret: String
public var timeout: TimeInterval
public var region: ConfidenceRegion
public var initializationStrategy: InitializationStrategy

init(
required public init(
clientSecret: String,
timeout: TimeInterval,
region: ConfidenceRegion,
initializationStrategy: InitializationStrategy
initializationStrategy: InitializationStrategy,
context: ConfidenceStruct = [:],
parent: ConfidenceEventSender? = nil
) {
self.context = [:]
self.clientSecret = clientSecret
self.timeout = timeout
self.region = region
self.initializationStrategy = initializationStrategy
self.localContext = context
self.parent = parent
}

// TODO: Implement actual event uploading to the backend
Expand All @@ -26,20 +43,25 @@ public class Confidence: ConfidenceEventSender {
}

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

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

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

// TODO: Implement creation of child instances
public func withContext(_ context: ConfidenceStruct) -> Self {
return self
return Self.init(
clientSecret: clientSecret,
timeout: timeout,
region: region,
initializationStrategy: initializationStrategy,
context: context,
parent: self)
}
}

Expand Down
44 changes: 44 additions & 0 deletions Tests/ConfidenceTests/ConfidenceTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Confidence
import XCTest

final class ConfidenceTests: XCTestCase {
func testWithContext() {
let confidenceParent = Confidence.init(
clientSecret: "",
timeout: TimeInterval(),
region: .europe,
initializationStrategy: .activateAndFetchAsync,
context: ["parent_key": ConfidenceValue(string: "parent_value")]
)
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext(
["child_key": ConfidenceValue(string: "child_value")]
)
let expected = [
"parent_key": ConfidenceValue(string: "parent_value"),
"child_key": ConfidenceValue(string: "child_value")
]
XCTAssertEqual(confidenceChild.context, expected)
}

func testWithContextUpdateParent() {
let confidenceParent = Confidence.init(
clientSecret: "",
timeout: TimeInterval(),
region: .europe,
initializationStrategy: .activateAndFetchAsync,
context: ["parent_key": ConfidenceValue(string: "parent_value")]
)
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext(
["child_key": ConfidenceValue(string: "child_value")]
)
confidenceParent.updateContextEntry(
key: "new_parent_key",
value: ConfidenceValue(string: "new_parent_value"))
let expected = [
"parent_key": ConfidenceValue(string: "parent_value"),
"child_key": ConfidenceValue(string: "child_value"),
"new_parent_key": ConfidenceValue(string: "new_parent_value"),
]
XCTAssertEqual(confidenceChild.context, expected)
}
}

0 comments on commit 907dd0b

Please sign in to comment.