Skip to content

Commit

Permalink
fix: targeting_key conversion (#136)
Browse files Browse the repository at this point in the history
* fix: track API can throw

* fix: targeting key conversions
  • Loading branch information
fabriziodemaria authored Jun 7, 2024
1 parent da06d45 commit d295ffa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
12 changes: 9 additions & 3 deletions Sources/ConfidenceProvider/ConfidenceTypeMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions Tests/ConfidenceProviderTests/ConfidenceTypeMapperTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d295ffa

Please sign in to comment.