-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLOJSONFormatSupport.j
343 lines (285 loc) · 13.6 KB
/
LOJSONFormatSupport.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
@import <Foundation/CPCompoundPredicate.j>
@import <Foundation/CPComparisonPredicate.j>
@import <Foundation/CPExpression.j>
LOJSONUnsupportedPredicateOperatorException = @"LOJSONUnsupportedPredicateOperatorException";
LOJSONInvalidExpressionValueException = @"LOJSONInvalidExpressionValueException";
LOJSONUnsupportedExpressionTypeException = @"LOJSONUnsupportedExpressionTypeException";
LOJSONUnsupportedExpressionValueException = @"LOJSONUnsupportedExpressionValueException";
@implementation CPPredicate (LOJSONFormatSupport)
+ (CPPredicate)predicateFromLOJSONFormat:(JSON)someJSON {
if ([LOJSONFormatSupportGetComparisonOperatorMap() isSupportedOperatorString:someJSON.o]) {
return [CPComparisonPredicate predicateFromLOJSONFormat:someJSON];
} else {
return [CPCompoundPredicate predicateFromLOJSONFormat:someJSON];
}
}
@end
@implementation CPComparisonPredicate (LOJSONFormatSupport)
+ (CPPredicate)predicateFromLOJSONFormat:(JSON)someJSON {
var opType = [LOJSONFormatSupportGetComparisonOperatorMap() getTypeFromStringOrRaise:someJSON.o];
var leftExpression = [CPExpression expressionFromLOJSONFormat:{"type": someJSON.lt, "value": someJSON.l}];
var rightExpression = [CPExpression expressionFromLOJSONFormat:{"type": someJSON.rt, "value": someJSON.r}];
var options = 0;
if (someJSON.c) options = CPCaseInsensitivePredicateOption;
return [[CPComparisonPredicate alloc] initWithLeftExpression:leftExpression rightExpression:rightExpression modifier:CPDirectPredicateModifier type:opType options:options];
}
- (JSON)LOJSONFormat {
var result = {};
result["o"] = [LOJSONFormatSupportGetComparisonOperatorMap() getStringFromTypeOrRaise:[self predicateOperatorType]];
var expressionJSON = [[self leftExpression] LOJSONFormat];
result["l"] = expressionJSON.value;
result["lt"] = expressionJSON.type;
expressionJSON = [[self rightExpression] LOJSONFormat];
result["r"] = expressionJSON.value;
result["rt"] = expressionJSON.type;
if ([self options] & CPCaseInsensitivePredicateOption)
result["c"] = true;
return result;
}
@end
@implementation CPCompoundPredicate (LOJSONFormatSupport)
+ (CPPredicate)predicateFromLOJSONFormat:(JSON)someJSON {
var opType = [LOJSONFormatSupportGetCompoundOperatorMap() getTypeFromStringOrRaise:someJSON.o];
var subpreds = [];
for (var i=0; i<someJSON.p.length; i++) {
var predJson = someJSON.p[i];
var subpred = [CPPredicate predicateFromLOJSONFormat:predJson];
[subpreds addObject:subpred];
}
return [[CPCompoundPredicate alloc] initWithType:opType subpredicates:subpreds];
}
- (JSON)LOJSONFormat {
var result = {};
result["o"] = [LOJSONFormatSupportGetCompoundOperatorMap() getStringFromTypeOrRaise:[self compoundPredicateType]];
result.p = [];
for (var i=0; i < [[self subpredicates] count]; i++) {
var subpred = [[self subpredicates] objectAtIndex:i];
[result.p addObject:[subpred LOJSONFormat]];
}
return result;
}
@end
@implementation CPExpression (LOJSONFormatSupport)
function _BOPValidValueJSONOrRaise(someJSON) {
var missing = [CPMutableArray array];
if (!someJSON.type) [missing addObject:@"type"];
if (someJSON.value === undefined) [missing addObject:@"value"];
if ([missing count] > 0)
[CPException raise:LOJSONInvalidExpressionValueException reason:@"Expression format missing " + missing.join(@" and ")];
return someJSON;
}
+ (CPExpression)expressionFromLOJSONFormat:(JSON)someJSON {
var json = _BOPValidValueJSONOrRaise(someJSON);
var type = json.type;
var value = json.value;
if ([type isEqual:@"array"]) {
if (!value || !value.isa || ![value isKindOfClass:[CPArray class]])
[CPException raise:LOJSONInvalidExpressionValueException reason:@"Unable to parse expression of type '" + type + "'. Value must be an array: '" + value + "'"];
var subexprs = [CPMutableArray array];
try {
for (var i=0; i<[value count]; i++) {
var subValue = value[i];
[subexprs addObject:[self _atomExpressionFromLOJSONFormat:subValue]];
}
}
catch (exception) {
[CPException raise:LOJSONInvalidExpressionValueException reason:@"Unable to parse expression of type '" + type + "'. Invalid sub-value: "+ [exception reason]];
}
return [CPExpression expressionForAggregate:subexprs];
}
return [self _atomExpressionFromLOJSONFormat:json];
}
+ (CPExpression)_atomExpressionFromLOJSONFormat:(JSON)someJSON {
_BOPValidValueJSONOrRaise(someJSON);
var validNumberOrRaise = function(value,type) {
var v = (value === null) ? null : Number(value);
if (v === null || isNaN(v))
[CPException raise:LOJSONInvalidExpressionValueException reason:@"Unable to parse expression of type '" + type + "'. Value must be a valid number: '" + v + "'"];
return v;
};
var validBooleanOrRaise = function(value,type) {
if (typeof value == "boolean") return value;
if (value == "true") return true;
if (value == "false") return false;
[CPException raise:LOJSONInvalidExpressionValueException reason:@"Unable to parse expression of type '" + type + "'. Value must be a valid boolean: '" + value + "'"];
};
var validStringOrRaise = function(value,type) {
if (value === null)
[CPException raise:LOJSONInvalidExpressionValueException reason:@"Unable to parse expression of type '" + type + "'. Value must not be null"];
return String(value);
}
var validKeyPathOrRaise = function(value,type) {
validStringOrRaise(value,type);
if (value == "")
[CPException raise:LOJSONInvalidExpressionValueException reason:@"Unable to parse expression of type '" + type + "'. Value must not be empty"];
return String(value);
}
if (someJSON.type == "string") {
return [CPExpression expressionForConstantValue:validStringOrRaise(someJSON.value, someJSON.type)];
} else if (someJSON.type == "number") {
return [CPExpression expressionForConstantValue:validNumberOrRaise(someJSON.value, someJSON.type)];
} else if (someJSON.type == "bool") {
return [CPExpression expressionForConstantValue:validBooleanOrRaise(someJSON.value, someJSON.type)];
} else if (someJSON.type == "keyPath") {
return [CPExpression expressionForKeyPath:validKeyPathOrRaise(someJSON.value, someJSON.type)];
} else if (someJSON.type == "datetime") {
return [CPExpression expressionForConstantValue:[CPDate dateWithTimeIntervalSinceReferenceDate:validNumberOrRaise(someJSON.value, someJSON.type)]];
} else if (someJSON.type == "null") {
return [CPExpression expressionForConstantValue:nil];
}
[CPException raise:LOJSONUnsupportedExpressionTypeException reason:@"Unsupported expression type '" + someJSON.type + "'"];
}
- (JSON)LOJSONFormat {
var myType = [self expressionType];
var myValue = (myType == CPConstantValueExpressionType) ? [self constantValue] : (myType == CPAggregateExpressionType) ? [self collection] : undefined;
if (myType == CPAggregateExpressionType || (myType == CPConstantValueExpressionType && myValue && myValue.isa && [myValue isKindOfClass:[CPArray class]])) {
var values = [CPMutableArray array];
try {
for (var i=0; i<[myValue count]; i++) {
var subexpr = myValue[i];
if (!subexpr.isa || ![subexpr isKindOfClass:[CPExpression class]]) {
subexpr = [CPExpression expressionForConstantValue:subexpr];
}
[values addObject:[subexpr _atomLOJSONFormat]];
}
} catch (exception) {
[CPException raise:LOJSONUnsupportedExpressionValueException reason:@"Unsupported value of aggregate expression: " + [exception reason]];
}
return {type:"array", value:values};
}
return [self _atomLOJSONFormat];
}
- (JSON)_atomLOJSONFormat {
var type = undefined;
var value = undefined;
switch ([self expressionType]) {
case CPConstantValueExpressionType:
value = [self constantValue];
if (value === null || value === [CPNull null]) {
value = nil;
type = @"null";
} else if (typeof value == "string") {
type = "string";
} else if (typeof value == "number") {
type = "number";
} else if (typeof value == "boolean") {
type = "bool";
} else if (value != null && value.isa && [value isKindOfClass:[CPDate class]]) {
value = Number([value timeIntervalSinceReferenceDate]);
type = "datetime";
} else {
[CPException raise:LOJSONUnsupportedExpressionValueException reason:@"Unsupported type '" + typeof value + "' for constant value expression"];
}
break;
case CPKeyPathExpressionType:
value = [self keyPath];
if (value === undefined) [CPException raise:LOJSONUnsupportedExpressionValueException reason:@"Key path expression must not be undefined"];
if (value === null) [CPException raise:LOJSONUnsupportedExpressionValueException reason:@"Key path expression must not be null"];
if (value == @"") [CPException raise:LOJSONUnsupportedExpressionValueException reason:@"Key path expression must not be empty"];
type = "keyPath";
break;
default:
[CPException raise:LOJSONUnsupportedExpressionTypeException reason:@"Unsupported expression type '" + [self expressionType] + "'"];
}
return { @"type": type, @"value": value };
}
@end
@implementation LOJSONFormatTypeStringMap : CPObject {
CPArray typesAndStrings;
CPSet supportedTypes;
JSON typeStringMap;
JSON stringTypeMap;
}
+ (id)mapForComparisonOperators {
var pairs = [
[CPNotEqualToPredicateOperatorType, @"!=" /* also @"<>" */],
[CPLessThanOrEqualToPredicateOperatorType, @"<=" /* also @"=<" */],
[CPGreaterThanOrEqualToPredicateOperatorType, @">=" /* also @"=>" */],
[CPLessThanPredicateOperatorType, @"<"],
[CPGreaterThanPredicateOperatorType, @">"],
[CPEqualToPredicateOperatorType, @"==" /* also @"="*/],
[CPLikePredicateOperatorType, @"like"],
[CPInPredicateOperatorType, @"in"],
[CPBetweenPredicateOperatorType, @"between"],
[CPMatchesPredicateOperatorType, @"MATCHES"],
[CPBeginsWithPredicateOperatorType, @"BEGINSWITH"],
[CPEndsWithPredicateOperatorType, @"ENDSWITH"],
[CPContainsPredicateOperatorType, @"CONTAINS"],
];
var supported = [CPSet setWithArray:[
CPNotEqualToPredicateOperatorType,
CPLessThanOrEqualToPredicateOperatorType,
CPGreaterThanOrEqualToPredicateOperatorType,
CPLessThanPredicateOperatorType,
CPGreaterThanPredicateOperatorType,
CPEqualToPredicateOperatorType,
CPLikePredicateOperatorType,
CPInPredicateOperatorType,
CPBetweenPredicateOperatorType,
]];
return [[LOJSONFormatTypeStringMap alloc] initWithTypesAndStrings:pairs supportedTypes:supported];
}
+ (id)mapForCompoundOperators {
var pairs = [
[CPAndPredicateType, @"and"],
[CPOrPredicateType, @"or"],
[CPNotPredicateType, @"not"],
];
var supported = [CPSet setWithArray:[
CPAndPredicateType,
CPOrPredicateType,
CPNotPredicateType,
]];
return [[LOJSONFormatTypeStringMap alloc] initWithTypesAndStrings:pairs supportedTypes:supported];
}
- (id)initWithTypesAndStrings:(CPArray)typeStringPairs supportedTypes:(CPArray)someTypes {
if (!(self = [super init])) return nil;
typesAndStrings = typeStringPairs;
supportedTypes = someTypes;
[self generateMaps];
return self;
}
- (void)generateMaps {
typeStringMap = {};
stringTypeMap = {};
for (var i=0; i < typesAndStrings.length; i++) {
var typeAndString = typesAndStrings[i];
typeStringMap[typeAndString[0]] = typeAndString[1];
stringTypeMap[typeAndString[1]] = typeAndString[0];
}
}
- (CPNumber)getTypeFromStringOrRaise:(CPString)aString {
var t = stringTypeMap[aString];
if (t === undefined || ![supportedTypes containsObject:t]) {
[self raiseForUnsupportedOperator:aString];
}
return t;
}
- (CPString)getStringFromTypeOrRaise:(CPNumber)aType {
var s = typeStringMap[aType];
if (s === undefined || ![supportedTypes containsObject:aType]) {
[self raiseForUnsupportedOperator:s || aType];
}
return s;
}
- (BOOL)isSupportedOperatorString:(id)aString {
var t = stringTypeMap[aString];
if (t === undefined || ![supportedTypes containsObject:t]) return NO;
return YES;
}
- (void)raiseForUnsupportedOperator:(id)anOperator {
[CPException raise:LOJSONUnsupportedPredicateOperatorException reason:@"Unsupported predicate operator '" + anOperator + "'"];
}
@end
var LOJSONFormatSupportComparisonOperatorMap = nil;
var LOJSONFormatSupportCompoundOperatorMap = nil;
function LOJSONFormatSupportGetComparisonOperatorMap() {
if (!LOJSONFormatSupportComparisonOperatorMap)
LOJSONFormatSupportComparisonOperatorMap = [LOJSONFormatTypeStringMap mapForComparisonOperators];
return LOJSONFormatSupportComparisonOperatorMap;
}
function LOJSONFormatSupportGetCompoundOperatorMap() {
if (!LOJSONFormatSupportCompoundOperatorMap)
LOJSONFormatSupportCompoundOperatorMap = [LOJSONFormatTypeStringMap mapForCompoundOperators];
return LOJSONFormatSupportCompoundOperatorMap;
}