diff options
author | Fabian Guerra Soto <fabian.guerra@mapbox.com> | 2018-03-26 15:22:54 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-26 15:22:54 -0400 |
commit | 9a663fb4799bedb85ca583d7f23a1a67277d220b (patch) | |
tree | 438e3c3fb733428d36bf0c99c7dc88d44027c387 /platform/darwin/src/NSExpression+MGLAdditions.mm | |
parent | 9eff2c52fb57c5cb1a1c813d3fb6a307bde12706 (diff) | |
download | qtlocation-mapboxgl-9a663fb4799bedb85ca583d7f23a1a67277d220b.tar.gz |
[ios, macos] Add match expressions support. (#11464)
* [ios, macos] Add match expressions support.
* [ios, macos] Add coalesce operator support.
* [ios, macos] Add coalesce operator test.
* [ios, macos] Match operator implementation is moved to NSPredicate.
* [ios, macos] Match operator function implementation refactor.
* [ios, macos] Address merge conflicts.
Diffstat (limited to 'platform/darwin/src/NSExpression+MGLAdditions.mm')
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index bd747dc97e..9797709b7b 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -258,6 +258,12 @@ return [self valueForKeyPath:@"mgl_jsonExpressionObject"]; } +- (id)mgl_coalesce { + [NSException raise:NSInvalidArgumentException + format:@"Coalesce expressions lack underlying Objective-C implementations."]; + return nil; +} + @end @implementation NSDictionary (MGLExpressionAdditions) @@ -476,11 +482,31 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { } else { [arguments addObject:[NSExpression mgl_expressionWithJSONObject:caseExpressions[index]]]; } - } return [NSExpression expressionForFunction:firstConditional selectorName:@"mgl_case:" arguments:arguments]; - } else { + } else if ([op isEqualToString:@"match"]) { + NSExpression *operand = [NSExpression mgl_expressionWithJSONObject:argumentObjects[0]]; + NSArray *matchOptions = [argumentObjects subarrayWithRange:NSMakeRange(1, argumentObjects.count - 1)]; + + NSMutableArray *optionsArray = [NSMutableArray array]; + NSEnumerator *optionsEnumerator = matchOptions.objectEnumerator; + while (id object = optionsEnumerator.nextObject) { + NSExpression *option = [NSExpression mgl_expressionWithJSONObject:object]; + [optionsArray addObject:option]; + } + + return [NSExpression expressionForFunction:operand + selectorName:@"mgl_match:" + arguments:optionsArray]; + } else if ([op isEqualToString:@"coalesce"]) { + NSMutableArray *expressions = [NSMutableArray array]; + for (id operand in argumentObjects) { + [expressions addObject:[NSExpression mgl_expressionWithJSONObject:operand]]; + } + + return [NSExpression expressionWithFormat:@"FUNCTION(%@, 'mgl_coalesce')", expressions]; + }else { [NSException raise:NSInvalidArgumentException format:@"Expression operator %@ not yet implemented.", op]; } @@ -674,7 +700,7 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { } else if ([function isEqualToString:@"mgl_case:"]) { NSPredicate *firstConditional = (NSPredicate *)self.operand.constantValue; NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"case", firstConditional.mgl_jsonExpressionObject, nil]; - + for (NSExpression *option in self.arguments) { if ([option respondsToSelector:@selector(constantValue)] && [option.constantValue isKindOfClass:[NSComparisonPredicate class]]) { NSPredicate *predicate = (NSPredicate *)option.constantValue; @@ -683,6 +709,23 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { [expressionObject addObject:option.mgl_jsonExpressionObject]; } } + + return expressionObject; + } else if ([function isEqualToString:@"mgl_match:"]) { + NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"match", self.operand.mgl_jsonExpressionObject, nil]; + + + for (NSExpression *option in self.arguments) { + [expressionObject addObject:option.mgl_jsonExpressionObject]; + } + + return expressionObject; + } else if ([function isEqualToString:@"mgl_coalesce"]) { + NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"coalesce", nil]; + + for (NSExpression *expression in self.operand.constantValue) { + [expressionObject addObject:[expression mgl_jsonExpressionObject]]; + } return expressionObject; } else if ([function isEqualToString:@"median:"] || |