Skip to content

Commit

Permalink
fix: Provider still works with OF ctx nil (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria authored May 3, 2024
1 parent e78344d commit 4d58327
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 50 deletions.
14 changes: 3 additions & 11 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,13 @@ public class ConfidenceFeatureProvider: FeatureProvider {
}

public func initialize(initialContext: OpenFeature.EvaluationContext?) {
guard let initialContext = initialContext else {
return
}
let convertedInitialContext = ConfidenceTypeMapper.from(ctx: initialContext)
confidence?.putContext(context: convertedInitialContext)
let context = confidence?.getContext() ?? convertedInitialContext

confidence?.putContext(context: ConfidenceTypeMapper.from(ctx: initialContext))
if self.initializationStrategy == .activateAndFetchAsync {
eventHandler.send(.ready)
}

let context = confidence?.getContext() ?? ConfidenceTypeMapper.from(ctx: initialContext)

Task {
await resolve(strategy: initializationStrategy, context: context)
}
Expand Down Expand Up @@ -278,10 +274,6 @@ public class ConfidenceFeatureProvider: FeatureProvider {
reason: Reason.staticReason.rawValue)
}

guard let ctx = ctx else {
throw OpenFeatureError.invalidContextError
}

let context = confidence?.getContext() ?? ConfidenceTypeMapper.from(ctx: ctx)

do {
Expand Down
6 changes: 3 additions & 3 deletions Sources/ConfidenceProvider/Utils/ConfidenceTypeMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public enum ConfidenceTypeMapper {
return convertValue(value)
}

static func from(ctx: EvaluationContext) -> ConfidenceStruct {
var ctxMap = ctx.asMap()
ctxMap["targeting_key"] = .string(ctx.getTargetingKey())
static func from(ctx: EvaluationContext?) -> ConfidenceStruct {
var ctxMap = ctx?.asMap() ?? [:]
ctxMap["targeting_key"] = .string(ctx?.getTargetingKey() ?? "")
return ctxMap.compactMapValues(convertValue)
}

Expand Down
36 changes: 0 additions & 36 deletions Tests/ConfidenceProviderTests/ConfidenceFeatureProviderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -708,42 +708,6 @@ class ConfidenceFeatureProviderTest: XCTestCase {
}
}

func testProviderNoTargetingKey() throws {
let resolve: [String: MockedResolveClientURLProtocol.ResolvedTestFlag] = [
"user1": .init(variant: "control", value: .structure(["size": .null]))
]

let schemas: [String: StructFlagSchema] = [
"user1": .init(schema: ["size": .intSchema])
]

let flags: [String: MockedResolveClientURLProtocol.TestFlag] = [
"flags/flag": .init(resolve: resolve, schemas: schemas)
]

let session = MockedResolveClientURLProtocol.mockedSession(flags: flags)
let provider =
builder
.with(session: session)
.with(flagApplier: flagApplier)
.build()

// Note no context has been set via initialize or onContextSet
XCTAssertThrowsError(
try provider.getIntegerEvaluation(
key: "flag.size",
defaultValue: 3,
context: nil)
) { error in
XCTAssertEqual(
error as? OpenFeatureError, OpenFeatureError.invalidContextError)
}

// TODO: Check this - how do we check for something not called?
XCTAssertEqual(MockedResolveClientURLProtocol.resolveStats, 0)
XCTAssertEqual(flagApplier.applyCallCount, 0)
}

func testProviderTargetingKeyError() throws {
let resolve: [String: MockedResolveClientURLProtocol.ResolvedTestFlag] = [
"user1": .init(variant: "control", value: .structure(["size": .integer(3)]))
Expand Down

0 comments on commit 4d58327

Please sign in to comment.