Skip to content

Commit

Permalink
feat: Force correct init for Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Jun 18, 2024
1 parent eeda236 commit 096a07b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
25 changes: 22 additions & 3 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,25 @@ public class ConfidenceFeatureProvider: FeatureProvider {
/**
Creates the `Confidence` object to be used as init parameter for this Provider.
*/
public static func createConfidence(clientSecret: String) -> Confidence {
return Confidence.Builder.init(clientSecret: clientSecret)
public static func createConfidence(clientSecret: String) -> ConfidenceForOpenFeature {
return ConfidenceForOpenFeature.init(confidence: Confidence.Builder.init(clientSecret: clientSecret)
.withRegion(region: .global)
.withMetadata(metadata: ConfidenceMetadata.init(
name: providerId,
version: "0.2.1") // x-release-please-version
)
.build()
.build())
}

/**
Proxy holder to ensure correct Confidence configuration passed into the Provider's init.
Do not instantiate directly.
*/
public class ConfidenceForOpenFeature {
internal init(confidence: Confidence) {
self.confidence = confidence
}
let confidence: Confidence
}

/**
Expand All @@ -41,6 +52,14 @@ public class ConfidenceFeatureProvider: FeatureProvider {
rather then be instantiated directly via `Confidence.init(...)` as you would if not using the OpenFeature integration.
*/
public convenience init(
confidenceForOF: ConfidenceForOpenFeature,
initializationStrategy: InitializationStrategy = .fetchAndActivate
) {
self.init(confidence: confidenceForOF.confidence, session: nil)
}

// Allows to pass a confidence object with injected configurations for testing
internal convenience init(
confidence: Confidence,
initializationStrategy: InitializationStrategy = .fetchAndActivate
) {
Expand Down
6 changes: 3 additions & 3 deletions Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Foundation
import ConfidenceProvider
import Combine
import OpenFeature
import XCTest

@testable import Confidence
@testable import ConfidenceProvider

class ConfidenceProviderTest: XCTestCase {
private var readyExpectation = XCTestExpectation(description: "Ready")
Expand Down Expand Up @@ -84,11 +84,11 @@ class ConfidenceProviderTest: XCTestCase {

let confidenceForOpenFeature = ConfidenceFeatureProvider.createConfidence(clientSecret: "testSecret")
let provider = ConfidenceFeatureProvider(
confidence: confidenceForOpenFeature,
confidenceForOF: confidenceForOpenFeature,
initializationStrategy: .activateAndFetchAsync
)
XCTAssertEqual("SDK_ID_SWIFT_PROVIDER", provider.metadata.name)
let remoteClient = confidenceForOpenFeature.remoteFlagResolver as? RemoteConfidenceResolveClient
let remoteClient = confidenceForOpenFeature.confidence.remoteFlagResolver as? RemoteConfidenceResolveClient
XCTAssertEqual("SDK_ID_SWIFT_PROVIDER", remoteClient?.metadata.name)
XCTAssertNotEqual("", remoteClient?.metadata.version)
}
Expand Down

0 comments on commit 096a07b

Please sign in to comment.