diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-03-27 14:11:19 -0400 |
---|---|---|
committer | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-03-28 19:40:08 -0400 |
commit | ddc77692f4c3c4dd6f2348d5828934520dd2ad59 (patch) | |
tree | 2b1f1c57890c19acb9210ad3b07527d9d79149a5 | |
parent | d84eede5729cef5778370072c1f3023ca29df90d (diff) | |
download | qtlocation-mapboxgl-ddc77692f4c3c4dd6f2348d5828934520dd2ad59.tar.gz |
[ios, macos] Add MGL_SWITCH expression operator.
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 41 | ||||
-rw-r--r-- | platform/darwin/src/NSPredicate+MGLAdditions.h | 4 | ||||
-rw-r--r-- | platform/darwin/src/NSPredicate+MGLAdditions.mm | 31 | ||||
-rw-r--r-- | platform/darwin/test/MGLExpressionTests.mm | 4 |
4 files changed, 33 insertions, 47 deletions
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index 776aed642b..23ba0d87fe 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -66,6 +66,7 @@ // with an explicit and implicit first argument. INSTALL_CONTROL_STRUCTURE(MGL_LET); INSTALL_CONTROL_STRUCTURE(MGL_MATCH); + INSTALL_CONTROL_STRUCTURE(MGL_SWITCH); INSTALL_CONTROL_STRUCTURE(MGL_FUNCTION); #undef INSTALL_AFTERMARKET_FN @@ -118,6 +119,29 @@ } /** + A placeholder for a method that evaluates an expression and returns the matching element. + */ +- (id)MGL_SWITCH:(id)firstCondition, ... { + va_list argumentList; + va_start(argumentList, firstCondition); + + for (id eachExpression = firstCondition; eachExpression; eachExpression = va_arg(argumentList, id)) { + if ([eachExpression isKindOfClass:[NSComparisonPredicate class]]) { + id valueExpression = va_arg(argumentList, id); + if ([eachExpression evaluateWithObject:nil]) { + return valueExpression; + } + } else { + return eachExpression; + } + } + va_end(argumentList); + + return nil; +} + + +/** A placeholder for a catch-all method that evaluates an arbitrary number of arguments as an expression according to the Mapbox Style Specification’s expression language. @@ -610,21 +634,19 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { } else if ([op isEqualToString:@"var"]) { return [NSExpression expressionForVariable:argumentObjects.firstObject]; } else if ([op isEqualToString:@"case"]) { - NSArray *caseExpressions = argumentObjects; - NSExpression *firstConditional = [NSExpression expressionWithFormat:@"%@", [NSPredicate mgl_predicateWithJSONObject:caseExpressions[0]]]; NSMutableArray *arguments = [NSMutableArray array]; - for (NSUInteger index = 1; index < caseExpressions.count; index++) { - if ([caseExpressions[index] isKindOfClass:[NSArray class]]) { - NSPredicate *conditional = [NSPredicate mgl_predicateWithJSONObject:caseExpressions[index]]; + for (NSUInteger index = 0; index < argumentObjects.count; index++) { + if ([argumentObjects[index] isKindOfClass:[NSArray class]]) { + NSPredicate *conditional = [NSPredicate mgl_predicateWithJSONObject:argumentObjects[index]]; NSExpression *argument = [NSExpression expressionWithFormat:@"%@", conditional]; [arguments addObject:argument]; } else { - [arguments addObject:[NSExpression mgl_expressionWithJSONObject:caseExpressions[index]]]; + [arguments addObject:[NSExpression mgl_expressionWithJSONObject:argumentObjects[index]]]; } } - return [NSExpression expressionForFunction:firstConditional selectorName:@"mgl_case:" arguments:arguments]; + return [NSExpression expressionForFunction:@"MGL_SWITCH" arguments:arguments]; } else if ([op isEqualToString:@"match"]) { NSMutableArray *optionsArray = [NSMutableArray array]; NSEnumerator *optionsEnumerator = argumentObjects.objectEnumerator; @@ -829,9 +851,8 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { }]; [expressionObject addObject:self.operand.mgl_jsonExpressionObject]; return expressionObject; - } else if ([function isEqualToString:@"mgl_case:"]) { - NSPredicate *firstConditional = (NSPredicate *)self.operand.constantValue; - NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"case", firstConditional.mgl_jsonExpressionObject, nil]; + } else if ([function isEqualToString:@"MGL_SWITCH"]) { + NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"case", nil]; for (NSExpression *option in self.arguments) { if ([option respondsToSelector:@selector(constantValue)] && [option.constantValue isKindOfClass:[NSComparisonPredicate class]]) { diff --git a/platform/darwin/src/NSPredicate+MGLAdditions.h b/platform/darwin/src/NSPredicate+MGLAdditions.h index a67c6ca005..89e9e65c64 100644 --- a/platform/darwin/src/NSPredicate+MGLAdditions.h +++ b/platform/darwin/src/NSPredicate+MGLAdditions.h @@ -16,8 +16,4 @@ @property (nonatomic, readonly) id mgl_jsonExpressionObject; -- (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 1a7bb30a92..63c8307803 100644 --- a/platform/darwin/src/NSPredicate+MGLAdditions.mm +++ b/platform/darwin/src/NSPredicate+MGLAdditions.mm @@ -324,35 +324,4 @@ NSArray *MGLSubpredicatesWithJSONObjects(NSArray *objects) { return nil; } -- (id)mgl_case:(id)firstValue, ... { - - if ([self evaluateWithObject:nil]) { - return firstValue; - } - - id eachExpression; - va_list argumentList; - va_start(argumentList, firstValue); - - while ((eachExpression = va_arg(argumentList, id))) { - if ([eachExpression isKindOfClass:[NSComparisonPredicate class]]) { - id valueExpression = va_arg(argumentList, id); - if ([eachExpression evaluateWithObject:nil]) { - return valueExpression; - } - } else { - return eachExpression; - } - } - va_end(argumentList); - - 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 5fb36ccbb9..bfb1a2edc5 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -660,7 +660,7 @@ using namespace std::string_literals; - (void)testConditionalExpressionObject { { - NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(%@, 'mgl_case:', %@, %@)", + NSExpression *expression = [NSExpression expressionWithFormat:@"MGL_SWITCH(%@, %@, %@)", [NSExpression expressionWithFormat:@"%@", [NSPredicate predicateWithFormat:@"1 = 2"]], MGLConstantExpression(@YES), MGLConstantExpression(@NO)]; @@ -670,7 +670,7 @@ using namespace std::string_literals; XCTAssertEqualObjects([expression expressionValueWithObject:nil context:nil], @NO); } { - NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(%@, 'mgl_case:', %@, %@, %@, %@)", + NSExpression *expression = [NSExpression expressionWithFormat:@"MGL_SWITCH(%@, %@, %@, %@, %@)", [NSExpression expressionWithFormat:@"%@", [NSPredicate predicateWithFormat:@"1 = 2"]], MGLConstantExpression(@YES), [NSExpression expressionWithFormat:@"%@", [NSPredicate predicateWithFormat:@"1 = 1"]], |