From 4d583271fcd3e3655ca60036724f2e500ce783fc Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Fri, 3 May 2024 08:58:30 +0200 Subject: [PATCH] fix: Provider still works with OF ctx nil (#115) --- .../ConfidenceFeatureProvider.swift | 14 ++------ .../Utils/ConfidenceTypeMapper.swift | 6 ++-- .../ConfidenceFeatureProviderTest.swift | 36 ------------------- 3 files changed, 6 insertions(+), 50 deletions(-) diff --git a/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift b/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift index e644f70b..0ee45dde 100644 --- a/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift +++ b/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift @@ -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) } @@ -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 { diff --git a/Sources/ConfidenceProvider/Utils/ConfidenceTypeMapper.swift b/Sources/ConfidenceProvider/Utils/ConfidenceTypeMapper.swift index 6f1d0edf..b8818a4f 100644 --- a/Sources/ConfidenceProvider/Utils/ConfidenceTypeMapper.swift +++ b/Sources/ConfidenceProvider/Utils/ConfidenceTypeMapper.swift @@ -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) } diff --git a/Tests/ConfidenceProviderTests/ConfidenceFeatureProviderTest.swift b/Tests/ConfidenceProviderTests/ConfidenceFeatureProviderTest.swift index 055fc078..045aa92a 100644 --- a/Tests/ConfidenceProviderTests/ConfidenceFeatureProviderTest.swift +++ b/Tests/ConfidenceProviderTests/ConfidenceFeatureProviderTest.swift @@ -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)]))