Skip to content

Commit

Permalink
fix: evaluate variables overridden from rules with no variations (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
polok authored Oct 26, 2023
1 parent 90a5ebf commit 1609228
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 18 deletions.
35 changes: 19 additions & 16 deletions Sources/FeaturevisorSDK/Instance+Feature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,29 @@ extension FeaturevisorInstance {
logger: Logger
) -> MatchedTrafficAndAllocation {

var matchedAllocation: Allocation?

let matchedTraffic = traffic.first(where: { traffic in
if !allGroupSegmentsAreMatched(
groupSegments: traffic.segments,
context: context,
datafileReader: datafileReader
) {
return false
}

matchedAllocation = getMatchedAllocation(traffic: traffic, bucketValue: bucketValue)

return matchedAllocation != nil
})
guard
let matchedTraffic = traffic.first(where: { traffic in
return allGroupSegmentsAreMatched(
groupSegments: traffic.segments,
context: context,
datafileReader: datafileReader
)
})
else {
return (
matchedTraffic: nil,
matchedAllocation: nil
)
}

let matchedAllocation = getMatchedAllocation(
traffic: matchedTraffic,
bucketValue: bucketValue
)

return (
matchedTraffic: matchedTraffic,
matchedAllocation: matchedAllocation
)

}
}
9 changes: 7 additions & 2 deletions Sources/FeaturevisorTypes/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ public struct Attribute: Decodable {
public let archived: Bool? // only available in YAML
public let capture: Bool?

public init(key: AttributeKey, type: String, archived: Bool?, capture: Bool?) {
public init(
key: AttributeKey,
type: String,
archived: Bool? = nil,
capture: Bool? = nil
) {
self.key = key
self.type = type
self.archived = archived
Expand Down Expand Up @@ -184,7 +189,7 @@ public struct Segment: Codable {
public init(
key: SegmentKey,
conditions: Condition,
archived: Bool?
archived: Bool? = nil
) {
self.key = key
self.conditions = conditions
Expand Down
65 changes: 65 additions & 0 deletions Tests/FeaturevisorSDKTests/InstanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1171,4 +1171,69 @@ class FeaturevisorInstanceTests: XCTestCase {
// THEN
XCTAssertTrue(wasDatafileContentFetchErrorThrown)
}

func testShouldGetVariablesWithoutAnyVariations() {

// GIVEN
var options: InstanceOptions = .default
options.datafile = DatafileContent(
schemaVersion: "1",
revision: "1.0",
attributes: [
.init(key: "userId", type: "string", capture: true),
.init(key: "country", type: "string"),
],
segments: [
.init(
key: "netherlands",
conditions: .plain(
.init(attribute: "country", operator: .equals, value: .string("nl"))
)
)
],
features: [
.init(
key: "test",
bucketBy: .single("userId"),
variablesSchema: [
.init(key: "color", type: .string, defaultValue: .string("red"))
],
traffic: [
.init(
key: "1",
segments: .plain("netherlands"),
percentage: 100000,
allocation: [],
variables: ["color": .string("orange")]
),
.init(
key: "2",
segments: .plain("*"),
percentage: 100000,
allocation: []
),
]
)
]
)

let sdk = try! createInstance(options: options)

// WHEN
let variableWithDefaultContext = sdk.getVariable(
featureKey: "test",
variableKey: "color",
context: ["userId": .string("123")]
)

let variableWithExtendedContextByCountry = sdk.getVariable(
featureKey: "test",
variableKey: "color",
context: ["userId": .string("123"), "country": .string("nl")]
)

// THEN
XCTAssertEqual(variableWithDefaultContext!.value as! String, "red")
XCTAssertEqual(variableWithExtendedContextByCountry!.value as! String, "orange")
}
}

0 comments on commit 1609228

Please sign in to comment.