diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-04-09 12:39:51 -0400 |
---|---|---|
committer | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-04-12 10:11:53 -0400 |
commit | d6139f0d0bec9cd489aa117a2cf956f31e5db9cf (patch) | |
tree | c0fc66ff402b865840a6a1e761a797b629c94584 | |
parent | ff11cb20163caddf5e31ebdaeaafa0e1a9e456ec (diff) | |
download | qtlocation-mapboxgl-d6139f0d0bec9cd489aa117a2cf956f31e5db9cf.tar.gz |
[ios, macos] Add NSPredicate between function.
-rw-r--r-- | platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm | 16 | ||||
-rw-r--r-- | platform/darwin/src/NSPredicate+MGLAdditions.mm | 22 | ||||
-rw-r--r-- | platform/darwin/test/MGLPredicateTests.mm | 22 |
3 files changed, 57 insertions, 3 deletions
diff --git a/platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm b/platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm index 5b54d66aeb..8b29199c7c 100644 --- a/platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm +++ b/platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm @@ -318,6 +318,21 @@ case NSNotEqualToPredicateOperatorType: op = @"!="; break; + case NSBetweenPredicateOperatorType: { + op = @"all"; + NSArray *arguments = self.rightExpression.constantValue; + NSPredicate *leftHandPredicate = [NSComparisonPredicate predicateWithLeftExpression:arguments[0] + rightExpression:self.leftExpression + modifier:NSAllPredicateModifier + type:NSLessThanOrEqualToPredicateOperatorType + options:0]; + NSPredicate *rightHandPredicate = [NSComparisonPredicate predicateWithLeftExpression:self.leftExpression + rightExpression:arguments[1] + modifier:NSAllPredicateModifier + type:NSLessThanOrEqualToPredicateOperatorType + options:0]; + return @[op, leftHandPredicate.mgl_jsonExpressionObject, rightHandPredicate.mgl_jsonExpressionObject]; + } case NSMatchesPredicateOperatorType: case NSLikePredicateOperatorType: case NSBeginsWithPredicateOperatorType: @@ -325,7 +340,6 @@ case NSInPredicateOperatorType: case NSCustomSelectorPredicateOperatorType: case NSContainsPredicateOperatorType: - case NSBetweenPredicateOperatorType: [NSException raise:NSInvalidArgumentException format:@"NSPredicateOperatorType:%lu is not supported.", (unsigned long)self.predicateOperatorType]; } diff --git a/platform/darwin/src/NSPredicate+MGLAdditions.mm b/platform/darwin/src/NSPredicate+MGLAdditions.mm index 8fe5735bf9..ba616d2228 100644 --- a/platform/darwin/src/NSPredicate+MGLAdditions.mm +++ b/platform/darwin/src/NSPredicate+MGLAdditions.mm @@ -280,7 +280,7 @@ NSArray *MGLSubpredicatesWithJSONObjects(NSArray *objects) { } if ([op isEqualToString:@">="]) { NSArray *subexpressions = MGLSubexpressionsWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]); - return [NSPredicate predicateWithFormat:@"%K >= %@" argumentArray:subexpressions]; + return [NSPredicate predicateWithFormat:@"%@ >= %@" argumentArray:subexpressions]; } if ([op isEqualToString:@"!"]) { NSArray *subpredicates = MGLSubpredicatesWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]); @@ -294,7 +294,25 @@ NSArray *MGLSubpredicatesWithJSONObjects(NSArray *objects) { return [NSPredicate predicateWithValue:YES]; } if ([op isEqualToString:@"all"]) { - NSArray *subpredicates = MGLSubpredicatesWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]); + NSArray *jsonObjects = [objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]; + NSArray *subpredicates = MGLSubpredicatesWithJSONObjects(jsonObjects); + if (jsonObjects.count == 2) { + // Determine if the expression is of BETWEEN type + if ([jsonObjects[0] isKindOfClass:[NSArray class]] && + [jsonObjects[1] isKindOfClass:[NSArray class]]) { + NSArray *leftCondition = jsonObjects[0]; + NSArray *rightCondition = jsonObjects[1]; + NSString *leftOperator = leftCondition.firstObject; + NSString *rightOperator = rightCondition.firstObject; + if ([leftOperator isEqualToString:@"<="] && [rightOperator isEqualToString:@"<="]) { + return [NSPredicate predicateWithFormat:@"%@ BETWEEN %@", [NSExpression mgl_expressionWithJSONObject:leftCondition[2]], @[[NSExpression mgl_expressionWithJSONObject:leftCondition[1]], [NSExpression mgl_expressionWithJSONObject:rightCondition[2]]]]; + } else if([leftOperator isEqualToString:@">="] && [rightOperator isEqualToString:@"<="]) { + return [NSPredicate predicateWithFormat:@"%@ BETWEEN %@", [NSExpression mgl_expressionWithJSONObject:leftCondition[1]], @[[NSExpression mgl_expressionWithJSONObject:leftCondition[2]], [NSExpression mgl_expressionWithJSONObject:rightCondition[2]]]]; + } else if([leftOperator isEqualToString:@">="] && [rightOperator isEqualToString:@">="]) { + return [NSPredicate predicateWithFormat:@"%@ BETWEEN %@", [NSExpression mgl_expressionWithJSONObject:leftCondition[1]], @[[NSExpression mgl_expressionWithJSONObject:leftCondition[2]], [NSExpression mgl_expressionWithJSONObject:rightCondition[1]]]]; + } + } + } return [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates]; } if ([op isEqualToString:@"any"]) { diff --git a/platform/darwin/test/MGLPredicateTests.mm b/platform/darwin/test/MGLPredicateTests.mm index d8cad0b166..6cce110619 100644 --- a/platform/darwin/test/MGLPredicateTests.mm +++ b/platform/darwin/test/MGLPredicateTests.mm @@ -576,6 +576,28 @@ namespace mbgl { NSArray *expected = @[@"==", @1, @2]; XCTAssertEqualObjects([NSPredicate predicateWithFormat:@"1 = 2"].mgl_jsonExpressionObject, expected); } + { + NSArray *expected = @[@"all", @[@"<=", @10, @[@"get", @"x"]], @[@"<=", @[@"get", @"x"], @100]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x BETWEEN %@", @[[NSExpression expressionForConstantValue:@10], [NSExpression expressionForConstantValue:@100]]]; + XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, expected); + XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:expected], predicate); + } + { + NSArray *expected = @[@"all", @[@">=", @[@"get", @"x"], @10], @[@"<=", @[@"get", @"x"], @100]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x BETWEEN %@", @[[NSExpression expressionForConstantValue:@10], [NSExpression expressionForConstantValue:@100]]]; + XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:expected], predicate); + } + { + NSArray *expected = @[@"all", @[@">=", @[@"get", @"x"], @10], @[@">=", @100, @[@"get", @"x"]]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x BETWEEN %@", @[[NSExpression expressionForConstantValue:@10], [NSExpression expressionForConstantValue:@100]]]; + XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:expected], predicate); + } + { + NSArray *expected = @[@"all", @[@"==", @10, @[@"get", @"x"]], @[@"<=", @[@"get", @"x"], @100]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ == x && x <= %@", [NSExpression expressionForConstantValue:@10], [NSExpression expressionForConstantValue:@100]]; + XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, expected); + XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:expected], predicate); + } } @end |