Skip to content

Commit

Permalink
fix: handle Int32 and Int64 in defaultValue evaluations (#162)
Browse files Browse the repository at this point in the history
* test: write failing test for Integer issue

* fix: handle Int32 and Int64 in defaultValue evaluations

* fixup! test: write failing test for Integer issue
  • Loading branch information
nicklasl authored Jul 10, 2024
1 parent 297658b commit 6bb03d5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Sources/Confidence/FlagEvaluation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ extension FlagResolution {
}
}

// swiftlint:disable:next cyclomatic_complexity
private func getTyped<T>(value: ConfidenceValue) -> T? {
if let value = self as? T {
return value
Expand All @@ -97,7 +98,16 @@ extension FlagResolution {
case .string:
return value.asString() as? T
case .integer:
return value.asInteger() as? T
if let intValue = value.asInteger() as? T {
return intValue
}
if T.self == Int32.self, let intValue = value.asInteger() {
return Int32(intValue) as? T
}
if T.self == Int64.self, let intValue = value.asInteger() {
return Int64(intValue) as? T
}
return nil
case .double:
return value.asDouble() as? T
case .date:
Expand Down
38 changes: 38 additions & 0 deletions Tests/ConfidenceTests/ConfidenceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,44 @@ class ConfidenceTest: XCTestCase {
}


func testResolveIntegerFlagWithInt64() async throws {
class FakeClient: ConfidenceResolveClient {
var resolveStats: Int = 0
var resolvedValues: [ResolvedValue] = []
func resolve(ctx: ConfidenceStruct) async throws -> ResolvesResult {
self.resolveStats += 1
return .init(resolvedValues: resolvedValues, resolveToken: "token")
}
}

let client = FakeClient()
client.resolvedValues = [
ResolvedValue(
variant: "control",
value: .init(structure: ["size": .init(integer: 3)]),
flag: "flag",
resolveReason: .match)
]

let confidence = Confidence.Builder(clientSecret: "test")
.withContext(initialContext: ["targeting_key": .init(string: "user2")])
.withFlagResolverClient(flagResolver: client)
.withFlagApplier(flagApplier: flagApplier)
.build()

try await confidence.fetchAndActivate()
let value = try confidence.getValue(
key: "flag.size",
defaultValue: 0 as Int64)

XCTAssertEqual(client.resolveStats, 1)
XCTAssertEqual(value, 3)
XCTAssertEqual(client.resolveStats, 1)
await fulfillment(of: [flagApplier.applyExpectation], timeout: 1)
XCTAssertEqual(flagApplier.applyCallCount, 1)
}


func testResolveAndApplyIntegerFlagNoSegmentMatch() async throws {
class FakeClient: ConfidenceResolveClient {
var resolveStats: Int = 0
Expand Down

0 comments on commit 6bb03d5

Please sign in to comment.