Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adopt latest [WIP] OF SDK #184

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"package": "OpenFeature",
"repositoryURL": "[email protected]:open-feature/swift-sdk.git",
"state": {
"branch": null,
"revision": "02b033c954766e86d5706bfc8ee5248244c11e77",
"version": "0.1.0"
"branch": "fatal-state",
"revision": "ac150987ad2ff7657e7d976cd304880bc6d33784",
"version": null
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let package = Package(
targets: ["Confidence"])
],
dependencies: [
.package(url: "[email protected]:open-feature/swift-sdk.git", from: "0.1.0"),
.package(url: "[email protected]:open-feature/swift-sdk.git", branch: "fatal-state"),
],
targets: [
.target(
Expand Down
41 changes: 14 additions & 27 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
public var hooks: [any Hook] = []
private let lock = UnfairLock()
private let initializationStrategy: InitializationStrategy
private let eventHandler = EventHandler(ProviderEvent.notReady)
private let eventHandler = EventHandler()
private let confidence: Confidence
private let confidenceFeatureProviderQueue = DispatchQueue(label: "com.provider.queue")
private var cancellables = Set<AnyCancellable>()
Expand All @@ -26,7 +26,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
The `initializationStrategy` defines when the Provider is ready to read flags, before or after a refresh of the flag evaluation fata.
*/
public convenience init(confidence: Confidence, initializationStrategy: InitializationStrategy = .fetchAndActivate) {
self.init(confidence: confidence, session: nil)
self.init(confidence: confidence, initializationStrategy: initializationStrategy, session: nil)
}

internal init(
Expand All @@ -39,28 +39,16 @@ public class ConfidenceFeatureProvider: FeatureProvider {
self.confidence = confidence
}

public func initialize(initialContext: OpenFeature.EvaluationContext?) {
public func initialize(initialContext: OpenFeature.EvaluationContext?) async throws {
let context = ConfidenceTypeMapper.from(ctx: initialContext ?? MutableContext(attributes: [:]))
confidence.putContextLocal(context: context)
do {
if initializationStrategy == .activateAndFetchAsync {
try confidence.activate()
eventHandler.send(.ready)
Task {
await confidence.asyncFetch()
}
} else {
Task {
do {
try await confidence.fetchAndActivate()
eventHandler.send(.ready)
} catch {
eventHandler.send(.error)
}
}
if initializationStrategy == .activateAndFetchAsync {
try confidence.activate()
Task {
await confidence.asyncFetch()
}
} catch {
eventHandler.send(.error)
} else {
try await confidence.fetchAndActivate()
}
}

Expand All @@ -75,14 +63,13 @@ public class ConfidenceFeatureProvider: FeatureProvider {
public func onContextSet(
oldContext: OpenFeature.EvaluationContext?,
newContext: OpenFeature.EvaluationContext
) {
) async {
let removedKeys: [String] = oldContext.map {
Array($0.asMap().filter { key, _ in !newContext.asMap().keys.contains(key) }.keys)
} ?? []

Task {
confidence.putContext(context: ConfidenceTypeMapper.from(ctx: newContext), removedKeys: removedKeys)
}
await confidence.putContextAndWait(
context: ConfidenceTypeMapper.from(ctx: newContext),
removedKeys: removedKeys)
}

public func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws
Expand Down Expand Up @@ -115,7 +102,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
try confidence.getEvaluation(key: key, defaultValue: defaultValue).toProviderEvaluation()
}

public func observe() -> AnyPublisher<OpenFeature.ProviderEvent, Never> {
public func observe() -> AnyPublisher<OpenFeature.ProviderEvent?, Never> {
return eventHandler.observe()
}

Expand Down
28 changes: 16 additions & 12 deletions Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ class ConfidenceProviderTest: XCTestCase {
.withFlagResolverClient(flagResolver: client)
.build()

let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
OpenFeatureAPI.shared.setProvider(provider: provider)

let cancellable = OpenFeatureAPI.shared.observe().sink { event in
if event == .ready {
readyExpectation.fulfill()
} else {
print(event)
print(event.debugDescription)
}
}

let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
OpenFeatureAPI.shared.setProvider(provider: provider)

await fulfillment(of: [readyExpectation], timeout: 5.0)
cancellable.cancel()
}
Expand Down Expand Up @@ -60,16 +61,19 @@ class ConfidenceProviderTest: XCTestCase {
.withStorage(storage: FakeStorage())
.build()

let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
OpenFeatureAPI.shared.setProvider(provider: provider)

let cancellable = OpenFeatureAPI.shared.observe().sink { event in
if event == .error {
errorExpectation.fulfill()
} else {
print(event)
if let event = event {
if case .error = event {
errorExpectation.fulfill()
} else {
// no-op
}
}
}

let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
OpenFeatureAPI.shared.setProvider(provider: provider)

await fulfillment(of: [errorExpectation], timeout: 5.0)
cancellable.cancel()
}
Expand Down Expand Up @@ -103,7 +107,7 @@ class ConfidenceProviderTest: XCTestCase {
if event == .ready {
readyExpectation.fulfill()
} else {
print(event)
print(event.debugDescription)
}
}
await fulfillment(of: [readyExpectation], timeout: 1.0)
Expand Down
6 changes: 3 additions & 3 deletions api/ConfidenceProvider_public_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
},
{
"name": "initialize(initialContext:)",
"declaration": "public func initialize(initialContext: OpenFeature.EvaluationContext?)"
"declaration": "public func initialize(initialContext: OpenFeature.EvaluationContext?) async throws"
},
{
"name": "onContextSet(oldContext:newContext:)",
"declaration": "public func onContextSet(\n oldContext: OpenFeature.EvaluationContext?,\n newContext: OpenFeature.EvaluationContext\n)"
"declaration": "public func onContextSet(\n oldContext: OpenFeature.EvaluationContext?,\n newContext: OpenFeature.EvaluationContext\n) async"
},
{
"name": "getBooleanEvaluation(key:defaultValue:context:)",
Expand All @@ -36,7 +36,7 @@
},
{
"name": "observe()",
"declaration": "public func observe() -> AnyPublisher<OpenFeature.ProviderEvent, Never>"
"declaration": "public func observe() -> AnyPublisher<OpenFeature.ProviderEvent?, Never>"
}
]
}
Expand Down
Loading