From d295ffa81cce2bb0ef611caea6661319541ac40c Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Fri, 7 Jun 2024 14:03:59 +0200 Subject: [PATCH] fix: targeting_key conversion (#136) * fix: track API can throw * fix: targeting key conversions --- .../ConfidenceDemoApp/ConfidenceDemoApp.swift | 2 +- .../ConfidenceTypeMapper.swift | 12 +++-- .../ConfidenceTypeMapperTest.swift | 44 +++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift b/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift index 93493d2a..11ea9e7b 100644 --- a/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift +++ b/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift @@ -44,7 +44,7 @@ struct ConfidenceDemoApp: App { extension ConfidenceDemoApp { func setup(confidence: Confidence) async throws { try await confidence.fetchAndActivate() - confidence.track( + try confidence.track( eventName: "all-types", data: [ "my_string": ConfidenceValue(string: "hello_from_world"), diff --git a/Sources/ConfidenceProvider/ConfidenceTypeMapper.swift b/Sources/ConfidenceProvider/ConfidenceTypeMapper.swift index 6eea092c..90b253e6 100644 --- a/Sources/ConfidenceProvider/ConfidenceTypeMapper.swift +++ b/Sources/ConfidenceProvider/ConfidenceTypeMapper.swift @@ -8,9 +8,15 @@ public enum ConfidenceTypeMapper { } static func from(ctx: EvaluationContext?) -> ConfidenceStruct { - var ctxMap = ctx?.asMap() ?? [:] - ctxMap["targeting_key"] = .string(ctx?.getTargetingKey() ?? "") - return ctxMap.compactMapValues(convertValue) + guard let openFeatureContext = ctx else { + return [:] + } + var ofCtxMap = openFeatureContext.asMap() + // Precendence given to the `attributes` rather then the bespoke `targeting_key` + if !openFeatureContext.getTargetingKey().isEmpty && !ofCtxMap.keys.contains("targeting_key") { + ofCtxMap["targeting_key"] = .string(openFeatureContext.getTargetingKey()) + } + return ofCtxMap.compactMapValues(convertValue) } // swiftlint:disable:next cyclomatic_complexity diff --git a/Tests/ConfidenceProviderTests/ConfidenceTypeMapperTest.swift b/Tests/ConfidenceProviderTests/ConfidenceTypeMapperTest.swift index 70b3397b..35988d71 100644 --- a/Tests/ConfidenceProviderTests/ConfidenceTypeMapperTest.swift +++ b/Tests/ConfidenceProviderTests/ConfidenceTypeMapperTest.swift @@ -18,6 +18,50 @@ class ValueConverterTest: XCTestCase { XCTAssertEqual(confidenceStruct, expected) } + func testContextConversionTargetingKey() throws { + let openFeatureCtx = MutableContext( + targetingKey: "", + structure: MutableStructure(attributes: (["targeting_key": .string("userid")]))) + let confidenceStruct = ConfidenceTypeMapper.from(ctx: openFeatureCtx) + let expected = [ + "targeting_key": ConfidenceValue(string: "userid") + ] + XCTAssertEqual(confidenceStruct, expected) + } + + func testContextConversionTargetingKeyPrecendence() throws { + let openFeatureCtx = MutableContext( + targetingKey: "userid-1", + structure: MutableStructure(attributes: (["targeting_key": .string("userid-2")]))) + let confidenceStruct = ConfidenceTypeMapper.from(ctx: openFeatureCtx) + let expected = [ + "targeting_key": ConfidenceValue(string: "userid-2") + ] + XCTAssertEqual(confidenceStruct, expected) + } + + func testContextConversionTargetingKeyPrecendence3() throws { + let openFeatureCtx = MutableContext( + targetingKey: "userid-1", + structure: MutableStructure(attributes: (["targeting_key": .string("")]))) + let confidenceStruct = ConfidenceTypeMapper.from(ctx: openFeatureCtx) + let expected = [ + "targeting_key": ConfidenceValue(string: "") + ] + XCTAssertEqual(confidenceStruct, expected) + } + + func testContextConversionTargetingKeyPrecendence2() throws { + let openFeatureCtx = MutableContext( + targetingKey: "userid-1", + structure: MutableStructure(attributes: ([:]))) + let confidenceStruct = ConfidenceTypeMapper.from(ctx: openFeatureCtx) + let expected = [ + "targeting_key": ConfidenceValue(string: "userid-1") + ] + XCTAssertEqual(confidenceStruct, expected) + } + func testContextConversionWithLists() throws { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"