@@ -21,6 +21,7 @@ extension Feature: Codable {
21
21
case testVariations = " test-variations "
22
22
case labels = " labels "
23
23
}
24
+ static let defaultTestVariations : [ TestVariation ] = [ . enabled, . disabled]
24
25
25
26
public init ( from decoder: Decoder ) throws {
26
27
let container = try decoder. container ( keyedBy: CodingKeys . self)
@@ -38,76 +39,71 @@ extension Feature: Codable {
38
39
}
39
40
let testBiases = try container. decodeIfPresent ( [ Percentage ] . self, forKey: . testBiases)
40
41
self . testVariationAssignment = try container. decodeIfPresent ( Double . self, forKey: . testVariationAssignment)
41
- ?? Double . random ( in: 0 ..< 100 ) // [0.0, 100.0)
42
+ ?? Double . random ( in: 0 ..< 100 ) // [0.0, 100.0)
42
43
let testVariations = try container. decodeIfPresent ( [ String ] . self, forKey: . testVariations)
43
- let defaultTestVariations = [ TestVariation ( rawValue: " Enabled " ) , TestVariation ( rawValue: " Disabled " ) ]
44
44
self . unlocked = unlocked
45
+ let parsedTestVariations : [ TestVariation ]
45
46
if let testVariations = testVariations {
46
- if testVariations. isEmpty {
47
+ switch testVariations. count {
48
+ case 0 :
47
49
self . isDevelopment = isDevelopment ?? false
48
50
self . enabled = isEnabled ?? false
49
- self . testVariations = defaultTestVariations
50
51
self . type = type ?? . featureFlag
51
52
self . testVariationAssignment = enabled ? 1.0 : 99.0
52
- } else if testVariations. count == 1 , let firstVariation = testVariations. first {
53
+ parsedTestVariations = Self . defaultTestVariations
54
+ case 1 :
53
55
self . enabled = isEnabled ?? false
54
56
self . isDevelopment = isDevelopment ?? false
55
- self . testVariations = [ TestVariation ( rawValue: firstVariation) ,
56
- TestVariation ( rawValue: " ! \( firstVariation) " ) ]
57
57
self . type = type ?? . featureFlag
58
- } else if testVariations. count == 2 {
58
+ if let firstVariation = testVariations. first {
59
+ parsedTestVariations = [
60
+ TestVariation ( rawValue: firstVariation) ,
61
+ TestVariation ( rawValue: " ! \( firstVariation) " )
62
+ ]
63
+ } else {
64
+ parsedTestVariations = Self . defaultTestVariations
65
+ }
66
+ case 2 :
59
67
self . enabled = isEnabled ?? true
60
68
self . isDevelopment = isDevelopment ?? false
61
- let testVariations = testVariations. map ( { TestVariation ( rawValue: $0) } )
62
- if Feature . testVariationsContains ( variationNames: [ " enabled " , " disabled " ] , in: testVariations)
63
- || Feature . testVariationsContains ( variationNames: [ " on " , " off " ] , in: testVariations) {
64
- self . type = type ?? . featureTest( . featureFlagAB)
65
- self . testVariations = defaultTestVariations
66
- } else {
67
- self . type = type ?? . featureTest( . ab)
68
- self . testVariations = testVariations
69
+ parsedTestVariations = testVariations. map {
70
+ TestVariation ( rawValue: $0)
69
71
}
70
- } else {
72
+ self . type = parsedTestVariations. sorted ( ) == Self . defaultTestVariations. sorted ( )
73
+ ? . featureTest( . featureFlagAB)
74
+ : . featureTest( . ab)
75
+ default :
71
76
self . enabled = isEnabled ?? true
72
77
self . isDevelopment = isDevelopment ?? false
73
- self . testVariations = testVariations. map ( { TestVariation ( rawValue: $0) } )
78
+ parsedTestVariations = testVariations. map {
79
+ TestVariation ( rawValue: $0)
80
+ }
74
81
self . type = type ?? . featureTest( . mvt)
75
82
}
76
83
} else {
77
84
self . enabled = isEnabled ?? false
78
85
self . isDevelopment = isDevelopment ?? false
79
- self . testVariations = defaultTestVariations
80
86
self . type = type ?? . featureFlag
81
87
self . testVariationAssignment = enabled ? 1.0 : 99.0
88
+ parsedTestVariations = Self . defaultTestVariations
82
89
}
83
- let variations = self . testVariations // Silences compiler error
90
+ self . testVariations = parsedTestVariations
84
91
if let testBiases = testBiases,
85
- testBiases. reduce ( Percentage . min, { ( runningTotal, testBias) -> Percentage in
86
- return runningTotal + testBias
87
- } ) == Percentage . max,
88
- testBiases. count == variations . count {
92
+ testBiases. reduce ( Percentage . min, { ( runningTotal, testBias) -> Percentage in
93
+ return runningTotal + testBias
94
+ } ) == Percentage . max,
95
+ testBiases. count == parsedTestVariations . count {
89
96
self . testBiases = testBiases
90
97
} else {
91
- self . testBiases = variations . map ( { _ in
92
- return Percentage ( rawValue: 100.0 / Double( variations . count) )
98
+ self . testBiases = parsedTestVariations . map ( { _ in
99
+ return Percentage ( rawValue: 100.0 / Double( parsedTestVariations . count) )
93
100
} )
94
101
}
95
102
self . labels = try container. decodeIfPresent ( [ String ? ] . self, forKey: . labels)
96
- ?? [ String? ] ( repeating: nil , count: variations . count)
103
+ ?? [ String? ] ( repeating: nil , count: parsedTestVariations . count)
97
104
self . testVariationOverride = nil
98
105
}
99
106
100
- private static func testVariationsContains( variationNames: [ String ] , in variations: [ Test . Variation ] ) -> Bool {
101
- var variationsFound : Bool = false
102
- for variationName in variationNames {
103
- variationsFound = variations. contains ( where: { testVariation in
104
- return testVariation. rawValue. lowercased ( ) == variationName
105
- } )
106
- if !variationsFound { break }
107
- }
108
- return variationsFound
109
- }
110
-
111
107
public func encode( to encoder: Encoder ) throws {
112
108
var container = encoder. container ( keyedBy: CodingKeys . self)
113
109
try container. encode ( name, forKey: . name)
0 commit comments