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 29890f7
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 10 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
5 changes: 4 additions & 1 deletion Tests/ConfidenceProviderTests/ConfidenceTypeMapperTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class ValueConverterTest: XCTestCase {
"dateList": .list([.date(date1), .date(date2)]),
"nullList": .list([.null, .null]),
"listList": .list([.list([.string("nested_value1")]), .list([.string("nested_value2")])]),
"structList": .list([.structure(["test": .string("nested_test1")]), .structure(["test": .string("nested_test2")])])
"structList": .list([
.structure(["test": .string("nested_test1")]),
.structure(["test": .string("nested_test2")])
])
])))
let confidenceStruct = ConfidenceTypeMapper.from(ctx: openFeatureCtx)
let expected = [
Expand Down
137 changes: 137 additions & 0 deletions Tests/ConfidenceTests/ConfidenceTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import Confidence
import XCTest

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

func testWithContextUpdateParent() {
let confidenceParent = Confidence.init(
clientSecret: "",
timeout: TimeInterval(),
region: .europe,
initializationStrategy: .activateAndFetchAsync,
context: ["k1": ConfidenceValue(string: "v1")]
)
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext(
["k2": ConfidenceValue(string: "v2")]
)
confidenceParent.updateContextEntry(
key: "k3",
value: ConfidenceValue(string: "v3"))
let expected = [
"k1": ConfidenceValue(string: "v1"),
"k2": ConfidenceValue(string: "v2"),
"k3": ConfidenceValue(string: "v3"),
]
XCTAssertEqual(confidenceChild.context, expected)
}

func testUpdateContextWithOverride() {
let confidence = Confidence.init(
clientSecret: "",
timeout: TimeInterval(),
region: .europe,
initializationStrategy: .activateAndFetchAsync,
context: ["k1": ConfidenceValue(string: "v1")]
)
confidence.updateContextEntry(
key: "k1",
value: ConfidenceValue(string: "v3"))
let expected = [
"k1": ConfidenceValue(string: "v3"),
]
XCTAssertEqual(confidence.context, expected)
}

func testWithContextUpdateParentWithoutOverride() {
let confidenceParent = Confidence.init(
clientSecret: "",
timeout: TimeInterval(),
region: .europe,
initializationStrategy: .activateAndFetchAsync,
context: ["k1": ConfidenceValue(string: "v1")]
)
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext(
["k2": ConfidenceValue(string: "v2")]
)
confidenceParent.updateContextEntry(
key: "k2",
value: ConfidenceValue(string: "v4"))
let expected = [
"k1": ConfidenceValue(string: "v1"),
"k2": ConfidenceValue(string: "v2"),
]
XCTAssertEqual(confidenceChild.context, expected)
}

func testWithContextUpdateChildWithOverride() {
let confidenceParent = Confidence.init(
clientSecret: "",
timeout: TimeInterval(),
region: .europe,
initializationStrategy: .activateAndFetchAsync,
context: ["k1": ConfidenceValue(string: "v1")]
)
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext(
["k2": ConfidenceValue(string: "v2")]
)
confidenceChild.updateContextEntry(
key: "k2",
value: ConfidenceValue(string: "v4"))
let expected = [
"k1": ConfidenceValue(string: "v1"),
"k2": ConfidenceValue(string: "v4"),
]
XCTAssertEqual(confidenceChild.context, expected)
}

func testRemoveContextEntry() {
let confidence = Confidence.init(
clientSecret: "",
timeout: TimeInterval(),
region: .europe,
initializationStrategy: .activateAndFetchAsync,
context: [
"k1": ConfidenceValue(string: "v1"),
"k2": ConfidenceValue(string: "v2")
]
)
confidence.removeContextEntry(key: "k2")
let expected = [
"k1": ConfidenceValue(string: "v1")
]
XCTAssertEqual(confidence.context, expected)
}

func testClearContext() {
let confidence = Confidence.init(
clientSecret: "",
timeout: TimeInterval(),
region: .europe,
initializationStrategy: .activateAndFetchAsync,
context: [
"k1": ConfidenceValue(string: "v1"),
"k2": ConfidenceValue(string: "v2")
]
)
confidence.clearContext()
let expected: ConfidenceStruct = [:]
XCTAssertEqual(confidence.context, expected)
}
}

0 comments on commit 29890f7

Please sign in to comment.