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

[ios, macos] Add support for subscripting in expressions. #11770

Merged
merged 3 commits into from
May 4, 2018

Conversation

fabian-guerra
Copy link
Contributor

Fixes #11757

@fabian-guerra fabian-guerra added iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling labels Apr 24, 2018
@fabian-guerra fabian-guerra added this to the ios-v4.0.1 milestone Apr 24, 2018
@fabian-guerra fabian-guerra self-assigned this Apr 24, 2018
@fabian-guerra fabian-guerra requested a review from 1ec5 April 24, 2018 20:53
Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the comment below, please add a blurb to the iOS and macOS changelogs. Thanks!

if ([value isEqualToString:@"FIRST"]) {
index = [NSExpression expressionForConstantValue:@0].mgl_jsonExpressionObject;
} else if ([value isEqualToString:@"LAST"]) {
NSArray *array = self.arguments[0].constantValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first argument isn’t necessarily a constant value; it could be an aggregate expression or perhaps a conditional expression. Instead, synthesize an expression of the form count(%@) - 1, where %@ is arguments[0]. Also add a unit test to exercise this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are tests for each case:

NSExpression *expression = [NSExpression expressionForFunction:@"objectFrom:withIndex:"
arguments:@[array, MGLConstantExpression(@"LAST")]];

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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the first argument is an expression like CAST(color, 'NSArray')? Casting a color to an array is unimplemented by default in NSExpression, so evaluating the expression will raise an exception. Instead, get the mgl_jsonExpressionObject of count itself, similar to how the SIZE case is implemented.

index = [NSExpression expressionForConstantValue:@0].mgl_jsonExpressionObject;
} else if ([value isEqualToString:@"LAST"]) {
NSExpression *count = [NSExpression expressionWithFormat:@"count(%@) - 1", self.arguments[0]];
index = count.mgl_jsonExpressionObject;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider inlining count here, because the variable name count is a bit misleading. (It’s the last index, not the count.)

@fabian-guerra fabian-guerra merged commit 1c847b3 into release-boba May 4, 2018
@fabian-guerra fabian-guerra deleted the fabian-subscripting-11757 branch May 4, 2018 17:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants