Skip to content

Commit 8d620a6

Browse files
fix errors on FSC that are valid errors (#354)
1 parent 8fc101c commit 8d620a6

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

Sources/Data Model/Audience/SemanticVersion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ extension SemanticVersion {
8484
}
8585
// Expect a version string of the form x.y.z
8686
let dotCount = targetPrefix.filter({$0 == "."}).count
87-
if dotCount > 3 {
87+
if dotCount > 2 {
8888
throw OptimizelyError.attributeFormatInvalid
8989
}
9090
var targetedVersionParts = targetPrefix.split(separator: ".")

Sources/Data Model/Audience/UserAttribute.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,31 +157,29 @@ extension UserAttribute {
157157
return try value!.isLessOrEqual(than: rawAttributeValue!, condition: stringRepresentation, name: nameFinal)
158158
// semantic versioning seems unique. the comarison is to compare verion but the passed in version is the target version.
159159
case .semver_eq:
160-
guard let targetValue = AttributeValue(value: rawAttributeValue) else {
161-
throw OptimizelyError.evaluateAttributeInvalidCondition("attribute value \(nameFinal) invalid type")
162-
}
160+
let targetValue = try targetAsAttributeValue(value: rawAttributeValue, attribute: value, nameFinal: nameFinal)
163161
return try targetValue.isSemanticVersionEqual(than: value!.stringValue)
164162
case .semver_lt:
165-
guard let targetValue = AttributeValue(value: rawAttributeValue) else {
166-
throw OptimizelyError.evaluateAttributeInvalidCondition("attribute value \(nameFinal) invalid type")
167-
}
163+
let targetValue = try targetAsAttributeValue(value: rawAttributeValue, attribute: value, nameFinal: nameFinal)
168164
return try targetValue.isSemanticVersionLess(than: value!.stringValue)
169165
case .semver_le:
170-
guard let targetValue = AttributeValue(value: rawAttributeValue) else {
171-
throw OptimizelyError.evaluateAttributeInvalidCondition("attribute value \(nameFinal) invalid type")
172-
}
166+
let targetValue = try targetAsAttributeValue(value: rawAttributeValue, attribute: value, nameFinal: nameFinal)
173167
return try targetValue.isSemanticVersionLessOrEqual(than: value!.stringValue)
174168
case .semver_gt:
175-
guard let targetValue = AttributeValue(value: rawAttributeValue) else {
176-
throw OptimizelyError.evaluateAttributeInvalidCondition("attribute value \(nameFinal) invalid type")
177-
}
169+
let targetValue = try targetAsAttributeValue(value: rawAttributeValue, attribute: value, nameFinal: nameFinal)
178170
return try targetValue.isSemanticVersionGreater(than: value!.stringValue)
179171
case .semver_ge:
180-
guard let targetValue = AttributeValue(value: rawAttributeValue) else {
181-
throw OptimizelyError.evaluateAttributeInvalidCondition("attribute value \(nameFinal) invalid type")
182-
}
172+
let targetValue = try targetAsAttributeValue(value: rawAttributeValue, attribute: value, nameFinal: nameFinal)
183173
return try targetValue.isSemanticVersionGreaterOrEqual(than: value!.stringValue)
184174
}
185175
}
186176

177+
private func targetAsAttributeValue(value:Any?, attribute:AttributeValue?, nameFinal:String) throws -> AttributeValue {
178+
guard let targetValue = AttributeValue(value: value), targetValue.isComparable(with: attribute!) else {
179+
throw OptimizelyError.evaluateAttributeInvalidCondition("attribute value \(nameFinal) invalid type")
180+
}
181+
182+
return targetValue
183+
}
184+
187185
}

Tests/OptimizelyTests-DataModel/SemanticVersionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class SemanticVersionTests: XCTestCase {
165165

166166
func testInvalidAttributes() {
167167
let target = "2.1.0"
168-
let versions = ["-", ".", "..", "+", "+test", " ", "2 .3. 0", "2.", ".2.2"]
168+
let versions = ["-", ".", "..", "+", "+test", " ", "2 .3. 0", "2.", ".2.2", "3.7.2.2"]
169169
for (_, version) in versions.enumerated() {
170170
XCTAssert(((try? (version.compareVersion(targetedVersion: target)) < 0) == nil))
171171
}

Tests/OptimizelyTests-DataModel/UserAttributeTests_Evaluate.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,13 @@ extension UserAttributeTests_Evaluate {
450450
XCTAssertFalse(try! model.evaluate(attributes: attributes))
451451
}
452452

453+
func testEqualSemanticVersionInvalidType() {
454+
let model = UserAttribute(name: "version", type: "custom_attribute", match: "semver_eq", value: .string("2.0"))
455+
456+
var attributes:[String: Any] = ["version": true]
457+
XCTAssertNil(try? model.evaluate(attributes: attributes))
458+
attributes["version"] = 37
459+
XCTAssertNil(try? model.evaluate(attributes: attributes))
460+
}
461+
453462
}

0 commit comments

Comments
 (0)