From 129730f3ea8c1c3ae3a56acd8c7ec0182f6c3d0a Mon Sep 17 00:00:00 2001 From: Fabian Guerra Date: Tue, 24 Apr 2018 16:51:31 -0400 Subject: [PATCH 1/3] [ios, macos] Add support for subscripting in expressions. --- .../darwin/src/NSExpression+MGLAdditions.mm | 19 ++++++++++++- platform/darwin/test/MGLExpressionTests.mm | 27 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index c82a7dc008a..d74e388b8ea 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -1097,7 +1097,24 @@ - (id)mgl_jsonExpressionObject { 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"]) { + NSExpression *count = [NSExpression expressionWithFormat:@"count(%@) - 1", self.arguments[0]]; + id last = [count expressionValueWithObject:nil context:nil]; + index = [NSExpression expressionForConstantValue:last].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 d54e961b005..31ed806c41a 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -827,6 +827,33 @@ - (void)testConditionalExpressionObject { } - (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", @2, @[ @"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), From 8de14901c44db8d4ab9a2111f5721333c276c43f Mon Sep 17 00:00:00 2001 From: Fabian Guerra Date: Wed, 25 Apr 2018 10:15:12 -0400 Subject: [PATCH 2/3] [ios, macos] Update changelogs. --- platform/ios/CHANGELOG.md | 1 + platform/macos/CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index 7ec4b4633f3..ac3b4f66d1a 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)) ### Other diff --git a/platform/macos/CHANGELOG.md b/platform/macos/CHANGELOG.md index 498ed3c379b..3baf953c077 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)) ## 0.7.0 - April 19, 2018 From 827998a85acd30546ce2701fa0b12336405a1998 Mon Sep 17 00:00:00 2001 From: Fabian Guerra Date: Thu, 26 Apr 2018 11:40:39 -0400 Subject: [PATCH 3/3] [ios, macos] Refactor LAST subscripting expression. --- platform/darwin/src/NSExpression+MGLAdditions.mm | 4 +--- platform/darwin/test/MGLExpressionTests.mm | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index d74e388b8ea..5d35ee696e8 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -1106,9 +1106,7 @@ - (id)mgl_jsonExpressionObject { if ([value isEqualToString:@"FIRST"]) { index = [NSExpression expressionForConstantValue:@0].mgl_jsonExpressionObject; } else if ([value isEqualToString:@"LAST"]) { - NSExpression *count = [NSExpression expressionWithFormat:@"count(%@) - 1", self.arguments[0]]; - id last = [count expressionValueWithObject:nil context:nil]; - index = [NSExpression expressionForConstantValue:last].mgl_jsonExpressionObject; + index = [NSExpression expressionWithFormat:@"count(%@) - 1", self.arguments[0]].mgl_jsonExpressionObject; } else if ([value isEqualToString:@"SIZE"]) { return [NSExpression expressionWithFormat:@"count(%@)", self.arguments[0]].mgl_jsonExpressionObject; } diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm index 31ed806c41a..c6c85bdc585 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -842,7 +842,7 @@ - (void)testLookupExpressionObject { MGLConstantExpression(@7)]]; NSExpression *expression = [NSExpression expressionForFunction:@"objectFrom:withIndex:" arguments:@[array, MGLConstantExpression(@"LAST")]]; - NSArray *jsonExpression = @[@"at", @2, @[ @"literal", @[@9, @8, @7]]]; + NSArray *jsonExpression = @[@"at", @[@"-", @[@"length", @[ @"literal", @[@9, @8, @7]]], @1], @[ @"literal", @[@9, @8, @7]]]; XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); } {