diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2018-03-29 04:13:25 -0700 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2018-03-29 04:13:25 -0700 |
commit | 72499daf39b6baea8c5fa48b768bf46f35e2b6e3 (patch) | |
tree | 6dbfc760e20038448a210494a9177bfdf20f0050 | |
parent | 2625c7c215bbb6b063fac4e941a04c064ff56b43 (diff) | |
download | qtlocation-mapboxgl-72499daf39b6baea8c5fa48b768bf46f35e2b6e3.tar.gz |
[ios, macos] Renamed, reversed has expression
Renamed mgl_hasProperty:properties: to mgl_does:have: for better readability and consistency with the conventional mgl_has: function. Documented both forms of mgl_has:.
-rw-r--r-- | platform/darwin/docs/guides/For Style Authors.md.ejs | 2 | ||||
-rw-r--r-- | platform/darwin/docs/guides/Predicates and Expressions.md | 14 | ||||
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 29 | ||||
-rw-r--r-- | platform/darwin/test/MGLExpressionTests.mm | 15 |
4 files changed, 36 insertions, 24 deletions
diff --git a/platform/darwin/docs/guides/For Style Authors.md.ejs b/platform/darwin/docs/guides/For Style Authors.md.ejs index fb1fd3b3c0..8dd5d53ebf 100644 --- a/platform/darwin/docs/guides/For Style Authors.md.ejs +++ b/platform/darwin/docs/guides/For Style Authors.md.ejs @@ -343,7 +343,7 @@ In style specification | Method, function, or predicate type | Format string syn `properties` | |`$mgl_featureProperties` `at` | `objectFrom:withIndex:` | `array[n]` `get` | `+[NSExpression expressionForKeyPath:]` | Key path -`has` | `mgl_hasProperty:properties:` | +`has` | `mgl_does:have:` | `mgl_does:have:(self, 'key')` `length` | `count:` | `count({1, 2, 2, 3, 4, 7, 9})` `!` | `NSNotPredicateType` | `NOT (p0 OR … OR pn)` `!=` | `NSNotEqualToPredicateOperatorType` | `key != value` diff --git a/platform/darwin/docs/guides/Predicates and Expressions.md b/platform/darwin/docs/guides/Predicates and Expressions.md index 27c7c4cd8d..53fe40e3aa 100644 --- a/platform/darwin/docs/guides/Predicates and Expressions.md +++ b/platform/darwin/docs/guides/Predicates and Expressions.md @@ -192,8 +192,9 @@ string syntax: Initializer parameter | Format string syntax | Description ----------------------|----------------------|------------ +`mgl_does:have:` | `mgl_does:have:(SELF, 'key')` or `mgl_does:have:(%@, 'key')` | Returns a Boolean value indicating whether the dictionary has a value for the key or whether the evaluated object (`SELF`) has a value for the feature attribute. Compared to the `mgl_has:` custom function, that function’s target is instead passed in as the first argument to this function. Both functions are equivalent to the syntax `key != NIL` or `%@[key] != NIL` but can be used outside of a predicate. `mgl_interpolate:withCurveType:parameters:stops:` | `mgl_interpolate:withCurveType:parameters:stops:(x, 'linear', nil, %@)` | Produces continuous, smooth results by interpolating between pairs of input and output values (“stops”). Compared to the `mgl_interpolateWithCurveType:parameters:stops:` custom function, the input expression (that function’s target) is instead passed in as the first argument to this function. -`mgl_step:from:stops:` | `mgl_step:from:stops:(x, 11, %@)` |Produces discrete, stepped results by evaluating a piecewise-constant function defined by pairs of input and output values ("stops"). Compared to the `mgl_stepWithMinimum:stops:` custom function, the input expression (that function’s target) is instead passed in as the first argument to this function. +`mgl_step:from:stops:` | `mgl_step:from:stops:(x, 11, %@)` | Produces discrete, stepped results by evaluating a piecewise-constant function defined by pairs of input and output values ("stops"). Compared to the `mgl_stepWithMinimum:stops:` custom function, the input expression (that function’s target) is instead passed in as the first argument to this function. `mgl_join:` | `mgl_join({'Old', 'MacDonald'})` | Returns the result of concatenating together all the elements of an array in order. Compared to the `stringByAppendingString:` custom function, this function takes only one argument, which is an aggregate expression containing the strings to concatenate. `mgl_coalesce:` | `mgl_coalesce({x, y, z})` | Returns the first non-`nil` value from an array of expressions. `MGL_LET` | `MGL_LET('age', uppercase('old'), 'name', uppercase('MacDonald'), mgl_join({$age, $name}))` | Any number of variable names interspersed with their assigned `NSExpression` values, followed by an `NSExpression` that may contain references to those variables. Compared to the `mgl_expressionWithContext:` custom function, this function takes the variable names and values inline before the expression that contains references to those variables. @@ -222,6 +223,17 @@ The following custom functions are also available with the </td> </tr> <tr> + <td><code>mgl_has:</code></td> + <td> + An `NSExpression` that evaluates to an <code>NSDictionary</code> or the evaluated object (<code>SELF</code>). + </td> + <td> + An `NSExpression` that evaluates to an <code>NSString</code> representing the key to look up in the dictionary or the feature attribute to look up in the evaluated object (see <code>MGLFeature.attributes</code>). + </td> + <td> + `true` if the dictionary has a value for the key or if the evaluated object has a value for the feature attribute. + </td> +<tr> <td><code>mgl_expressionWithContext:</code></td> <td> An `NSExpression` that may contain references to the variables defined in diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index 46e976f032..765d1e9385 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -61,7 +61,7 @@ INSTALL_METHOD(mgl_interpolate:withCurveType:parameters:stops:); INSTALL_METHOD(mgl_step:from:stops:); INSTALL_METHOD(mgl_coalesce:); - INSTALL_METHOD(mgl_hasProperty:properties:); + INSTALL_METHOD(mgl_does:have:); // Install functions that resemble control structures, taking arbitrary // numbers of arguments. Vararg aftermarket functions need to be declared @@ -111,12 +111,11 @@ } /** - A placeholder for a method that evaluates has expression. + Returns a Boolean value indicating whether the object has a value for the given + key. */ -- (id)mgl_hasProperty:(id)element properties:(id)properties { - [NSException raise:NSInvalidArgumentException - format:@"Has expressions lack underlying Objective-C implementations."]; - return nil; +- (BOOL)mgl_does:(id)object have:(NSString *)key { + return [object valueForKey:key] != nil; } /** @@ -594,10 +593,9 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { 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 expressionForFunction:@"mgl_hasProperty:properties:" arguments:@[property, operand]]; + NSExpression *operand = argumentObjects.count > 1 ? subexpressions[1] : [NSExpression expressionForEvaluatedObject]; + NSExpression *key = subexpressions.firstObject; + return [NSExpression expressionForFunction:@"mgl_does:have:" arguments:@[operand, key]]; } else if ([op isEqualToString:@"interpolate"]) { NSArray *interpolationOptions = argumentObjects.firstObject; NSString *curveType = interpolationOptions.firstObject; @@ -848,7 +846,7 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { return @[@"to-string", self.operand.mgl_jsonExpressionObject]; } else if ([function isEqualToString:@"noindex:"]) { return self.arguments.firstObject.mgl_jsonExpressionObject; - } else if ([function isEqualToString:@"mgl_hasProperty:properties:"] || + } else if ([function isEqualToString:@"mgl_does:have:"] || [function isEqualToString:@"mgl_has:"]) { return self.mgl_jsonHasExpressionObject; } else if ([function isEqualToString:@"mgl_interpolate:withCurveType:parameters:stops:"] @@ -1075,16 +1073,15 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { } - (id)mgl_jsonHasExpressionObject { - BOOL isAftermarketFunction = [self.function isEqualToString:@"mgl_hasProperty:properties:"]; - NSExpression *operand = isAftermarketFunction ? self.arguments[1] : self.operand; + BOOL isAftermarketFunction = [self.function isEqualToString:@"mgl_does:have:"]; + NSExpression *operand = isAftermarketFunction ? self.arguments[0] : self.operand; + NSExpression *key = self.arguments[isAftermarketFunction ? 1 : 0]; - NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"has", self.arguments[0].mgl_jsonExpressionObject, nil]; + NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"has", key.mgl_jsonExpressionObject, nil]; if (operand.expressionType != NSEvaluatedObjectExpressionType) { [expressionObject addObject:operand.mgl_jsonExpressionObject]; } return expressionObject; - - return expressionObject; } @end diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm index 10c627abf4..63c8e5d2f1 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -720,8 +720,9 @@ using namespace std::string_literals; XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression); } { - NSExpression *expression = [NSExpression expressionForFunction:@"mgl_hasProperty:properties:" arguments:@[[NSExpression expressionForConstantValue:@"x"], - [NSExpression expressionForEvaluatedObject]]]; + NSExpression *expression = [NSExpression expressionForFunction:@"mgl_does:have:" + arguments:@[[NSExpression expressionForEvaluatedObject], + [NSExpression expressionForConstantValue:@"x"]]]; NSExpression *compatibilityExpression = [NSExpression expressionWithFormat:@"FUNCTION(self, 'mgl_has:', 'x')"]; NSArray *jsonExpression = @[@"has", @"x"]; XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); @@ -729,15 +730,17 @@ using namespace std::string_literals; XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression); } { - NSExpression *expression = [NSExpression expressionForFunction:@"mgl_hasProperty:properties:" arguments:@[[NSExpression expressionForConstantValue:@"x"], - [NSExpression expressionForConstantValue:@{@"x": MGLConstantExpression(@0)}]]]; + NSExpression *expression = [NSExpression expressionForFunction:@"mgl_does:have:" + arguments:@[MGLConstantExpression(@{@"x": MGLConstantExpression(@0)}), + MGLConstantExpression(@"x")]]; NSArray *jsonExpression = @[@"has", @"x", @[@"literal", @{@"x": @0}]]; XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression); } { - NSExpression *expression = [NSExpression expressionForFunction:@"mgl_hasProperty:properties:" arguments:@[[NSExpression expressionForConstantValue:@"x"], - [NSExpression expressionForVariable:@"mgl_featureProperties"]]]; + NSExpression *expression = [NSExpression expressionForFunction:@"mgl_does:have:" + arguments:@[[NSExpression expressionForVariable:@"mgl_featureProperties"], + [NSExpression expressionForConstantValue:@"x"]]]; NSArray *jsonExpression = @[@"has", @"x", @[@"properties"]]; XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression); |