Skip to content

Commit e66b01d

Browse files
committed
Merge branch 'master' into jae/dispatcher2
2 parents 1cb97c1 + e6a7a66 commit e66b01d

File tree

15 files changed

+1096
-285
lines changed

15 files changed

+1096
-285
lines changed

OptimizelySDK/Data Model/Audience/AttributeValue.swift

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ enum AttributeValue: Codable, Equatable, CustomStringConvertible {
4141
}
4242

4343
// 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+
{
4547
self = .double(doubleValue)
4648
return
4749
}
4850

49-
if let int64Value = Utils.getInt64Value(value) {
51+
if let int64Value = Utils.getInt64Value(value),
52+
AttributeValue.isValueValidRange(num: Double(int64Value))
53+
{
5054
self = .int(int64Value)
5155
return
5256
}
@@ -110,8 +114,6 @@ enum AttributeValue: Codable, Equatable, CustomStringConvertible {
110114
extension AttributeValue {
111115

112116
func isExactMatch(with target: Any) throws -> Bool {
113-
try checkValidAttributeNumber(target)
114-
115117
guard let targetValue = AttributeValue(value: target) else {
116118
throw OptimizelyError.conditionInvalidValueType(prettySrc(#function, target: target))
117119
}
@@ -146,38 +148,28 @@ extension AttributeValue {
146148
}
147149

148150
func isGreater(than target: Any) throws -> Bool {
149-
try checkValidAttributeNumber(target)
150-
151151
guard let targetValue = AttributeValue(value: target) else {
152152
throw OptimizelyError.conditionInvalidValueType(prettySrc(#function, target: target))
153153
}
154154

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 {
160157
throw OptimizelyError.conditionInvalidValueType(prettySrc(#function, target: target))
161158
}
162159

163160
return currentDouble > targetDouble
164161
}
165162

166163
func isLess(than target: Any) throws -> Bool {
167-
try checkValidAttributeNumber(target)
168-
169164
guard let targetValue = AttributeValue(value: target) else {
170165
throw OptimizelyError.conditionInvalidValueType(prettySrc(#function, target: target))
171166
}
172167

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))
179171
}
180-
172+
181173
return currentDouble < targetDouble
182174
}
183175

@@ -201,22 +193,9 @@ extension AttributeValue {
201193
}
202194
}
203195

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 {
216197
// 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)
220199
}
221200

222201
func prettySrc(_ src: String, target: Any? = nil) -> String {

OptimizelySDK/Data Model/Audience/UserAttribute.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,33 +106,33 @@ extension UserAttribute {
106106

107107
let attributes = attributes ?? OptimizelyAttributes()
108108

109-
let attributeValue = attributes[nameFinal] ?? nil // default to nil to avoid warning "coerced from 'Any??' to 'Any?'"
109+
let rawAttributeValue = attributes[nameFinal] ?? nil // default to nil to avoid warning "coerced from 'Any??' to 'Any?'"
110110

111111
if matchFinal != .exists {
112112
if value == nil {
113113
throw OptimizelyError.conditionInvalidFormat("missing value (\(nameFinal)) in condition)")
114114
}
115115

116-
if attributeValue == nil {
116+
if rawAttributeValue == nil {
117117
throw OptimizelyError.conditionNoAttributeValue("no attribute value for (\(nameFinal))")
118118
}
119119
}
120120

121121
switch matchFinal {
122122
case .exists:
123-
return !(attributeValue is NSNull || attributeValue == nil)
123+
return !(rawAttributeValue is NSNull || rawAttributeValue == nil)
124124
case .exact:
125-
return try value!.isExactMatch(with: attributeValue!)
125+
return try value!.isExactMatch(with: rawAttributeValue!)
126126
case .substring:
127-
return try value!.isSubstring(of: attributeValue!)
127+
return try value!.isSubstring(of: rawAttributeValue!)
128128
case .lt:
129129
// user attribute "less than" this condition value
130130
// so evaluate if this condition value "isGreater" than the user attribute value
131-
return try value!.isGreater(than: attributeValue!)
131+
return try value!.isGreater(than: rawAttributeValue!)
132132
case .gt:
133133
// user attribute "greater than" this condition value
134134
// so evaluate if this condition value "isLess" than the user attribute value
135-
return try value!.isLess(than: attributeValue!)
135+
return try value!.isLess(than: rawAttributeValue!)
136136
}
137137
}
138138

OptimizelySDK/Data Model/DispatchEvents/BatchEvent.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ struct BatchEvent: Codable, Equatable {
2828
case anonymizeIP = "anonymize_ip"
2929
case enrichDecisions = "enrich_decisions"
3030
}
31+
32+
func getEventAttribute(key: String) -> EventAttribute? {
33+
for visitor in visitors {
34+
if let attribute = visitor.attributes.filter({ $0.key == key }).first {
35+
return attribute
36+
}
37+
}
38+
39+
return nil
40+
}
3141
}
3242

3343
struct Visitor: Codable, Equatable {
@@ -103,7 +113,7 @@ struct DispatchEvent: Codable, Equatable {
103113
key: String,
104114
entityID: String,
105115
uuid: String,
106-
tags: Dictionary<String,AttributeValue>? = [:],
116+
tags: [String: AttributeValue]? = [:],
107117
value: AttributeValue? = nil,
108118
revenue: AttributeValue? = nil)
109119
{

0 commit comments

Comments
 (0)