Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle Int32 and Int64 in defaultValue evaluations #162

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading