From 1c847b321b401a473a45f839d7e53c27836c1393 Mon Sep 17 00:00:00 2001 From: Fabian Guerra Soto Date: Fri, 4 May 2018 13:30:06 -0400 Subject: [ios, macos] Add support for subscripting in expressions. (#11770) * [ios, macos] Add support for subscripting in expressions. * [ios, macos] Update changelogs. * [ios, macos] Refactor LAST subscripting expression. --- platform/darwin/src/NSExpression+MGLAdditions.mm | 17 ++++++++++++++- platform/darwin/test/MGLExpressionTests.mm | 27 ++++++++++++++++++++++++ platform/ios/CHANGELOG.md | 1 + platform/macos/CHANGELOG.md | 1 + 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index 633f433a8f..7881b79370 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -1097,7 +1097,22 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { 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]; + id index = self.arguments[1].mgl_jsonExpressionObject; + + if ([self.arguments[1] expressionType] == NSConstantValueExpressionType + && [[self.arguments[1] constantValue] isKindOfClass:[NSString class]]) { + id value = self.arguments[1].constantValue; + + if ([value isEqualToString:@"FIRST"]) { + index = [NSExpression expressionForConstantValue:@0].mgl_jsonExpressionObject; + } else if ([value isEqualToString:@"LAST"]) { + index = [NSExpression expressionWithFormat:@"count(%@) - 1", self.arguments[0]].mgl_jsonExpressionObject; + } else if ([value isEqualToString:@"SIZE"]) { + return [NSExpression expressionWithFormat:@"count(%@)", self.arguments[0]].mgl_jsonExpressionObject; + } + } + + return @[@"at", index, self.arguments[0].mgl_jsonExpressionObject]; } else if ([function isEqualToString:@"boolValue"]) { return @[@"to-boolean", self.operand.mgl_jsonExpressionObject]; } else if ([function isEqualToString:@"mgl_number"] || diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm index 9c75dedaa3..186cae609d 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -837,6 +837,33 @@ using namespace std::string_literals; } - (void)testLookupExpressionObject { + { + NSExpression *array = [NSExpression expressionForAggregate:@[MGLConstantExpression(@9), + MGLConstantExpression(@8), + MGLConstantExpression(@7)]]; + NSExpression *expression = [NSExpression expressionForFunction:@"objectFrom:withIndex:" + arguments:@[array, MGLConstantExpression(@"FIRST")]]; + NSArray *jsonExpression = @[@"at", @0, @[ @"literal", @[@9, @8, @7]]]; + XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); + } + { + NSExpression *array = [NSExpression expressionForAggregate:@[MGLConstantExpression(@9), + MGLConstantExpression(@8), + MGLConstantExpression(@7)]]; + NSExpression *expression = [NSExpression expressionForFunction:@"objectFrom:withIndex:" + arguments:@[array, MGLConstantExpression(@"LAST")]]; + NSArray *jsonExpression = @[@"at", @[@"-", @[@"length", @[ @"literal", @[@9, @8, @7]]], @1], @[ @"literal", @[@9, @8, @7]]]; + XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); + } + { + NSExpression *array = [NSExpression expressionForAggregate:@[MGLConstantExpression(@9), + MGLConstantExpression(@8), + MGLConstantExpression(@7)]]; + NSExpression *expression = [NSExpression expressionForFunction:@"objectFrom:withIndex:" + arguments:@[array, MGLConstantExpression(@"SIZE")]]; + NSArray *jsonExpression = @[@"length", @[ @"literal", @[@9, @8, @7]]]; + XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); + } { NSExpression *array = [NSExpression expressionForAggregate:@[MGLConstantExpression(@9), MGLConstantExpression(@8), diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index 8fc1f7e1e0..d979021760 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -7,6 +7,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT ### Style layers * Deprecated `+[NSExpression featurePropertiesVariableExpression]` use `+[NSExpression featureAttributesVariableExpression]` instead. ([#11748](https://github.com/mapbox/mapbox-gl-native/pull/11748)) +* Added `FISRT`, `LAST`, and `SIZE` symbolic array subscripting support to expressions. ([#11770](https://github.com/mapbox/mapbox-gl-native/pull/11770)) ### Annotations diff --git a/platform/macos/CHANGELOG.md b/platform/macos/CHANGELOG.md index 0e8d40f256..7cc8e945af 100644 --- a/platform/macos/CHANGELOG.md +++ b/platform/macos/CHANGELOG.md @@ -5,6 +5,7 @@ ### Style layers * Deprecated `+[NSExpression featurePropertiesVariableExpression]` use `+[NSExpression featureAttributesVariableExpression]` instead. ([#11748](https://github.com/mapbox/mapbox-gl-native/pull/11748)) +* Added `FISRT`, `LAST`, and `SIZE` symbolic array subscripting support to expressions. ([#11770](https://github.com/mapbox/mapbox-gl-native/pull/11770)) ### Other changes -- cgit v1.2.1