summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Guerra Soto <fabian.guerra@mapbox.com>2018-03-26 15:22:54 -0400
committerGitHub <noreply@github.com>2018-03-26 15:22:54 -0400
commit9a663fb4799bedb85ca583d7f23a1a67277d220b (patch)
tree438e3c3fb733428d36bf0c99c7dc88d44027c387
parent9eff2c52fb57c5cb1a1c813d3fb6a307bde12706 (diff)
downloadqtlocation-mapboxgl-9a663fb4799bedb85ca583d7f23a1a67277d220b.tar.gz
[ios, macos] Add match expressions support. (#11464)
* [ios, macos] Add match expressions support. * [ios, macos] Add coalesce operator support. * [ios, macos] Add coalesce operator test. * [ios, macos] Match operator implementation is moved to NSPredicate. * [ios, macos] Match operator function implementation refactor. * [ios, macos] Address merge conflicts.
-rw-r--r--platform/darwin/src/NSExpression+MGLAdditions.mm49
-rw-r--r--platform/darwin/src/NSExpression+MGLPrivateAdditions.h2
-rw-r--r--platform/darwin/src/NSPredicate+MGLAdditions.h2
-rw-r--r--platform/darwin/src/NSPredicate+MGLAdditions.mm6
-rw-r--r--platform/darwin/test/MGLExpressionTests.mm31
5 files changed, 87 insertions, 3 deletions
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm
index bd747dc97e..9797709b7b 100644
--- a/platform/darwin/src/NSExpression+MGLAdditions.mm
+++ b/platform/darwin/src/NSExpression+MGLAdditions.mm
@@ -258,6 +258,12 @@
return [self valueForKeyPath:@"mgl_jsonExpressionObject"];
}
+- (id)mgl_coalesce {
+ [NSException raise:NSInvalidArgumentException
+ format:@"Coalesce expressions lack underlying Objective-C implementations."];
+ return nil;
+}
+
@end
@implementation NSDictionary (MGLExpressionAdditions)
@@ -476,11 +482,31 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
} else {
[arguments addObject:[NSExpression mgl_expressionWithJSONObject:caseExpressions[index]]];
}
-
}
return [NSExpression expressionForFunction:firstConditional selectorName:@"mgl_case:" arguments:arguments];
- } else {
+ } else if ([op isEqualToString:@"match"]) {
+ NSExpression *operand = [NSExpression mgl_expressionWithJSONObject:argumentObjects[0]];
+ NSArray *matchOptions = [argumentObjects subarrayWithRange:NSMakeRange(1, argumentObjects.count - 1)];
+
+ NSMutableArray *optionsArray = [NSMutableArray array];
+ NSEnumerator *optionsEnumerator = matchOptions.objectEnumerator;
+ while (id object = optionsEnumerator.nextObject) {
+ NSExpression *option = [NSExpression mgl_expressionWithJSONObject:object];
+ [optionsArray addObject:option];
+ }
+
+ return [NSExpression expressionForFunction:operand
+ selectorName:@"mgl_match:"
+ arguments:optionsArray];
+ } else if ([op isEqualToString:@"coalesce"]) {
+ NSMutableArray *expressions = [NSMutableArray array];
+ for (id operand in argumentObjects) {
+ [expressions addObject:[NSExpression mgl_expressionWithJSONObject:operand]];
+ }
+
+ return [NSExpression expressionWithFormat:@"FUNCTION(%@, 'mgl_coalesce')", expressions];
+ }else {
[NSException raise:NSInvalidArgumentException
format:@"Expression operator %@ not yet implemented.", op];
}
@@ -674,7 +700,7 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
} else if ([function isEqualToString:@"mgl_case:"]) {
NSPredicate *firstConditional = (NSPredicate *)self.operand.constantValue;
NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"case", firstConditional.mgl_jsonExpressionObject, nil];
-
+
for (NSExpression *option in self.arguments) {
if ([option respondsToSelector:@selector(constantValue)] && [option.constantValue isKindOfClass:[NSComparisonPredicate class]]) {
NSPredicate *predicate = (NSPredicate *)option.constantValue;
@@ -683,6 +709,23 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
[expressionObject addObject:option.mgl_jsonExpressionObject];
}
}
+
+ return expressionObject;
+ } else if ([function isEqualToString:@"mgl_match:"]) {
+ NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"match", self.operand.mgl_jsonExpressionObject, nil];
+
+
+ for (NSExpression *option in self.arguments) {
+ [expressionObject addObject:option.mgl_jsonExpressionObject];
+ }
+
+ return expressionObject;
+ } else if ([function isEqualToString:@"mgl_coalesce"]) {
+ NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"coalesce", nil];
+
+ for (NSExpression *expression in self.operand.constantValue) {
+ [expressionObject addObject:[expression mgl_jsonExpressionObject]];
+ }
return expressionObject;
} else if ([function isEqualToString:@"median:"] ||
diff --git a/platform/darwin/src/NSExpression+MGLPrivateAdditions.h b/platform/darwin/src/NSExpression+MGLPrivateAdditions.h
index 8d1b4d6af5..b16e2625a0 100644
--- a/platform/darwin/src/NSExpression+MGLPrivateAdditions.h
+++ b/platform/darwin/src/NSExpression+MGLPrivateAdditions.h
@@ -54,6 +54,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) id mgl_jsonExpressionObject;
+@property (nonatomic, readonly) id mgl_coalesce;
+
@end
@interface NSDictionary (MGLExpressionAdditions)
diff --git a/platform/darwin/src/NSPredicate+MGLAdditions.h b/platform/darwin/src/NSPredicate+MGLAdditions.h
index cce7561add..a67c6ca005 100644
--- a/platform/darwin/src/NSPredicate+MGLAdditions.h
+++ b/platform/darwin/src/NSPredicate+MGLAdditions.h
@@ -18,4 +18,6 @@
- (id)mgl_case:(id)firstValue, ...;
+- (id)mgl_match:(NSExpression *)firstCase, ...;
+
@end
diff --git a/platform/darwin/src/NSPredicate+MGLAdditions.mm b/platform/darwin/src/NSPredicate+MGLAdditions.mm
index 2d5b646ff2..1a7bb30a92 100644
--- a/platform/darwin/src/NSPredicate+MGLAdditions.mm
+++ b/platform/darwin/src/NSPredicate+MGLAdditions.mm
@@ -349,4 +349,10 @@ NSArray *MGLSubpredicatesWithJSONObjects(NSArray *objects) {
return nil;
}
+- (id)mgl_match:(NSExpression *)firstCase, ... {
+ [NSException raise:NSInvalidArgumentException
+ format:@"Match expressions lack underlying Objective-C implementations."];
+ return nil;
+}
+
@end
diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm
index 3f36a0cf61..ea5a1cd41c 100644
--- a/platform/darwin/test/MGLExpressionTests.mm
+++ b/platform/darwin/test/MGLExpressionTests.mm
@@ -572,6 +572,37 @@ using namespace std::string_literals;
}
}
+- (void)testMatchExpressionObject {
+ {
+ NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(2 - 1, 'mgl_match:', %@, %@, %@, %@, 'default')", MGLConstantExpression(@1),
+ MGLConstantExpression(@"one"),
+ MGLConstantExpression(@0),
+ MGLConstantExpression(@"zero")];
+ NSArray *jsonExpression = @[@"match", @[@"-", @2, @1], @1, @"one", @0, @"zero", @"default"];
+ XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
+ XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
+ }
+ {
+ NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(2 * 1, 'mgl_match:', %@, %@, 'default')", MGLConstantExpression(@1), MGLConstantExpression(@"one")];
+ NSArray *jsonExpression = @[@"match", @[@"*", @2, @1], @1, @"one", @"default"];
+ XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
+ XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
+ }
+}
+
+- (void)testCoalesceExpressionObject {
+ {
+ NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(%@, 'mgl_coalesce')", @[[NSExpression expressionForKeyPath:@"x"],
+ [NSExpression expressionForKeyPath:@"y"],
+ [NSExpression expressionForKeyPath:@"z"],
+ [NSExpression expressionForConstantValue:@0]]];
+ NSArray *jsonExpression = @[@"coalesce", @[@"get", @"x"], @[@"get", @"y"], @[@"get", @"z"], @0];
+ XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
+ XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
+ }
+
+}
+
- (void)testConditionalExpressionObject {
{
NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(%@, 'mgl_case:', %@, %@)",