summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Guerra <fabian.guerra@mapbox.com>2018-03-28 21:12:03 -0400
committerFabian Guerra <fabian.guerra@mapbox.com>2018-03-28 21:12:03 -0400
commit0a9b7a0d74fa0768c314ed0f0bcc3d4d4f796bcb (patch)
tree9e0c34ea562a7863fc352bb2276c85258f196289
parent79844eb330e2c21b6d6967a9d19b1eceba64bd6c (diff)
downloadqtlocation-mapboxgl-0a9b7a0d74fa0768c314ed0f0bcc3d4d4f796bcb.tar.gz
[ios, macos] Add aftermarket function to 'has' operator.
-rw-r--r--platform/darwin/src/NSExpression+MGLAdditions.mm35
-rw-r--r--platform/darwin/test/MGLExpressionTests.mm11
2 files changed, 36 insertions, 10 deletions
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm
index 4f5739aaa1..a5676529fc 100644
--- a/platform/darwin/src/NSExpression+MGLAdditions.mm
+++ b/platform/darwin/src/NSExpression+MGLAdditions.mm
@@ -61,6 +61,7 @@
INSTALL_METHOD(mgl_interpolate:withCurveType:parameters:stops:);
INSTALL_METHOD(mgl_step:from:stops:);
INSTALL_METHOD(mgl_coalesce:);
+ INSTALL_METHOD(mgl_hasProperty:properties:);
// Install functions that resemble control structures, taking arbitrary
// numbers of arguments. Vararg aftermarket functions need to be declared
@@ -110,6 +111,15 @@
}
/**
+ A placeholder for a method that evaluates a coalesce expression.
+ */
+- (id)mgl_hasProperty:(id)element properties:(id)properties {
+ [NSException raise:NSInvalidArgumentException
+ format:@"Has expressions lack underlying Objective-C implementations."];
+ return nil;
+}
+
+/**
A placeholder for a method that evaluates an expression based on an arbitrary
number of variable names and assigned expressions.
*/
@@ -586,7 +596,8 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects(argumentObjects);
NSExpression *operand = argumentObjects.count > 1 ? subexpressions[1] : [NSExpression expressionWithFormat:@"self"];
NSExpression *property = subexpressions.firstObject;
- return [NSExpression expressionWithFormat:@"FUNCTION(%@, 'mgl_has:', %@)", operand, property];
+
+ return [NSExpression expressionForFunction:@"mgl_hasProperty:properties:" arguments:@[property, operand]];
} else if ([op isEqualToString:@"interpolate"]) {
NSArray *interpolationOptions = argumentObjects.firstObject;
NSString *curveType = interpolationOptions.firstObject;
@@ -837,12 +848,9 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
return @[@"to-string", self.operand.mgl_jsonExpressionObject];
} else if ([function isEqualToString:@"noindex:"]) {
return self.arguments.firstObject.mgl_jsonExpressionObject;
- } else if ([function isEqualToString:@"mgl_has:"]) {
- NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"has", self.arguments[0].mgl_jsonExpressionObject, nil];
- if (self.operand.expressionType != NSEvaluatedObjectExpressionType) {
- [expressionObject addObject:self.operand.mgl_jsonExpressionObject];
- }
- return expressionObject;
+ } else if ([function isEqualToString:@"mgl_hasProperty:properties:"] ||
+ [function isEqualToString:@"mgl_has:"]) {
+ return self.mgl_jsonHasExpressionObject;
} else if ([function isEqualToString:@"mgl_interpolate:withCurveType:parameters:stops:"]
|| [function isEqualToString:@"mgl_interpolateWithCurveType:parameters:stops:"]) {
return self.mgl_jsonInterpolationExpressionObject;
@@ -1066,4 +1074,17 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
return expressionObject;
}
+- (id)mgl_jsonHasExpressionObject {
+ BOOL isAftermarketFunction = [self.function isEqualToString:@"mgl_hasProperty:properties:"];
+ NSExpression *operand = isAftermarketFunction ? self.arguments[1] : self.operand;
+
+ NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"has", self.arguments[0].mgl_jsonExpressionObject, nil];
+ if (operand.expressionType != NSEvaluatedObjectExpressionType) {
+ [expressionObject addObject:operand.mgl_jsonExpressionObject];
+ }
+ return expressionObject;
+
+ return expressionObject;
+}
+
@end
diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm
index 20209f305e..9c6a454943 100644
--- a/platform/darwin/test/MGLExpressionTests.mm
+++ b/platform/darwin/test/MGLExpressionTests.mm
@@ -718,19 +718,24 @@ using namespace std::string_literals;
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
}
{
- NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(self, 'mgl_has:', 'x')"];
+ NSExpression *expression = [NSExpression expressionForFunction:@"mgl_hasProperty:properties:" arguments:@[[NSExpression expressionForConstantValue:@"x"],
+ [NSExpression expressionForEvaluatedObject]]];
+ NSExpression *compatibilityExpression = [NSExpression expressionWithFormat:@"FUNCTION(self, 'mgl_has:', 'x')"];
NSArray *jsonExpression = @[@"has", @"x"];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
+ XCTAssertEqualObjects(compatibilityExpression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
}
{
- NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(%@, 'mgl_has:', 'x')", [NSExpression expressionForConstantValue:@{@"x": MGLConstantExpression(@0)}]];
+ NSExpression *expression = [NSExpression expressionForFunction:@"mgl_hasProperty:properties:" arguments:@[[NSExpression expressionForConstantValue:@"x"],
+ [NSExpression expressionForConstantValue:@{@"x": MGLConstantExpression(@0)}]]];
NSArray *jsonExpression = @[@"has", @"x", @[@"literal", @{@"x": @0}]];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
}
{
- NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION($mgl_featureProperties, 'mgl_has:', 'x')"];
+ NSExpression *expression = [NSExpression expressionForFunction:@"mgl_hasProperty:properties:" arguments:@[[NSExpression expressionForConstantValue:@"x"],
+ [NSExpression expressionForVariable:@"mgl_featureProperties"]]];
NSArray *jsonExpression = @[@"has", @"x", @[@"properties"]];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);