@@ -41,12 +41,16 @@ enum AttributeValue: Codable, Equatable, CustomStringConvertible {
41
41
}
42
42
43
43
// NOTE: keep {Double, Float} before Int checking for testing consistency
44
- if let doubleValue = Utils . getDoubleValue ( value) {
44
+ if let doubleValue = Utils . getDoubleValue ( value) ,
45
+ AttributeValue . isValueValidRange ( num: doubleValue)
46
+ {
45
47
self = . double( doubleValue)
46
48
return
47
49
}
48
50
49
- if let int64Value = Utils . getInt64Value ( value) {
51
+ if let int64Value = Utils . getInt64Value ( value) ,
52
+ AttributeValue . isValueValidRange ( num: Double ( int64Value) )
53
+ {
50
54
self = . int( int64Value)
51
55
return
52
56
}
@@ -110,8 +114,6 @@ enum AttributeValue: Codable, Equatable, CustomStringConvertible {
110
114
extension AttributeValue {
111
115
112
116
func isExactMatch( with target: Any ) throws -> Bool {
113
- try checkValidAttributeNumber ( target)
114
-
115
117
guard let targetValue = AttributeValue ( value: target) else {
116
118
throw OptimizelyError . conditionInvalidValueType ( prettySrc ( #function, target: target) )
117
119
}
@@ -146,38 +148,28 @@ extension AttributeValue {
146
148
}
147
149
148
150
func isGreater( than target: Any ) throws -> Bool {
149
- try checkValidAttributeNumber ( target)
150
-
151
151
guard let targetValue = AttributeValue ( value: target) else {
152
152
throw OptimizelyError . conditionInvalidValueType ( prettySrc ( #function, target: target) )
153
153
}
154
154
155
- guard let currentDouble = self . doubleValue else {
156
- throw OptimizelyError . conditionInvalidValueType ( prettySrc ( #function, target: target) )
157
- }
158
-
159
- guard let targetDouble = targetValue. doubleValue else {
155
+ guard let currentDouble = self . doubleValue,
156
+ let targetDouble = targetValue. doubleValue else {
160
157
throw OptimizelyError . conditionInvalidValueType ( prettySrc ( #function, target: target) )
161
158
}
162
159
163
160
return currentDouble > targetDouble
164
161
}
165
162
166
163
func isLess( than target: Any ) throws -> Bool {
167
- try checkValidAttributeNumber ( target)
168
-
169
164
guard let targetValue = AttributeValue ( value: target) else {
170
165
throw OptimizelyError . conditionInvalidValueType ( prettySrc ( #function, target: target) )
171
166
}
172
167
173
- guard let currentDouble = self . doubleValue else {
174
- throw OptimizelyError . conditionInvalidValueType ( prettySrc ( #function, target: target) )
175
- }
176
-
177
- guard let targetDouble = targetValue. doubleValue else {
178
- throw OptimizelyError . conditionInvalidValueType ( prettySrc ( #function, target: target) )
168
+ guard let currentDouble = self . doubleValue,
169
+ let targetDouble = targetValue. doubleValue else {
170
+ throw OptimizelyError . conditionInvalidValueType ( prettySrc ( #function, target: target) )
179
171
}
180
-
172
+
181
173
return currentDouble < targetDouble
182
174
}
183
175
@@ -201,22 +193,9 @@ extension AttributeValue {
201
193
}
202
194
}
203
195
204
- func checkValidAttributeNumber( _ number: Any ) throws {
205
- var num : Double
206
-
207
- if let number = Utils . getInt64Value ( number) {
208
- num = Double ( number)
209
- } else if let number = Utils . getDoubleValue ( number) {
210
- num = number
211
- } else {
212
- // do not check range if it's not a number
213
- return
214
- }
215
-
196
+ static func isValueValidRange( num: Double ) -> Bool {
216
197
// valid range: [-2^53, 2^53] i
217
- if abs ( num) > pow ( 2 , 53 ) {
218
- throw OptimizelyError . attributeValueInvalid
219
- }
198
+ return abs ( num) <= pow ( 2 , 53 )
220
199
}
221
200
222
201
func prettySrc( _ src: String , target: Any ? = nil ) -> String {
0 commit comments