-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0656931
commit 4facf6f
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Sources/ConfidenceProvider/Utils/ConfidenceTypeMapper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
import Confidence | ||
import OpenFeature | ||
|
||
public enum ConfidenceTypeMapper { | ||
static func from(value: Value) -> ConfidenceValue { | ||
return convertValue(value) | ||
} | ||
|
||
static func from(ctx: EvaluationContext) -> ConfidenceStruct { | ||
var ctxMap = ctx.asMap() | ||
ctxMap["targeting_key"] = .string(ctx.getTargetingKey()) | ||
return ctxMap.compactMapValues(convertValue) | ||
} | ||
|
||
static private func convertValue(_ value: Value) -> ConfidenceValue { | ||
switch value { | ||
case .boolean(let value): | ||
return .boolean(value) | ||
case .string(let value): | ||
return .string(value) | ||
case .integer(let value): | ||
return .integer(value) | ||
case .double(let value): | ||
return .double(value) | ||
case .date(let value): | ||
return .timestamp(value) | ||
case .list(let values): | ||
return .list(values.compactMap(convertValue)) | ||
case .structure(let values): | ||
return .structure(values.compactMapValues(convertValue)) | ||
case .null: | ||
return .null | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
Tests/ConfidenceProviderTests/ConfidenceTypeMapperTest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Foundation | ||
import Confidence | ||
import OpenFeature | ||
import XCTest | ||
|
||
@testable import ConfidenceProvider | ||
|
||
class ValueConverterTest: XCTestCase { | ||
func testContextConversion() throws { | ||
let openFeatureCtx = MutableContext( | ||
targetingKey: "userid", | ||
structure: MutableStructure(attributes: (["key": .string("value")]))) | ||
let confidenceStruct = ConfidenceTypeMapper.from(ctx: openFeatureCtx) | ||
let expected = [ | ||
"key": ConfidenceValue.string("value"), | ||
"targeting_key": ConfidenceValue.string("userid") | ||
] | ||
XCTAssertEqual(confidenceStruct, expected) | ||
} | ||
|
||
func testValueConversion() throws { | ||
let formatter = DateFormatter() | ||
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" | ||
let date = try XCTUnwrap(formatter.date(from: "2022-01-01 12:00:00")) | ||
|
||
let openFeatureValue = Value.structure([ | ||
"key": .string("value"), | ||
"null": .null, | ||
"bool": .boolean(true), | ||
"int": .integer(3), | ||
"double": .double(4.5), | ||
"date": .date(date), | ||
"list": .list([.integer(3), .integer(5)]), | ||
"structure": .structure(["field1": .string("test"), "field2": .integer(12)]), | ||
]) | ||
|
||
let confidenceValue = ConfidenceTypeMapper.from(value: openFeatureValue) | ||
let expected = ConfidenceValue.structure([ | ||
"key": .string("value"), | ||
"null": .null, | ||
"bool": .boolean(true), | ||
"int": .integer(3), | ||
"double": .double(4.5), | ||
"date": .timestamp(date), | ||
"list": .list([.integer(3), .integer(5)]), | ||
"structure": .structure(["field1": .string("test"), "field2": .integer(12)]), | ||
]) | ||
XCTAssertEqual(confidenceValue, expected) | ||
} | ||
} |