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 lookup and feature operators. (#11528)
Browse files Browse the repository at this point in the history
* [ios, macos] Add Expressions 'at' operator.

* [ios, macos] Implement 'has' operator.

* [ios, macos] Implement expressions feature operators.

* [ios, macos] Refactor expressions variable name and 'mgl_has' implementation.

* [ios, macos] Refactor expresssion variable names.

* [ios, macos] Update 'mgl_has' to support non-self operands.
  • Loading branch information
fabian-guerra authored Mar 28, 2018
1 parent fbd6e2b commit 2aaa038
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
45 changes: 45 additions & 0 deletions platform/darwin/src/NSExpression+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ - (id)mgl_jsonExpressionObject {
return expressionObject;
}

- (id)mgl_has:(id)element {
[NSException raise:NSInvalidArgumentException
format:@"Has expressions lack underlying Objective-C implementations."];
return nil;
}

@end

@implementation NSExpression (MGLExpressionAdditions)
Expand All @@ -287,6 +293,12 @@ - (NSExpression *)mgl_expressionWithContext:(NSDictionary<NSString *, NSExpressi
return self;
}

- (id)mgl_has:(id)element {
[NSException raise:NSInvalidArgumentException
format:@"Has expressions lack underlying Objective-C implementations."];
return nil;
}

@end

@implementation NSExpression (MGLAdditions)
Expand Down Expand Up @@ -406,6 +418,16 @@ + (instancetype)mgl_expressionWithJSONObject:(id)object {
NSExpression *operand = subexpressions.firstObject;
subexpressions = [subexpressions subarrayWithRange:NSMakeRange(1, subexpressions.count - 1)];
return [NSExpression expressionForFunction:operand selectorName:@"stringByAppendingString:" arguments:subexpressions];
} else if ([op isEqualToString:@"at"]) {
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects(argumentObjects);
NSExpression *index = subexpressions.firstObject;
NSExpression *operand = subexpressions[1];
return [NSExpression expressionForFunction:@"objectFrom:withIndex:" arguments:@[operand, index]];
} else if ([op isEqualToString:@"has"]) {
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];
} else if ([op isEqualToString:@"interpolate"]) {
NSArray *interpolationOptions = argumentObjects.firstObject;
NSString *curveType = interpolationOptions.firstObject;
Expand Down Expand Up @@ -456,6 +478,12 @@ + (instancetype)mgl_expressionWithJSONObject:(id)object {
return [NSExpression expressionForVariable:@"zoomLevel"];
} else if ([op isEqualToString:@"heatmap-density"]) {
return [NSExpression expressionForVariable:@"heatmapDensity"];
} else if ([op isEqualToString:@"geometry-type"]) {
return [NSExpression expressionForVariable:@"mgl_geometryType"];
} else if ([op isEqualToString:@"id"]) {
return [NSExpression expressionForVariable:@"mgl_featureIdentifier"];
} else if ([op isEqualToString:@"properties"]) {
return [NSExpression expressionForVariable:@"mgl_featureProperties"];
} else if ([op isEqualToString:@"let"]) {
NSExpression *operand = [NSExpression mgl_expressionWithJSONObject:argumentObjects.lastObject];
NSArray *bindingObjects = [argumentObjects subarrayWithRange:NSMakeRange(0, argumentObjects.count - 1)];
Expand Down Expand Up @@ -544,6 +572,15 @@ - (id)mgl_jsonExpressionObject {
if ([self.variable isEqualToString:@"zoomLevel"]) {
return @[@"zoom"];
}
if ([self.variable isEqualToString:@"mgl_geometryType"]) {
return @[@"geometry-type"];
}
if ([self.variable isEqualToString:@"mgl_featureIdentifier"]) {
return @[@"id"];
}
if ([self.variable isEqualToString:@"mgl_featureProperties"]) {
return @[@"properties"];
}
return @[@"var", self.variable];
}

Expand Down Expand Up @@ -639,6 +676,8 @@ - (id)mgl_jsonExpressionObject {
} else if ([function isEqualToString:@"stringByAppendingString:"]) {
NSArray *arguments = self.arguments.mgl_jsonExpressionObject;
return [@[@"concat", self.operand.mgl_jsonExpressionObject] arrayByAddingObjectsFromArray:arguments];
} else if ([function isEqualToString:@"objectFrom:withIndex:"]) {
return @[@"at", self.arguments[1].mgl_jsonExpressionObject, self.arguments[0].mgl_jsonExpressionObject];
} else if ([function isEqualToString:@"boolValue"]) {
return @[@"to-boolean", self.operand.mgl_jsonExpressionObject];
} else if ([function isEqualToString:@"mgl_numberWithFallbackValues:"] ||
Expand All @@ -651,6 +690,12 @@ - (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_interpolateWithCurveType:parameters:stops:"]) {
if (self.arguments.count < 3) {
[NSException raise:NSInvalidArgumentException format:
Expand Down
4 changes: 4 additions & 0 deletions platform/darwin/src/NSExpression+MGLPrivateAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, readonly) id mgl_jsonExpressionObject;

- (id)mgl_has:(id)element;

@end

@interface MGLColor (MGLExpressionAdditions)
Expand All @@ -74,6 +76,8 @@ NS_ASSUME_NONNULL_BEGIN

- (NSExpression *)mgl_expressionWithContext:(NSDictionary<NSString *, NSExpression *> *)context;

- (id)mgl_has:(id)element;

@end

extern NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects);
Expand Down
57 changes: 57 additions & 0 deletions platform/darwin/test/MGLExpressionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ - (void)testVariableExpressionObject {
NSMutableDictionary *context = [@{@"heatmapDensity": @1} mutableCopy];
XCTAssertEqualObjects([expression expressionValueWithObject:nil context:context], @1);
}
{
NSExpression *expression = [NSExpression expressionForVariable:@"mgl_geometryType"];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, @[@"geometry-type"]);
XCTAssertEqualObjects([NSExpression expressionWithFormat:@"$mgl_geometryType"].mgl_jsonExpressionObject, @[@"geometry-type"]);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:@[@"geometry-type"]], expression);
}
{
NSExpression *expression = [NSExpression expressionForVariable:@"mgl_featureIdentifier"];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, @[@"id"]);
XCTAssertEqualObjects([NSExpression expressionWithFormat:@"$mgl_featureIdentifier"].mgl_jsonExpressionObject, @[@"id"]);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:@[@"id"]], expression);
}
{
NSExpression *expression = [NSExpression expressionForVariable:@"mgl_featureProperties"];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, @[@"properties"]);
XCTAssertEqualObjects([NSExpression expressionWithFormat:@"$mgl_featureProperties"].mgl_jsonExpressionObject, @[@"properties"]);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:@[@"properties"]], expression);
}
{
NSExpression *expression = [NSExpression expressionForVariable:@"loremIpsum"];
NSArray *jsonExpression = @[@"var", @"loremIpsum"];
Expand Down Expand Up @@ -629,4 +647,43 @@ - (void)testConditionalExpressionObject {

}

- (void)testLookupExpressionObject {
{
NSExpression *array = [NSExpression expressionForAggregate:@[MGLConstantExpression(@9),
MGLConstantExpression(@8),
MGLConstantExpression(@7)]];
NSExpression *expression = [NSExpression expressionForFunction:@"objectFrom:withIndex:" arguments:@[array, MGLConstantExpression(@1)]];
NSArray *jsonExpression = @[@"at", @1, @[ @"literal", @[@9, @8, @7]]];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
}
{
NSExpression *array = [NSExpression expressionForAggregate:@[MGLConstantExpression(@9),
MGLConstantExpression(@8),
MGLConstantExpression(@7)]];
NSExpression *expression = [NSExpression expressionForFunction:@"objectFrom:withIndex:" arguments:@[array, [NSExpression expressionForKeyPath:@"x"]]];
NSArray *jsonExpression = @[@"at", @[@"get", @"x"], @[ @"literal", @[@9, @8, @7]]];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
}
{
NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(self, 'mgl_has:', 'x')"];
NSArray *jsonExpression = @[@"has", @"x"];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
}
{
NSExpression *expression = [NSExpression expressionWithFormat:@"FUNCTION(%@, 'mgl_has:', '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')"];
NSArray *jsonExpression = @[@"has", @"x", @[@"properties"]];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
}
}

@end

0 comments on commit 2aaa038

Please sign in to comment.