Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong parsing datafile for segments if defined as pure string #61

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}

Comment on lines +35 to +40
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are the fix

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
}
]
}
]
}