-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathDecisionInfo.swift
149 lines (117 loc) · 5.91 KB
/
DecisionInfo.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Copyright 2021, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
struct DecisionInfo {
/// The decision type.
let decisionType: Constants.DecisionType
/// The experiment that the decision variation belongs to.
var experiment: ExperimentCore?
/// The variation selected by the decision.
var variation: Variation?
// The source of the decision
var source: String?
/// The flag for which the decision has been made.
var feature: FeatureFlag?
/// The boolean value indicating the flag is enabled or not.
var featureEnabled: Bool?
/// The key of the requested flag variable.
var variableKey: String?
/// The type of the requested flag variable.
var variableType: String?
/// The value of the requested flag variable.
var variableValue: Any?
/// The map of all the requested flag variable values.
var variableValues: [String: Any]?
/// The ruleKey for the decision (for .flag type only).
var ruleKey: String?
/// The array of decision reason messages (for .flag type only).
var reasons: [String]?
/// The boolean value indicating an decision event has been sent for the decision (for .flag type only).
var decisionEventDispatched: Bool
init(decisionType: Constants.DecisionType,
experiment: ExperimentCore? = nil,
variation: Variation? = nil,
source: String? = nil,
feature: FeatureFlag? = nil,
featureEnabled: Bool? = nil,
variableKey: String? = nil,
variableType: String? = nil,
variableValue: Any? = nil,
variableValues: [String: Any]? = nil,
ruleKey: String? = nil,
reasons: [String]? = nil,
decisionEventDispatched: Bool = false) {
self.decisionType = decisionType
self.experiment = experiment
self.variation = variation
self.source = source
self.feature = feature
self.featureEnabled = featureEnabled
self.variableKey = variableKey
self.variableType = variableType
self.variableValue = variableValue
self.variableValues = variableValues
self.ruleKey = ruleKey
self.reasons = reasons
self.decisionEventDispatched = decisionEventDispatched
}
var toMap: [String: Any] {
var decisionInfo = [String: Any]()
switch decisionType {
case .featureTest, .abTest:
guard let experiment = experiment else { return decisionInfo }
decisionInfo[Constants.ExperimentDecisionInfoKeys.experiment] = experiment.key
decisionInfo[Constants.ExperimentDecisionInfoKeys.variation] = variation?.key ?? NSNull()
case .feature, .featureVariable, .allFeatureVariables:
guard let feature = feature, let featureEnabled = featureEnabled else { return decisionInfo }
decisionInfo[Constants.DecisionInfoKeys.feature] = feature.key
decisionInfo[Constants.DecisionInfoKeys.featureEnabled] = featureEnabled
let decisionSource: Constants.DecisionSource = experiment != nil ? .featureTest : .rollout
decisionInfo[Constants.DecisionInfoKeys.source] = source ?? decisionSource.rawValue
var sourceInfo = [String: Any]()
if let experiment = experiment, let variation = variation {
sourceInfo[Constants.ExperimentDecisionInfoKeys.experiment] = experiment.key
sourceInfo[Constants.ExperimentDecisionInfoKeys.variation] = variation.key
}
decisionInfo[Constants.DecisionInfoKeys.sourceInfo] = sourceInfo
// featureVariable
if decisionType == .featureVariable {
guard let variableKey = variableKey, let variableType = variableType, let variableValue = variableValue else {
return decisionInfo
}
decisionInfo[Constants.DecisionInfoKeys.variable] = variableKey
decisionInfo[Constants.DecisionInfoKeys.variableType] = variableType
decisionInfo[Constants.DecisionInfoKeys.variableValue] = variableValue
} else if decisionType == .allFeatureVariables {
guard let variableValues = variableValues else {
return decisionInfo
}
decisionInfo[Constants.DecisionInfoKeys.variableValues] = variableValues
}
// Decide-APIs
case .flag:
guard let flagKey = feature?.key, let enabled = featureEnabled else { return decisionInfo }
decisionInfo[Constants.DecisionInfoKeys.flagKey] = flagKey
decisionInfo[Constants.DecisionInfoKeys.enabled] = enabled
decisionInfo[Constants.DecisionInfoKeys.variables] = variableValues
decisionInfo[Constants.DecisionInfoKeys.variationKey] = variation?.key ?? NSNull() // keep key in the map even with nil value
decisionInfo[Constants.DecisionInfoKeys.ruleKey] = ruleKey ?? NSNull() //
decisionInfo[Constants.DecisionInfoKeys.reasons] = reasons
decisionInfo[Constants.DecisionInfoKeys.decisionEventDispatched] = decisionEventDispatched
}
return decisionInfo
}
}