Skip to content

Commit 61d905b

Browse files
committed
Recommended changes made.
1 parent ad8c193 commit 61d905b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Sources/Data Model/FeatureVariable.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct FeatureVariable: Codable, Equatable {
2121
var key: String
2222
var type: String
2323
var subType: String?
24-
// datafile schema rquires this, but test has "null" value case. keep optional for FSC
24+
// datafile schema requires this, but test has "null" value case. keep optional for FSC
2525
var defaultValue: String?
2626

2727
enum CodingKeys: String, CodingKey {
@@ -38,6 +38,7 @@ struct FeatureVariable: Codable, Equatable {
3838
self.type = type
3939
self.subType = subType
4040
self.defaultValue = defaultValue
41+
overrideTypeIfJSON()
4142
}
4243

4344
init(from decoder: Decoder) throws {
@@ -47,7 +48,10 @@ struct FeatureVariable: Codable, Equatable {
4748
type = try container.decode(String.self, forKey: .type)
4849
subType = try container.decodeIfPresent(String.self, forKey: .subType)
4950
defaultValue = try container.decodeIfPresent(String.self, forKey: .defaultValue)
50-
51+
overrideTypeIfJSON()
52+
}
53+
54+
mutating func overrideTypeIfJSON() {
5155
if type == "string" && subType == "json" {
5256
type = "json"
5357
}

Tests/OptimizelyTests-DataModel/FeatureVariableTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ class FeatureVariableTests: XCTestCase {
4545
XCTAssert(model.subType == "json")
4646
}
4747

48+
func testInitWithJsonSubtypeOverridesType() {
49+
let model = FeatureVariable(id: "553339214", key: "price", type: "string", subType: "json", defaultValue: "{}")
50+
51+
XCTAssert(model.id == "553339214")
52+
XCTAssert(model.key == "price")
53+
XCTAssert(model.type == "json")
54+
XCTAssert(model.defaultValue == "{}")
55+
XCTAssert(model.subType == "json")
56+
}
57+
4858
func testDecodeSuccessWithExtraFields() {
4959
let data = ["id": "553339214", "key": "price", "type": "integer", "defaultValue": "100", "extra": "123"]
5060
let model: FeatureVariable = try! OTUtils.model(from: data)

0 commit comments

Comments
 (0)