Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, macos] Add aftermarket function to 'has' operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-guerra committed Mar 29, 2018
1 parent 79844eb commit 0a9b7a0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
35 changes: 28 additions & 7 deletions platform/darwin/src/NSExpression+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ + (void)installFunctions {
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
Expand Down Expand Up @@ -109,6 +110,15 @@ - (id)mgl_coalesce:(NSArray<NSExpression *> *)elements {
return nil;
}

/**
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.
Expand Down Expand Up @@ -586,7 +596,8 @@ + (instancetype)mgl_expressionWithJSONObject:(id)object {
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;
Expand Down Expand Up @@ -837,12 +848,9 @@ - (id)mgl_jsonExpressionObject {
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;
Expand Down Expand Up @@ -1066,4 +1074,17 @@ - (id)mgl_jsonCoalesceExpressionObject {
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
11 changes: 8 additions & 3 deletions platform/darwin/test/MGLExpressionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -718,19 +718,24 @@ - (void)testLookupExpressionObject {
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);
Expand Down

0 comments on commit 0a9b7a0

Please sign in to comment.