Skip to content

Commit 424e2f9

Browse files
wip: add array support
1 parent 94d6175 commit 424e2f9

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

Sources/Data Model/Audience/AttributeValue.swift

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
import Foundation
1818

1919
enum AttributeValue: Codable, Equatable, CustomStringConvertible {
20+
typealias AttrArray = Array<AttributeValue>
21+
typealias AttrDictionary = [String : AttributeValue]
22+
2023
case string(String)
2124
case int(Int64) // supported value range [-2^53, 2^53]
2225
case double(Double)
2326
case bool(Bool)
24-
case custom([String : AttributeValue])
27+
case array(AttrArray)
28+
case dictionary(AttrDictionary)
2529
case others
2630

2731
var description: String {
@@ -34,8 +38,10 @@ enum AttributeValue: Codable, Equatable, CustomStringConvertible {
3438
return "int(\(value))"
3539
case .bool(let value):
3640
return "bool(\(value))"
37-
case .custom(let value):
38-
return "custom(\(value))"
41+
case .array(let value):
42+
return "array(\(value))"
43+
case .dictionary(let value):
44+
return "dictionary(\(value))"
3945
case .others:
4046
return "others"
4147
}
@@ -66,9 +72,15 @@ enum AttributeValue: Codable, Equatable, CustomStringConvertible {
6672
return
6773
}
6874

69-
if let custom = value as? [String : Any] {
70-
let attr = custom.compactMapValues { AttributeValue(value: $0) }
71-
self = .custom(attr)
75+
if let arrValue = value as? [Any] {
76+
let attr = arrValue.compactMap { AttributeValue(value: $0) }
77+
self = .array(attr)
78+
return
79+
}
80+
81+
if let dicValue = value as? [String : Any] {
82+
let attr = dicValue.compactMapValues { AttributeValue(value: $0) }
83+
self = .dictionary(attr)
7284
return
7385
}
7486

@@ -95,8 +107,13 @@ enum AttributeValue: Codable, Equatable, CustomStringConvertible {
95107
return
96108
}
97109

98-
if let value = try? container.decode([String: AttributeValue].self) {
99-
self = .custom(value)
110+
if let value = try? container.decode(AttrArray.self) {
111+
self = .array(value)
112+
return
113+
}
114+
115+
if let value = try? container.decode(AttrDictionary.self) {
116+
self = .dictionary(value)
100117
return
101118
}
102119

@@ -117,7 +134,9 @@ enum AttributeValue: Codable, Equatable, CustomStringConvertible {
117134
try container.encode(value)
118135
case .bool(let value):
119136
try container.encode(value)
120-
case .custom(let value):
137+
case .array(let value):
138+
try container.encode(value)
139+
case .dictionary(let value):
121140
try container.encode(value.mapValues { $0 })
122141
case .others:
123142
return
@@ -151,7 +170,11 @@ extension AttributeValue {
151170
return true
152171
}
153172

154-
if case .custom(let selfDict) = self, case .custom(let targetDict) = targetValue {
173+
if case .array(let selfArr) = self, case .array(let targetArr) = targetValue {
174+
return selfArr == targetArr
175+
}
176+
177+
if case .dictionary(let selfDict) = self, case .dictionary(let targetDict) = targetValue {
155178
return selfDict == targetDict
156179
}
157180

@@ -247,7 +270,9 @@ extension AttributeValue {
247270
return String(value)
248271
case .bool(let value):
249272
return String(value)
250-
case .custom(let value):
273+
case .array(let value):
274+
return String(describing: value)
275+
case .dictionary(let value):
251276
return String(describing: value)
252277
case .others:
253278
return "UNKNOWN"
@@ -262,7 +287,8 @@ extension AttributeValue {
262287
case (.double, .int): return true
263288
case (.double, .double): return true
264289
case (.bool, .bool): return true
265-
case (.custom, .custom): return true
290+
case (.array, .array): return true
291+
case (.dictionary, .dictionary): return true
266292
default: return false
267293
}
268294
}
@@ -294,6 +320,7 @@ extension AttributeValue {
294320
case (.int): return true
295321
case (.double): return true
296322
case (.bool): return true
323+
case (.array): return true
297324
default: return false
298325
}
299326
}

Tests/OptimizelyTests-DataModel/AttributeValueTests.swift

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,13 @@ class AttributeValueTests: XCTestCase {
128128
XCTAssert(model2 == AttributeValue.int(Int64(value)))
129129
}
130130

131-
func testDecodeSuccessWithInvalidType() {
132-
let value = ["invalid type"]
131+
func testDecodeSuccessWithArrayType() {
132+
let value = ["array type"]
133133

134134
let model = try! OTUtils.getAttributeValueFromNative(value)
135135

136-
XCTAssert(model == AttributeValue.others)
137-
138136
let model2 = AttributeValue(value: value)
139-
XCTAssertNil(model2)
137+
XCTAssertEqual(model, model2)
140138
}
141139

142140
func testDecodeSuccessWithInvalidTypeNil() {
@@ -274,10 +272,10 @@ extension AttributeValueTests {
274272
XCTAssert(OTUtils.isEqualWithEncodeThenDecode(modelGiven))
275273
}
276274

277-
// func testEncodeJSON5() {
278-
// let modelGiven = [AttributeValue.others]
279-
// XCTAssert(OTUtils.isEqualWithEncodeThenDecode(modelGiven))
280-
// }
275+
func testEncodeJSON5() {
276+
let modelGiven = [AttributeValue.array([AttributeValue.bool(true), AttributeValue.string("us"), AttributeValue.double(4.7)])]
277+
XCTAssert(OTUtils.isEqualWithEncodeThenDecode(modelGiven))
278+
}
281279

282280
}
283281

@@ -301,18 +299,17 @@ extension AttributeValueTests {
301299
XCTAssert(model == AttributeValue.bool(valueBool))
302300
XCTAssert(model.description == "bool(\(valueBool))")
303301

304-
let valueOther = [3]
305-
model = try! OTUtils.getAttributeValueFromNative(valueOther)
306-
XCTAssert(model == AttributeValue.others)
307-
XCTAssert(model.description == "others")
308-
302+
let values = [3.0]
303+
model = try! OTUtils.getAttributeValueFromNative(values)
304+
XCTAssert(model == AttributeValue(value: values))
305+
XCTAssert(model.description == "array([double(3.0)])")
309306

310307
let valueInteger = Int64(100)
311308
model = AttributeValue(value: valueInteger)!
312309
XCTAssert(model.description == "int(\(valueInteger))")
313310

314-
let modelOptional = AttributeValue(value: valueOther)
315-
XCTAssertNil(modelOptional)
311+
let modelOptional = AttributeValue(value: values)
312+
XCTAssertNotNil(modelOptional)
316313
}
317314

318315
func testStringValue() {

0 commit comments

Comments
 (0)