Skip to content

Commit

Permalink
fix: wrong parsing datafile for segments if defined as pure string (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
polok authored Jan 9, 2024
1 parent 828e9a7 commit a47d430
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ extension KeyedDecodingContainer {
throw DecodingError.dataCorrupted(context)
}

guard
let _ = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)
else {
return .plain(stringifiedGroupSegment)
}

return try JSONDecoder().decode(GroupSegment.self, from: data)
}
}
Expand Down
35 changes: 32 additions & 3 deletions Tests/FeaturevisorTypesTests/FeaturevisorTypesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ final class FeaturevisorTypesTests: XCTestCase {
// THEN
XCTAssertEqual(result.revision, "0.0.13")
XCTAssertEqual(result.schemaVersion, "1")
XCTAssertEqual(result.attributes.count, 3)
XCTAssertEqual(result.features.count, 2)
XCTAssertEqual(result.segments.count, 1)
XCTAssertEqual(result.attributes.count, 4)
XCTAssertEqual(result.features.count, 3)
XCTAssertEqual(result.segments.count, 3)

let segment = result.segments[0]
XCTAssertEqual(segment.key, "myAccount")
Expand Down Expand Up @@ -199,6 +199,35 @@ final class FeaturevisorTypesTests: XCTestCase {
XCTAssertNil(traffic22.variables)
XCTAssertEqual(traffic22.allocation.count, 0)

let feature3 = result.features[2]
XCTAssertEqual(feature3.key, "f_safe_mode_gcp")
XCTAssertNil(feature3.deprecated)
XCTAssertEqual(feature3.bucketBy, .single("userId"))
XCTAssertEqual(feature3.ranges.count, 0)
XCTAssertEqual(feature3.variations.count, 0)
XCTAssertEqual(feature3.traffic.count, 2)

let traffic31 = feature3.traffic[0]
XCTAssertEqual(traffic31.key, "0")
XCTAssertNil(traffic31.enabled)
XCTAssertEqual(traffic31.percentage, 100000)
XCTAssertEqual(traffic31.variation, nil)
XCTAssertEqual(
traffic31.segments,
.multiple([.or(.init(or: [.plain("OsIOS"), .plain("OsAndroid"), .plain("OsTvOS")]))])
)
XCTAssertEqual(traffic31.variables?.count, 1)
XCTAssertEqual(traffic31.allocation.count, 0)

let traffic32 = feature3.traffic[1]
XCTAssertEqual(traffic32.key, "1")
XCTAssertNil(traffic32.enabled)
XCTAssertEqual(traffic32.percentage, 100000)
XCTAssertEqual(traffic32.variation, nil)
XCTAssertEqual(traffic32.segments, .plain("PlatformWeb"))
XCTAssertEqual(traffic32.variables?.count, 1)
XCTAssertEqual(traffic32.allocation.count, 0)

let attribute1 = result.attributes[0]
XCTAssertEqual(attribute1.key, "chapter")
XCTAssertNil(attribute1.archived)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@
"key": "userId",
"type": "string",
"capture": true
},
{
"key": "device_os",
"type": "string"
}
],
"segments": [
{
"key": "myAccount",
"conditions": "[{\"attribute\":\"chapter\",\"operator\":\"equals\",\"value\":\"account\"}]"
},
{
"key": "OsIOS",
"conditions": "[{\"attribute\":\"device_os\",\"operator\":\"in\",\"value\":[\"iOS\",\"iPadOS\"]}]"
},
{
"key": "OsTvOS",
"conditions": "[{\"attribute\":\"device_os\",\"operator\":\"equals\",\"value\":\"tvOS\"}]"
}
],
"features": [
Expand Down Expand Up @@ -160,6 +172,41 @@
]
}
]
},
{
"key": "f_safe_mode_gcp",
"bucketBy": "userId",
"traffic": [
{
"key": "0",
"segments": "[{\"or\":[\"OsIOS\",\"OsAndroid\",\"OsTvOS\"]}]",
"percentage": 100000,
"allocation": [

],
"variables": {
"version": 4
}
},
{
"key": "1",
"segments": "PlatformWeb",
"percentage": 100000,
"allocation": [

],
"variables": {
"version": 1
}
}
],
"variablesSchema": [
{
"key": "version",
"type": "integer",
"defaultValue": 1
}
]
}
]
}

0 comments on commit a47d430

Please sign in to comment.