Skip to content

Commit 9d2e89e

Browse files
authored
New methods parsing in iOS (#97)
2 parents c68f260 + d23fd6a commit 9d2e89e

File tree

5 files changed

+149
-20
lines changed

5 files changed

+149
-20
lines changed

splitio_ios/example/ios/SplitTests/SplitMethodParserTests.swift

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,59 @@ class SplitMethodParserTests: XCTestCase {
8686
}
8787
}
8888

89+
func testGetTreatmentsByFlagSet() {
90+
methodParser?.onMethodCall(methodName: "getTreatmentsByFlagSet", arguments: ["matchingKey": "user-key", "bucketingKey": "bucketing-key", "flagSet": "set_1", "attributes": ["age": 50]], result: { (_: Any?) in
91+
return
92+
})
93+
94+
if let splitWrapper = (splitWrapper as? SplitWrapperStub) {
95+
XCTAssert(splitWrapper.matchingKeyValue == "user-key")
96+
XCTAssert(splitWrapper.bucketingKeyValue == "bucketing-key")
97+
XCTAssert(splitWrapper.flagSetValue == "set_1")
98+
print(splitWrapper.attributesValue)
99+
XCTAssert(NSDictionary(dictionary: ["age": 50]).isEqual(to: splitWrapper.attributesValue!))
100+
}
101+
}
102+
103+
func testGetTreatmentsByFlagSets() {
104+
methodParser?.onMethodCall(methodName: "getTreatmentsByFlagSets", arguments: ["matchingKey": "user-key", "bucketingKey": "bucketing-key", "flagSets": ["set_1", "set_2"], "attributes": ["age": 50]], result: { (_: Any?) in
105+
return
106+
})
107+
108+
if let splitWrapper = (splitWrapper as? SplitWrapperStub) {
109+
XCTAssert(splitWrapper.matchingKeyValue == "user-key")
110+
XCTAssert(splitWrapper.bucketingKeyValue == "bucketing-key")
111+
XCTAssert(splitWrapper.flagSetsValue == ["set_1", "set_2"])
112+
XCTAssert(NSDictionary(dictionary: ["age": 50]).isEqual(to: splitWrapper.attributesValue!))
113+
}
114+
}
115+
116+
func testGetTreatmentsWithConfigByFlagSet() {
117+
methodParser?.onMethodCall(methodName: "getTreatmentsWithConfigByFlagSet", arguments: ["matchingKey": "user-key", "bucketingKey": "bucketing-key", "flagSet": "set_1", "attributes": ["age": 50]], result: { (_: Any?) in
118+
return
119+
})
120+
121+
if let splitWrapper = (splitWrapper as? SplitWrapperStub) {
122+
XCTAssert(splitWrapper.matchingKeyValue == "user-key")
123+
XCTAssert(splitWrapper.bucketingKeyValue == "bucketing-key")
124+
XCTAssert(splitWrapper.flagSetValue == "set_1")
125+
XCTAssert(NSDictionary(dictionary: ["age": 50]).isEqual(to: splitWrapper.attributesValue!))
126+
}
127+
}
128+
129+
func testGetTreatmentsWithConfigByFlagSets() {
130+
methodParser?.onMethodCall(methodName: "getTreatmentsWithConfigByFlagSets", arguments: ["matchingKey": "user-key", "bucketingKey": "bucketing-key", "flagSets": ["set_1", "set_2"], "attributes": ["age": 50]], result: { (_: Any?) in
131+
return
132+
})
133+
134+
if let splitWrapper = (splitWrapper as? SplitWrapperStub) {
135+
XCTAssert(splitWrapper.matchingKeyValue == "user-key")
136+
XCTAssert(splitWrapper.bucketingKeyValue == "bucketing-key")
137+
XCTAssert(splitWrapper.flagSetsValue == ["set_1", "set_2"])
138+
XCTAssert(NSDictionary(dictionary: ["age": 50]).isEqual(to: splitWrapper.attributesValue!))
139+
}
140+
}
141+
89142
func testTrackWithValue() {
90143
methodParser?.onMethodCall(methodName: "track", arguments: ["matchingKey": "user-key", "bucketingKey": "bucketing-key", "eventType": "my_event", "value": 25.20], result: { (_: Any?) in
91144
return
@@ -395,7 +448,7 @@ class SplitWrapperStub: SplitWrapper {
395448
matchingKeyValue = matchingKey
396449
bucketingKeyValue = bucketingKey
397450
flagSetValue = flagSet
398-
attributeValue = attributes
451+
attributesValue = attributes
399452

400453
return [:]
401454
}
@@ -404,7 +457,7 @@ class SplitWrapperStub: SplitWrapper {
404457
matchingKeyValue = matchingKey
405458
bucketingKeyValue = bucketingKey
406459
flagSetsValue = flagSets
407-
attributeValue = attributes
460+
attributesValue = attributes
408461

409462
return [:]
410463
}
@@ -413,7 +466,7 @@ class SplitWrapperStub: SplitWrapper {
413466
matchingKeyValue = matchingKey
414467
bucketingKeyValue = bucketingKey
415468
flagSetValue = flagSet
416-
attributeValue = attributes
469+
attributesValue = attributes
417470

418471
return [:]
419472
}
@@ -422,7 +475,7 @@ class SplitWrapperStub: SplitWrapper {
422475
matchingKeyValue = matchingKey
423476
bucketingKeyValue = bucketingKey
424477
flagSetsValue = flagSets
425-
attributeValue = attributes
478+
attributesValue = attributes
426479

427480
return [:]
428481
}

splitio_ios/ios/Classes/Constants.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ enum Method: String {
1313
case getTreatments = "getTreatments"
1414
case getTreatmentWithConfig = "getTreatmentWithConfig"
1515
case getTreatmentsWithConfig = "getTreatmentsWithConfig"
16+
case getTreatmentsByFlagSet = "getTreatmentsByFlagSet"
17+
case getTreatmentsByFlagSets = "getTreatmentsByFlagSets"
18+
case getTreatmentsWithConfigByFlagSet = "getTreatmentsWithConfigByFlagSet"
19+
case getTreatmentsWithConfigByFlagSets = "getTreatmentsWithConfigByFlagSets"
1620
case track = "track"
1721
case getAttribute = "getAttribute"
1822
case getAllAttributes = "getAllAttributes"
@@ -40,4 +44,6 @@ enum Argument: String {
4044
case value = "value"
4145
case properties = "properties"
4246
case attributeName = "attributeName"
47+
case flagSet = "flagSet"
48+
case flagSets = "flagSets"
4349
}

splitio_ios/ios/Classes/Extensions.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ extension SplitView {
2222
"killed": splitView.killed,
2323
"treatments": splitView.treatments,
2424
"changeNumber": splitView.changeNumber,
25-
"configs": splitView.configs]
25+
"configs": splitView.configs,
26+
"defaultTreatment": splitView.defaultTreatment,
27+
"sets": splitView.sets
28+
]
2629
} else {
2730
return [:]
2831
}

splitio_ios/ios/Classes/SplitMethodParser.swift

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,34 @@ class DefaultSplitMethodParser: SplitMethodParser {
7575
splits: argumentParser.getStringListArgument(argumentName: .splitName, arguments: arguments),
7676
attributes: argumentParser.getMapArgument(argumentName: .attributes, arguments: arguments) as [String: Any]))
7777
break
78+
case .getTreatmentsByFlagSet:
79+
result(getTreatmentsByFlagSet(
80+
matchingKey: argumentParser.getStringArgument(argumentName: .matchingKey, arguments: arguments) ?? "",
81+
bucketingKey: argumentParser.getStringArgument(argumentName: .bucketingKey, arguments: arguments),
82+
flagSet: argumentParser.getStringArgument(argumentName: .flagSet, arguments: arguments) ?? "",
83+
attributes: argumentParser.getMapArgument(argumentName: .attributes, arguments: arguments) as [String: Any]))
84+
break
85+
case .getTreatmentsByFlagSets:
86+
result(getTreatmentsByFlagSets(
87+
matchingKey: argumentParser.getStringArgument(argumentName: .matchingKey, arguments: arguments) ?? "",
88+
bucketingKey: argumentParser.getStringArgument(argumentName: .bucketingKey, arguments: arguments),
89+
flagSets: argumentParser.getStringListArgument(argumentName: .flagSets, arguments: arguments),
90+
attributes: argumentParser.getMapArgument(argumentName: .attributes, arguments: arguments) as [String: Any]))
91+
break
92+
case .getTreatmentsWithConfigByFlagSet:
93+
result(getTreatmentsWithConfigByFlagSet(
94+
matchingKey: argumentParser.getStringArgument(argumentName: .matchingKey, arguments: arguments) ?? "",
95+
bucketingKey: argumentParser.getStringArgument(argumentName: .bucketingKey, arguments: arguments),
96+
flagSet: argumentParser.getStringArgument(argumentName: .flagSet, arguments: arguments) ?? "",
97+
attributes: argumentParser.getMapArgument(argumentName: .attributes, arguments: arguments) as [String: Any]))
98+
break
99+
case .getTreatmentsWithConfigByFlagSets:
100+
result(getTreatmentsWithConfigByFlagSets(
101+
matchingKey: argumentParser.getStringArgument(argumentName: .matchingKey, arguments: arguments) ?? "",
102+
bucketingKey: argumentParser.getStringArgument(argumentName: .bucketingKey, arguments: arguments),
103+
flagSets: argumentParser.getStringListArgument(argumentName: .flagSets, arguments: arguments),
104+
attributes: argumentParser.getMapArgument(argumentName: .attributes, arguments: arguments) as [String: Any]))
105+
break
78106
case .track:
79107
result(track(matchingKey: argumentParser.getStringArgument(argumentName: .matchingKey, arguments: arguments) ?? "",
80108
bucketingKey: argumentParser.getStringArgument(argumentName: .bucketingKey, arguments: arguments),
@@ -215,6 +243,50 @@ class DefaultSplitMethodParser: SplitMethodParser {
215243
}
216244
}
217245

246+
private func getTreatmentsByFlagSet(matchingKey: String, bucketingKey: String? = nil, flagSet: String, attributes: [String: Any]? = [:]) -> [String: String] {
247+
guard let splitWrapper = getSplitWrapper() else {
248+
return [:]
249+
}
250+
251+
let treatments = splitWrapper.getTreatmentsByFlagSet(matchingKey: matchingKey, flagSet: flagSet, bucketingKey: bucketingKey, attributes: attributes)
252+
253+
return treatments
254+
}
255+
256+
private func getTreatmentsByFlagSets(matchingKey: String, bucketingKey: String? = nil, flagSets: [String], attributes: [String: Any]? = [:]) -> [String: String] {
257+
guard let splitWrapper = getSplitWrapper() else {
258+
return [:]
259+
}
260+
261+
let treatments = splitWrapper.getTreatmentsByFlagSets(matchingKey: matchingKey, flagSets: flagSets, bucketingKey: bucketingKey, attributes: attributes)
262+
263+
return treatments
264+
}
265+
266+
private func getTreatmentsWithConfigByFlagSet(matchingKey: String, bucketingKey: String? = nil, flagSet: String, attributes: [String: Any]? = [:]) -> [String: [String: String?]] {
267+
guard let splitWrapper = getSplitWrapper() else {
268+
return [:]
269+
}
270+
271+
let treatments = splitWrapper.getTreatmentsWithConfigByFlagSet(matchingKey: matchingKey, flagSet: flagSet, bucketingKey: bucketingKey, attributes: attributes)
272+
273+
return treatments.mapValues {
274+
["treatment": $0.treatment, "config": $0.config]
275+
}
276+
}
277+
278+
private func getTreatmentsWithConfigByFlagSets(matchingKey: String, bucketingKey: String? = nil, flagSets: [String], attributes: [String: Any]? = [:]) -> [String: [String: String?]] {
279+
guard let splitWrapper = getSplitWrapper() else {
280+
return [:]
281+
}
282+
283+
let treatments = splitWrapper.getTreatmentsWithConfigByFlagSets(matchingKey: matchingKey, flagSets: flagSets, bucketingKey: bucketingKey, attributes: attributes)
284+
285+
return treatments.mapValues {
286+
["treatment": $0.treatment, "config": $0.config]
287+
}
288+
}
289+
218290
private func track(matchingKey: String, bucketingKey: String? = nil, eventType: String, trafficType: String? = nil, value: Double? = nil, properties: [String: Any?]) -> Bool {
219291
guard let splitWrapper = getSplitWrapper() else {
220292
return false
@@ -320,18 +392,4 @@ class DefaultSplitMethodParser: SplitMethodParser {
320392

321393
return splitWrapper
322394
}
323-
324-
private func getSplitViewAsMap(splitView: SplitView?) -> [String: Any?] {
325-
if let splitView = splitView {
326-
return [
327-
"name": splitView.name,
328-
"trafficType": splitView.trafficType,
329-
"killed": splitView.killed,
330-
"treatments": splitView.treatments,
331-
"changeNumber": splitView.changeNumber,
332-
"configs": splitView.configs]
333-
} else {
334-
return [:]
335-
}
336-
}
337395
}

splitio_platform_interface/lib/split_view.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ class SplitView {
2424
mappedConfig.addAll({entry.key.toString(): entry.value.toString()})
2525
});
2626

27+
if (entry['treatments'] == null) {
28+
entry['treatments'] = entry['treatments'] ?? [];
29+
}
30+
31+
if (entry['sets'] == null) {
32+
entry['sets'] = [];
33+
}
34+
2735
return SplitView(
2836
entry['name'],
2937
entry['trafficType'],
@@ -32,7 +40,8 @@ class SplitView {
3240
entry['changeNumber'],
3341
mappedConfig,
3442
entry['defaultTreatment'] ?? '',
35-
(entry['sets'] ?? [] as List).map((el) => el as String).toList());
43+
(entry['sets'] as List).map((el) => el as String).toList()
44+
);
3645
}
3746

3847
@override

0 commit comments

Comments
 (0)