@@ -39,7 +39,7 @@ @implementation OPTLYCondition
39
39
40
40
// need to check if the jsonArray is actually an array, otherwise, something is wrong with the audience condition
41
41
if (![jsonArray isKindOfClass: [NSArray class ]]) {
42
- if ([jsonArray isKindOfClass: [ NSDictionary class ]] && [ OPTLYBaseCondition isBaseConditionJSON: ((NSData *)jsonArray)]) {
42
+ if ([OPTLYBaseCondition isBaseConditionJSON: ((NSData *)jsonArray)]) {
43
43
mutableJsonArray = [[NSMutableArray alloc ] initWithArray: @[OPTLYDatafileKeysOrCondition,jsonArray]];
44
44
}
45
45
else {
@@ -61,11 +61,10 @@ @implementation OPTLYCondition
61
61
[mutableJsonArray insertObject: OPTLYDatafileKeysOrCondition atIndex: 0 ];
62
62
}
63
63
64
- if ([OPTLYBaseCondition isBaseConditionJSON: mutableJsonArray[1 ]]) { // base case condition
65
-
66
- // generate all base conditions
67
- NSMutableArray <OPTLYCondition> *conditions = (NSMutableArray <OPTLYCondition> *)[[NSMutableArray alloc ] initWithCapacity: (mutableJsonArray.count - 1 )];
68
- for (int i = 1 ; i < mutableJsonArray.count ; i++) {
64
+ NSMutableArray <OPTLYCondition> *conditions = (NSMutableArray <OPTLYCondition> *)[[NSMutableArray alloc ] initWithCapacity: (mutableJsonArray.count - 1 )];
65
+ for (int i = 1 ; i < mutableJsonArray.count ; i++) {
66
+ if ([OPTLYBaseCondition isBaseConditionJSON: mutableJsonArray[i]]) {
67
+ // generate base condition
69
68
NSDictionary *info = mutableJsonArray[i];
70
69
NSError *err = nil ;
71
70
OPTLYBaseCondition *condition = [[OPTLYBaseCondition alloc ] initWithDictionary: info
@@ -78,41 +77,33 @@ @implementation OPTLYCondition
78
77
[conditions addObject: condition];
79
78
}
80
79
}
80
+ continue ;
81
81
}
82
82
83
- // return an (And/Or/Not) Condition handling the base conditions
84
- NSObject <OPTLYCondition> *condition = [OPTLYCondition createConditionInstanceOfClass: mutableJsonArray[0 ]
85
- withConditions: conditions];
86
- return (NSArray <OPTLYCondition *><OPTLYCondition> *)@[condition];
87
- }
88
- else {
89
-
90
83
// further condition arrays to deserialize
91
- NSMutableArray <OPTLYCondition> *subConditions = (NSMutableArray <OPTLYCondition> *)[[NSMutableArray alloc ] initWithCapacity: (mutableJsonArray.count - 1 )];
92
- for (int i = 1 ; i < mutableJsonArray.count ; i++) {
93
- NSError *err = nil ;
94
- NSArray *deserializedJsonObject = [OPTLYCondition deserializeJSONArray: mutableJsonArray[i] error: &err];
95
-
96
- if (err) {
97
- *error = err;
98
- return nil ;
99
- }
100
-
101
- if (deserializedJsonObject != nil ) {
102
- [subConditions addObjectsFromArray: deserializedJsonObject];
103
- }
84
+ NSError *err = nil ;
85
+ NSArray *deserializedJsonObject = [OPTLYCondition deserializeJSONArray: mutableJsonArray[i] error: &err];
86
+ if (err) {
87
+ *error = err;
88
+ return nil ;
89
+ }
90
+ if (deserializedJsonObject != nil ) {
91
+ [conditions addObjectsFromArray: deserializedJsonObject];
104
92
}
105
- NSObject <OPTLYCondition> *condition = [OPTLYCondition createConditionInstanceOfClass: mutableJsonArray[0 ]
106
- withConditions: subConditions];
107
- return (NSArray <OPTLYCondition *><OPTLYCondition> *)@[condition];
108
93
}
94
+
95
+ // return an (And/Or/Not) Condition handling the base conditions
96
+ NSObject <OPTLYCondition> *condition = [OPTLYCondition createConditionInstanceOfClass: mutableJsonArray[0 ]
97
+ withConditions: conditions];
98
+ return (NSArray <OPTLYCondition *><OPTLYCondition> *)@[condition];
109
99
}
110
100
111
101
// example jsonArray:
112
102
// "[\"and\", [\"or\", \"3468206642\", \"3988293898\"], [\"or\", \"3988293899\", \"3468206646\", \"3468206647\", \"3468206644\", \"3468206643\"]]"
113
103
+ (NSArray <OPTLYCondition> *)deserializeAudienceConditionsJSONArray : (NSArray *)jsonArray
114
104
error : (NSError * __autoreleasing *)error {
115
105
106
+ NSMutableArray *mutableJsonArray = [NSMutableArray new ];
116
107
// need to check if the jsonArray is actually an array, otherwise, something is wrong with the audience condition
117
108
if (![jsonArray isKindOfClass: [NSArray class ]]) {
118
109
NSError *err = [NSError errorWithDomain: OPTLYErrorHandlerMessagesDomain
@@ -123,44 +114,42 @@ @implementation OPTLYCondition
123
114
}
124
115
return nil ;
125
116
}
117
+ if ([jsonArray count ] == 0 ) {
118
+ return (NSArray <OPTLYCondition> *)@[];
119
+ }
126
120
127
- if ([OPTLYAudienceBaseCondition isBaseConditionJSON: jsonArray[1 ]]) { // base case condition
128
-
129
- // generate all base conditions
130
- NSMutableArray <OPTLYCondition> *conditions = (NSMutableArray <OPTLYCondition> *)[[NSMutableArray alloc ] initWithCapacity: (jsonArray.count - 1 )];
131
- for (int i = 1 ; i < jsonArray.count ; i++) {
132
- NSString *audienceId = jsonArray[i];
121
+ mutableJsonArray = [jsonArray mutableCopy ];
122
+ // Should return 'OR' operator in case there is none
123
+ if (![mutableJsonArray[0 ] isEqualToString: OPTLYDatafileKeysAndCondition] && ![mutableJsonArray[0 ] isEqualToString: OPTLYDatafileKeysOrCondition] && ![mutableJsonArray[0 ] isEqualToString: OPTLYDatafileKeysNotCondition]) {
124
+ [mutableJsonArray insertObject: OPTLYDatafileKeysOrCondition atIndex: 0 ];
125
+ }
126
+
127
+ NSMutableArray <OPTLYCondition> *conditions = (NSMutableArray <OPTLYCondition> *)[[NSMutableArray alloc ] initWithCapacity: (mutableJsonArray.count - 1 )];
128
+ for (int i = 1 ; i < mutableJsonArray.count ; i++) {
129
+ if ([OPTLYAudienceBaseCondition isBaseConditionJSON: mutableJsonArray[i]]) {
130
+ // generate base condition
131
+ NSString *audienceId = mutableJsonArray[i];
133
132
OPTLYAudienceBaseCondition *condition = [OPTLYAudienceBaseCondition new ];
134
133
condition.audienceId = audienceId;
135
134
[conditions addObject: condition];
135
+ continue ;
136
136
}
137
137
138
- // return an (And/Or/Not) Condition handling the base conditions
139
- NSObject <OPTLYCondition> *condition = [OPTLYCondition createConditionInstanceOfClass: jsonArray[0 ]
140
- withConditions: conditions];
141
- return (NSArray <OPTLYCondition> *)@[condition];
142
- }
143
- else {
144
-
145
138
// further condition arrays to deserialize
146
- NSMutableArray <OPTLYCondition> *subConditions = (NSMutableArray <OPTLYCondition> *)[[NSMutableArray alloc ] initWithCapacity: (jsonArray.count - 1 )];
147
- for (int i = 1 ; i < jsonArray.count ; i++) {
139
+ if ([mutableJsonArray[i] isKindOfClass: [NSArray class ]]) {
148
140
NSError *err = nil ;
149
- NSArray *deserializedJsonObject = [OPTLYCondition deserializeAudienceConditionsJSONArray: jsonArray [i] error: &err];
150
-
141
+ NSArray *_tmpConditions = [OPTLYCondition deserializeAudienceConditionsJSONArray: ( NSArray *)mutableJsonArray [i] error: &err];
142
+ [conditions addObject: [_tmpConditions firstObject ]];
151
143
if (err) {
152
144
*error = err;
153
145
return nil ;
154
146
}
155
-
156
- if (deserializedJsonObject != nil ) {
157
- [subConditions addObjectsFromArray: deserializedJsonObject];
158
- }
159
147
}
160
- NSObject <OPTLYCondition> *condition = [OPTLYCondition createConditionInstanceOfClass: jsonArray[0 ]
161
- withConditions: subConditions];
162
- return (NSArray <OPTLYCondition> *)@[condition];
163
148
}
149
+ // return an (And/Or/Not) Condition handling the base conditions
150
+ NSObject <OPTLYCondition> *condition = [OPTLYCondition createConditionInstanceOfClass: mutableJsonArray[0 ]
151
+ withConditions: conditions];
152
+ return (NSArray <OPTLYCondition> *)@[condition];
164
153
}
165
154
166
155
+ (NSObject <OPTLYCondition> *)createConditionInstanceOfClass : (NSString *)conditionClass withConditions : (NSArray <OPTLYCondition> *)conditions {
0 commit comments