Skip to content

Commit

Permalink
fix: forcing variation with variable overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
polok committed Jan 4, 2024
1 parent 34976ad commit 81d1127
Show file tree
Hide file tree
Showing 3 changed files with 438 additions and 9 deletions.
17 changes: 14 additions & 3 deletions Sources/FeaturevisorSDK/Instance+Evaluation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ extension FeaturevisorInstance {
// forced
let force = findForceFromFeature(feature, context: context, datafileReader: datafileReader)

if let force, let variableValue = force.variables[variableKey] {
if let force, let variableValue = force.variables?[variableKey] {
evaluation = Evaluation(
featureKey: feature.key,
reason: .forced,
Expand Down Expand Up @@ -498,10 +498,21 @@ extension FeaturevisorInstance {
}

// regular allocation
if let matchedAllocation = matchedTrafficAndAllocation.matchedAllocation {
var variationValue: VariationValue? = nil

if let forceVariation = force?.variation {
variationValue = forceVariation
}
else if let matchedAllocationVariation = matchedTrafficAndAllocation.matchedAllocation?
.variation
{
variationValue = matchedAllocationVariation
}

if let variationValue {

let variation = feature.variations.first(where: { variation in
return variation.value == matchedAllocation.variation
return variation.value == variationValue
})

if let variationVariables = variation?.variables {
Expand Down
38 changes: 32 additions & 6 deletions Sources/FeaturevisorTypes/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,17 @@ public struct VariableOverride: Codable {
public let conditions: Condition?
public let segments: GroupSegment?

internal init(
value: VariableValue,
conditions: Condition? = nil,
segments: GroupSegment? = nil
) {

self.value = value
self.conditions = conditions
self.segments = segments
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
value = try container.decode(VariableValue.self, forKey: .value)
Expand Down Expand Up @@ -403,21 +414,36 @@ public typealias FeatureKey = String
public typealias VariableValues = [VariableKey: VariableValue]

public struct Force: Decodable {
public let variation: VariationValue?
public let variables: VariableValues?

// one of the below must be present in YAML
public let conditions: Condition?
public let segments: GroupSegment?

public let enabled: Bool?
public let variation: VariationValue
public let variables: VariableValues

internal init(
variation: VariationValue? = nil,
variables: VariableValues? = nil,
conditions: Condition? = nil,
segments: GroupSegment? = nil,
enabled: Bool? = nil
) {
self.variation = variation
self.variables = variables
self.conditions = conditions
self.segments = segments
self.enabled = enabled
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
enabled = try? container.decodeIfPresent(Bool.self, forKey: .enabled)
variation = try container.decode(VariationValue.self, forKey: .variation)
variables = try container.decode(VariableValues.self, forKey: .variables)
conditions = try container.decodeStringifiedIfPresent(Condition.self, forKey: .conditions)
segments = try container.decodeGroupSegmentIfPresent(forKey: .segments)
variation = try? container.decode(VariationValue.self, forKey: .variation)
variables = try? container.decode(VariableValues.self, forKey: .variables)
conditions = try? container.decodeStringifiedIfPresent(Condition.self, forKey: .conditions)
segments = try? container.decodeGroupSegmentIfPresent(forKey: .segments)
}

enum CodingKeys: CodingKey {
Expand Down
Loading

0 comments on commit 81d1127

Please sign in to comment.