Skip to content

Commit

Permalink
feat: create BKTValue.number to hold JSON number value
Browse files Browse the repository at this point in the history
  • Loading branch information
duyhungtnn committed Aug 21, 2024
1 parent 8d576f2 commit 58a6bb7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 72 deletions.
23 changes: 8 additions & 15 deletions Bucketeer/Sources/Public/BKTValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import Foundation
public enum BKTValue: Equatable, Codable, Hashable {
case boolean(Bool)
case string(String)
case integer(Int64)
case double(Double)
case number(Double)
case list([BKTValue])
case dictionary([String: BKTValue])
case null
Expand All @@ -16,10 +15,8 @@ public enum BKTValue: Equatable, Codable, Hashable {
let container = try decoder.singleValueContainer()
if let stringValue = try? container.decode(String.self) {
self = .string(stringValue)
} else if let intValue = try? container.decode(Int64.self) {
self = .integer(intValue)
} else if let doubleValue = try? container.decode(Double.self) {
self = .double(doubleValue)
self = .number(doubleValue)
} else if let boolValue = try? container.decode(Bool.self) {
self = .boolean(boolValue)
} else if let objectValue = try? container.decode([String: BKTValue].self) {
Expand All @@ -39,9 +36,7 @@ public enum BKTValue: Equatable, Codable, Hashable {
switch self {
case .string(let value):
try container.encode(value)
case .integer(let value):
try container.encode(value)
case .double(let value):
case .number(let value):
try container.encode(value)
case .dictionary(let value):
try container.encode(value)
Expand Down Expand Up @@ -70,16 +65,16 @@ public enum BKTValue: Equatable, Codable, Hashable {
return nil
}

public func asInteger() -> Int64? {
if case let .integer(int64) = self {
return int64
public func asInteger() -> Int? {
if case let .number(double) = self {
return Int(double)
}

return nil
}

public func asDouble() -> Double? {
if case let .double(double) = self {
if case let .number(double) = self {
return double
}

Expand Down Expand Up @@ -110,9 +105,7 @@ extension BKTValue: CustomStringConvertible {
return "\(value)"
case .string(let value):
return value
case .integer(let value):
return "\(value)"
case .double(let value):
case .number(let value):
return "\(value)"
case .list(value: let values):
return "\(values.map { value in value.description })"
Expand Down
14 changes: 7 additions & 7 deletions BucketeerTests/BKTClientEvaluationDetailTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ final class BKTClientEvaluationDetailTests: XCTestCase {
userId: expectedEvaluation.userId,
variationId: expectedEvaluation.variationId,
variationName: expectedEvaluation.variationName,
variationValue: .integer(1),
variationValue: .number(1),
reason: BKTEvaluationDetails.Reason.fromString(value: expectedEvaluation.reason.type.rawValue)
)
)
Expand Down Expand Up @@ -328,7 +328,7 @@ final class BKTClientEvaluationDetailTests: XCTestCase {
userId: expectedEvaluation.userId,
variationId: expectedEvaluation.variationId,
variationName: expectedEvaluation.variationName,
variationValue: .double(12.2),
variationValue: .number(12.2),
reason: BKTEvaluationDetails.Reason.fromString(value: expectedEvaluation.reason.type.rawValue)
)
)
Expand All @@ -349,20 +349,20 @@ final class BKTClientEvaluationDetailTests: XCTestCase {
[
"value": .string("body"),
"value1": .string("body1"),
"valueInt" : .integer(1),
"valueInt" : .number(1),
"valueBool" : .boolean(true),
"valueDouble" : .double(1.2),
"valueDouble" : .number(1.2),
"valueDictionary": .dictionary(["key" : .string("value")]),
"valueList1": .list(
[
.dictionary(["key" : .string("value")]),
.dictionary(["key" : .integer(10)])
.dictionary(["key" : .number(10)])
]
),
"valueList2": .list(
[
.integer(1),
.double(2.2),
.number(1),
.number(2.2),
.boolean(true)
]
)
Expand Down
70 changes: 35 additions & 35 deletions BucketeerTests/BKTValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class BKTValueEncodingTests: XCTestCase {
let testCases: [EncodeTestCase] = [
EncodeTestCase(value: .string("test string"), expectedJsonString: "\"test string\""),
EncodeTestCase(value: .string(""), expectedJsonString: "\"\""),
EncodeTestCase(value: .integer(123), expectedJsonString: "123"),
EncodeTestCase(value: .double(123.456), expectedJsonString: "123.456"),
EncodeTestCase(value: .double(123.0), expectedJsonString: "123"),
EncodeTestCase(value: .number(123), expectedJsonString: "123"),
EncodeTestCase(value: .number(123.456), expectedJsonString: "123.456"),
EncodeTestCase(value: .number(123.0), expectedJsonString: "123"),
EncodeTestCase(value: .boolean(true), expectedJsonString: "true"),
EncodeTestCase(value: .dictionary(["key": .string("value")]), expectedJsonString: "{\"key\":\"value\"}"),
EncodeTestCase(value: .list([.string("value1"), .string("value2")]), expectedJsonString: "[\"value1\",\"value2\"]"),
Expand All @@ -57,10 +57,10 @@ class BKTValueDecodeTests: XCTestCase {
let testCases: [DecodeTestCase] = [
DecodeTestCase(json: "\"test string\"", expected: .string("test string")),
DecodeTestCase(json: "null", expected: .null),
DecodeTestCase(json: "123", expected: .integer(123)),
DecodeTestCase(json: "123.456", expected: .double(123.456)),
DecodeTestCase(json: "123.0", expected: .integer(123)),
DecodeTestCase(json: "123.00", expected: .integer(123)),
DecodeTestCase(json: "123", expected: .number(123)),
DecodeTestCase(json: "123.456", expected: .number(123.456)),
DecodeTestCase(json: "123.0", expected: .number(123)),
DecodeTestCase(json: "123.00", expected: .number(123)),
DecodeTestCase(json: "true", expected: .boolean(true)),
DecodeTestCase(json: "false", expected: .boolean(false)),
DecodeTestCase(json: "\"true\"", expected: .string("true")),
Expand Down Expand Up @@ -91,8 +91,8 @@ final class BKTValueTests: XCTestCase {
func testAsBoolean() {
XCTAssertEqual(BKTValue.boolean(true).asBoolean(), true)
XCTAssertNil(BKTValue.string("string").asBoolean())
XCTAssertNil(BKTValue.integer(123).asBoolean())
XCTAssertNil(BKTValue.double(123.456).asBoolean())
XCTAssertNil(BKTValue.number(123).asBoolean())
XCTAssertNil(BKTValue.number(123.456).asBoolean())
XCTAssertNil(BKTValue.list([.boolean(true)]).asBoolean())
XCTAssertNil(BKTValue.dictionary(["key": .boolean(true)]).asBoolean())
XCTAssertNil(BKTValue.null.asBoolean())
Expand All @@ -101,39 +101,39 @@ final class BKTValueTests: XCTestCase {
func testAsString() {
XCTAssertEqual(BKTValue.string("test").asString(), "test")
XCTAssertNil(BKTValue.boolean(true).asString())
XCTAssertNil(BKTValue.integer(123).asString())
XCTAssertNil(BKTValue.double(123.456).asString())
XCTAssertNil(BKTValue.number(123).asString())
XCTAssertNil(BKTValue.number(123.456).asString())
XCTAssertNil(BKTValue.list([.string("value")]).asString())
XCTAssertNil(BKTValue.dictionary(["key": .string("value")]).asString())
XCTAssertNil(BKTValue.null.asString())
}

func testAsInteger() {
XCTAssertEqual(BKTValue.integer(123).asInteger(), 123)
XCTAssertEqual(BKTValue.number(123).asInteger(), 123)
XCTAssertNil(BKTValue.boolean(true).asInteger())
XCTAssertNil(BKTValue.string("string").asInteger())
XCTAssertNil(BKTValue.double(123.456).asInteger())
XCTAssertNil(BKTValue.list([.integer(123)]).asInteger())
XCTAssertNil(BKTValue.dictionary(["key": .integer(123)]).asInteger())
XCTAssertEqual(BKTValue.number(123.456).asInteger(), 123)
XCTAssertNil(BKTValue.list([.number(123)]).asInteger())
XCTAssertNil(BKTValue.dictionary(["key": .number(123)]).asInteger())
XCTAssertNil(BKTValue.null.asInteger())
}

func testAsDouble() {
XCTAssertEqual(BKTValue.double(123.456).asDouble(), 123.456)
XCTAssertEqual(BKTValue.number(123.456).asDouble(), 123.456)
XCTAssertNil(BKTValue.boolean(true).asDouble())
XCTAssertNil(BKTValue.string("string").asDouble())
XCTAssertNil(BKTValue.integer(123).asDouble())
XCTAssertNil(BKTValue.list([.double(123.456)]).asDouble())
XCTAssertNil(BKTValue.dictionary(["key": .double(123.456)]).asDouble())
XCTAssertEqual(BKTValue.number(123).asDouble(), 123)
XCTAssertNil(BKTValue.list([.number(123.456)]).asDouble())
XCTAssertNil(BKTValue.dictionary(["key": .number(123.456)]).asDouble())
XCTAssertNil(BKTValue.null.asDouble())
}

func testAsList() {
XCTAssertEqual(BKTValue.list([.string("value")]).asList(), [.string("value")])
XCTAssertNil(BKTValue.boolean(true).asList())
XCTAssertNil(BKTValue.string("string").asList())
XCTAssertNil(BKTValue.integer(123).asList())
XCTAssertNil(BKTValue.double(123.456).asList())
XCTAssertNil(BKTValue.number(123).asList())
XCTAssertNil(BKTValue.number(123.456).asList())
XCTAssertNil(BKTValue.dictionary(["key": .list([.string("value")])]).asList())
XCTAssertNil(BKTValue.null.asList())
}
Expand All @@ -142,8 +142,8 @@ final class BKTValueTests: XCTestCase {
XCTAssertEqual(BKTValue.dictionary(["key": .string("value")]).asDictionary(), ["key": .string("value")])
XCTAssertNil(BKTValue.boolean(true).asDictionary())
XCTAssertNil(BKTValue.string("string").asDictionary())
XCTAssertNil(BKTValue.integer(123).asDictionary())
XCTAssertNil(BKTValue.double(123.456).asDictionary())
XCTAssertNil(BKTValue.number(123).asDictionary())
XCTAssertNil(BKTValue.number(123.456).asDictionary())
XCTAssertNil(BKTValue.list([.dictionary(["key": .string("value")])]).asDictionary())
XCTAssertNil(BKTValue.null.asDictionary())
}
Expand All @@ -156,10 +156,10 @@ final class BKTValueTests: XCTestCase {
XCTAssertEqual("\"test value\"".getVariationBKTValue(logger: nil), .string("test value"))
XCTAssertEqual("true".getVariationBKTValue(logger: nil), .boolean(true))
XCTAssertEqual("false".getVariationBKTValue(logger: nil), .boolean(false))
XCTAssertEqual("1".getVariationBKTValue(logger: nil), .integer(1))
XCTAssertEqual("1.0".getVariationBKTValue(logger: nil), .integer(1))
XCTAssertEqual("1.2".getVariationBKTValue(logger: nil), .double(1.2))
XCTAssertEqual("1.234".getVariationBKTValue(logger: nil), .double(1.234))
XCTAssertEqual("1".getVariationBKTValue(logger: nil), .number(1))
XCTAssertEqual("1.0".getVariationBKTValue(logger: nil), .number(1))
XCTAssertEqual("1.2".getVariationBKTValue(logger: nil), .number(1.2))
XCTAssertEqual("1.234".getVariationBKTValue(logger: nil), .number(1.234))

let dictionaryJSONText = """
{
Expand All @@ -179,20 +179,20 @@ final class BKTValueTests: XCTestCase {
[
"value": .string("body"),
"value1": .string("body1"),
"valueInt" : .integer(1),
"valueInt" : .number(1),
"valueBool" : .boolean(true),
"valueDouble" : .double(1.2),
"valueDouble" : .number(1.2),
"valueDictionary": .dictionary(["key" : .string("value")]),
"valueList1": .list(
[
.dictionary(["key" : .string("value")]),
.dictionary(["key" : .integer(10)])
.dictionary(["key" : .number(10)])
]
),
"valueList2": .list(
[
.integer(1),
.double(2.2),
.number(1),
.number(2.2),
.boolean(true)
]
)
Expand All @@ -211,7 +211,7 @@ final class BKTValueTests: XCTestCase {
.list(
[
.dictionary(["key" : .string("value")]),
.dictionary(["key" : .integer(10)])
.dictionary(["key" : .number(10)])
]
)
)
Expand All @@ -223,8 +223,8 @@ final class BKTValueTests: XCTestCase {
listJSON2Text.getVariationBKTValue(logger: nil),
.list(
[
.integer(1),
.double(2.2),
.number(1),
.number(2.2),
.boolean(true)
]
)
Expand Down
6 changes: 3 additions & 3 deletions BucketeerTests/E2E/E2EBKTClientForceUpdateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ final class E2EBKTClientForceUpdateTests: XCTestCase {
XCTAssertEqual(androidEvaluationDetail.featureId, "feature-android-e2e-string")
XCTAssert(androidEvaluationDetail.variationValue.count > 0)

let androidObjectEvaluationDetail = client.objectVariationDetails(featureId: "feature-android-e2e-string", defaultValue: .integer(10))
let androidObjectEvaluationDetail = client.objectVariationDetails(featureId: "feature-android-e2e-string", defaultValue: .number(10))
XCTAssertNotNil(androidObjectEvaluationDetail)
XCTAssertEqual(androidObjectEvaluationDetail.featureId, "feature-android-e2e-string")
XCTAssert(androidObjectEvaluationDetail.variationValue.asString()?.count ?? 0 > 0)
Expand All @@ -189,7 +189,7 @@ final class E2EBKTClientForceUpdateTests: XCTestCase {
XCTAssertEqual(golangEvaluationDetail.featureId, "feature-go-server-e2e-1")
XCTAssert(golangEvaluationDetail.variationValue.count > 0)

let golangObjectEvaluationDetail = client.objectVariationDetails(featureId: "feature-go-server-e2e-1", defaultValue: .integer(10))
let golangObjectEvaluationDetail = client.objectVariationDetails(featureId: "feature-go-server-e2e-1", defaultValue: .number(10))
XCTAssertNotNil(golangObjectEvaluationDetail)
XCTAssertEqual(golangObjectEvaluationDetail.featureId, "feature-go-server-e2e-1")
XCTAssert(golangObjectEvaluationDetail.variationValue.asString()?.count ?? 0 > 0)
Expand All @@ -200,7 +200,7 @@ final class E2EBKTClientForceUpdateTests: XCTestCase {
XCTAssertEqual(javascriptEvaluationDetail.featureId, "feature-js-e2e-string")
XCTAssert(javascriptEvaluationDetail.variationValue.count > 0)

let javascriptObjectEvaluationDetail = client.objectVariationDetails(featureId: "feature-js-e2e-string", defaultValue: .integer(10))
let javascriptObjectEvaluationDetail = client.objectVariationDetails(featureId: "feature-js-e2e-string", defaultValue: .number(10))
XCTAssertNotNil(javascriptObjectEvaluationDetail)
XCTAssertEqual(javascriptObjectEvaluationDetail.featureId, "feature-js-e2e-string")
XCTAssert(javascriptObjectEvaluationDetail.variationValue.asString()?.count ?? 0 > 0)
Expand Down
10 changes: 5 additions & 5 deletions BucketeerTests/E2E/E2EEvaluationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ final class E2EEvaluationTests: XCTestCase {
))

XCTAssertEqual(
client.objectVariationDetails(featureId: FEATURE_ID_INT, defaultValue: .integer(1)),
client.objectVariationDetails(featureId: FEATURE_ID_INT, defaultValue: .number(1)),
.init(
featureId: FEATURE_ID_INT,
featureVersion: 4,
userId: USER_ID,
variationId: "9c5fd2d2-d587-4ba2-8de2-0fc9454d564e",
variationName: "variation 10",
variationValue: .integer(10),
variationValue: .number(10),
reason: .default
))
} catch {
Expand Down Expand Up @@ -182,14 +182,14 @@ final class E2EEvaluationTests: XCTestCase {
))

XCTAssertEqual(
client.objectVariationDetails(featureId: FEATURE_ID_DOUBLE, defaultValue: .double(1.1)),
client.objectVariationDetails(featureId: FEATURE_ID_DOUBLE, defaultValue: .number(1.1)),
.init(
featureId: FEATURE_ID_DOUBLE,
featureVersion: 3,
userId: USER_ID,
variationId: "38078d8f-c6eb-4b93-9d58-c3e57010983f",
variationName: "variation 2.1",
variationValue: .double(2.1),
variationValue: .number(2.1),
reason: .default
))
} catch {
Expand Down Expand Up @@ -310,7 +310,7 @@ final class E2EEvaluationTests: XCTestCase {
))

XCTAssertEqual(
client.objectVariationDetails(featureId: FEATURE_ID_STRING, defaultValue: .double(1.0)),
client.objectVariationDetails(featureId: FEATURE_ID_STRING, defaultValue: .number(1.0)),
.init(
featureId: FEATURE_ID_STRING,
featureVersion: 3,
Expand Down
14 changes: 7 additions & 7 deletions BucketeerTests/EvaluationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ final class EvaluationTests: XCTestCase {
XCTAssertEqual(stringValue, .string("a123"))
// "123" in JSON is a integer 123
let intValue: BKTValue? = mockEvaluation(value: "123").getVariationValue(logger: nil)
XCTAssertEqual(intValue, .integer(123))
XCTAssertEqual(intValue, .number(123))
let doubleValue: BKTValue? = mockEvaluation(value: "1.2").getVariationValue(logger: nil)
XCTAssertEqual(doubleValue, .double(1.2))
XCTAssertEqual(doubleValue, .number(1.2))

let objectValue: BKTValue = Evaluation.jsonObjectEvaluation.getVariationValue(defaultValue: .boolean(false), logger: nil)
XCTAssertEqual(
Expand All @@ -78,20 +78,20 @@ final class EvaluationTests: XCTestCase {
[
"value": .string("body"),
"value1": .string("body1"),
"valueInt" : .integer(1),
"valueInt" : .number(1),
"valueBool" : .boolean(true),
"valueDouble" : .double(1.2),
"valueDouble" : .number(1.2),
"valueDictionary": .dictionary(["key" : .string("value")]),
"valueList1": .list(
[
.dictionary(["key" : .string("value")]),
.dictionary(["key" : .integer(10)])
.dictionary(["key" : .number(10)])
]
),
"valueList2": .list(
[
.integer(1),
.double(2.2),
.number(1),
.number(2.2),
.boolean(true)
]
)
Expand Down

0 comments on commit 58a6bb7

Please sign in to comment.