summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Guerra <fabian.guerra@mapbox.com>2018-03-23 12:06:54 -0400
committerFabian Guerra <fabian.guerra@mapbox.com>2018-03-23 12:06:54 -0400
commit90f984cc5db3d36ad5c41ada30c5d41a9814d0cc (patch)
tree8749e564856aeb155e987f0722518ee8afccb69a
parente75bd22380d2f4fdcd6d0c20e1274cc927892ddc (diff)
downloadqtlocation-mapboxgl-upstream/fabian-coalesce-match-operators-11009.tar.gz
[ios, macos] Match operator function implementation refactor.upstream/fabian-coalesce-match-operators-11009
-rw-r--r--platform/darwin/src/NSExpression+MGLAdditions.mm36
-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.mm2
-rw-r--r--platform/darwin/test/MGLExpressionTests.mm9
5 files changed, 25 insertions, 26 deletions
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm
index 933268efe4..a60b14b35d 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)
@@ -478,23 +484,17 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
} else if ([op isEqualToString:@"match"]) {
NSExpression *operand = [NSExpression mgl_expressionWithJSONObject:argumentObjects[0]];
NSArray *matchOptions = [argumentObjects subarrayWithRange:NSMakeRange(1, argumentObjects.count - 1)];
- NSExpression *defaultOption;
- if (matchOptions.count % 2) {
- defaultOption = [NSExpression mgl_expressionWithJSONObject:matchOptions.lastObject];
- matchOptions = [matchOptions subarrayWithRange:NSMakeRange(0, matchOptions.count - 1)];
- }
+
NSMutableArray *optionsArray = [NSMutableArray array];
NSEnumerator *optionsEnumerator = matchOptions.objectEnumerator;
- while (NSNumber *key = optionsEnumerator.nextObject) {
- NSMutableDictionary *option = [NSMutableDictionary dictionaryWithCapacity:1];
- NSExpression *valueExpression = optionsEnumerator.nextObject;
- option[key] = [NSExpression mgl_expressionWithJSONObject:valueExpression];
+ while (id object = optionsEnumerator.nextObject) {
+ NSExpression *option = [NSExpression mgl_expressionWithJSONObject:object];
[optionsArray addObject:option];
}
- NSExpression *optionsExpression = [NSExpression expressionForConstantValue:optionsArray];
+
return [NSExpression expressionForFunction:operand
- selectorName:@"mgl_matchWithOptions:default:"
- arguments:@[optionsExpression, defaultOption]];
+ selectorName:@"mgl_match:"
+ arguments:optionsArray];
} else if ([op isEqualToString:@"coalesce"]) {
NSMutableArray *expressions = [NSMutableArray array];
for (id operand in argumentObjects) {
@@ -693,17 +693,13 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
}];
[expressionObject addObject:self.operand.mgl_jsonExpressionObject];
return expressionObject;
- } else if ([function isEqualToString:@"mgl_matchWithOptions:default:"]) {
+ } else if ([function isEqualToString:@"mgl_match:"]) {
NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"match", self.operand.mgl_jsonExpressionObject, nil];
- NSArray *optionsArray = self.arguments[0].constantValue;
- for (NSDictionary<id, NSExpression*> *options in optionsArray) {
- for (NSNumber *key in options.allKeys) {
- [expressionObject addObject:key];
- [expressionObject addObject:[options[key] mgl_jsonExpressionObject]];
- }
+
+ for (NSExpression *option in self.arguments) {
+ [expressionObject addObject:option.mgl_jsonExpressionObject];
}
- [expressionObject addObject:[self.arguments[1] mgl_jsonExpressionObject]];
return expressionObject;
} else if ([function isEqualToString:@"mgl_coalesce"]) {
NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"coalesce", nil];
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 d50029186c..6211e19e8a 100644
--- a/platform/darwin/src/NSPredicate+MGLAdditions.h
+++ b/platform/darwin/src/NSPredicate+MGLAdditions.h
@@ -16,6 +16,6 @@
@property (nonatomic, readonly) id mgl_jsonExpressionObject;
-- (id)mgl_matchWithOptions:(NSArray *)options default:(id)minimum;
+- (id)mgl_match:(NSExpression *)firstCase, ...;
@end
diff --git a/platform/darwin/src/NSPredicate+MGLAdditions.mm b/platform/darwin/src/NSPredicate+MGLAdditions.mm
index 82a3c249a2..afd2bae9e1 100644
--- a/platform/darwin/src/NSPredicate+MGLAdditions.mm
+++ b/platform/darwin/src/NSPredicate+MGLAdditions.mm
@@ -324,7 +324,7 @@ NSArray *MGLSubpredicatesWithJSONObjects(NSArray *objects) {
return nil;
}
-- (id)mgl_matchWithOptions:(NSArray *)options default:(id)minimum {
+- (id)mgl_match:(NSExpression *)firstCase, ... {
[NSException raise:NSInvalidArgumentException
format:@"Match expressions lack underlying Objective-C implementations."];
return nil;
diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm
index 352d661e08..e630cf931d 100644
--- a/platform/darwin/test/MGLExpressionTests.mm
+++ b/platform/darwin/test/MGLExpressionTests.mm
@@ -574,15 +574,16 @@ using namespace std::string_literals;
- (void)testMatchExpressionObject {
{
- NSArray *options = @[@{@1 : MGLConstantExpression(@"one")}, @{@0 : MGLConstantExpression(@"zero")}];
- NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(2 - 1, 'mgl_matchWithOptions:default:', %@, 'default')", options];
+ 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);
}
{
- NSArray *options = @[@{@1 : MGLConstantExpression(@"one")}];
- NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(2 * 1, 'mgl_matchWithOptions:default:', %@, 'default')", options];
+ 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);