From 2c62f0a15bed02bc6ca014fd113d5e62d9808624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Sat, 17 Dec 2016 15:39:43 -0800 Subject: [ios, macos] Fixed null expression crash Convert NSNull into an mbgl null value. --- platform/darwin/src/NSExpression+MGLAdditions.mm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'platform/darwin/src/NSExpression+MGLAdditions.mm') diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index 392a6d7f5b..6af069487e 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -55,9 +55,10 @@ // We use long long here to avoid any truncation. return { (int64_t)number.longLongValue }; } + } else if (value != [NSNull null]) { + [NSException raise:@"Value not handled" + format:@"Can’t convert %s:%@ to mbgl::Value", [value objCType], value]; } - [NSException raise:@"Value not handled" - format:@"Can’t convert %s:%@ to mbgl::Value", [value objCType], value]; return { }; } -- cgit v1.2.1 From e6132d248b54d202c3fa132b3084660a0bbe3c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Mon, 26 Dec 2016 14:24:05 -0800 Subject: [ios, macos] Rewrote predicate/filter conversion When converting predicates to filters, symmetric comparison predicates can now compare a value to a key in addition to the usual key-to-value order. Added error checking for unhandled combinations like key-to-key. Fixed a crash converting a CONTAINS predicate into a filter. Added support for constant value expressions inside aggregate expressions. Allow sets as aggregate expressions just like arrays, except in BETWEEN predicates where order matters. Flatten NOT predicates into more specialized filters. When converting filters to predicates, use constant value expressions inside aggregate expressions. Convert to a BETWEEN predicate when possible. Replaced predicate round-tripping integration tests with systematic unit tests for converting in either direction, plus unit tests for round-tripping and symmetry. Refined exception names and messages. Realphabetized files in groups. --- platform/darwin/src/NSExpression+MGLAdditions.mm | 25 +++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'platform/darwin/src/NSExpression+MGLAdditions.mm') diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index 6af069487e..97f3e11dba 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -4,15 +4,19 @@ - (std::vector)mgl_filterValues { - if ([self.constantValue isKindOfClass:NSArray.class]) { - NSArray *values = self.constantValue; - std::vectorconvertedValues; - for (id value in values) { - convertedValues.push_back([self mgl_convertedValueWithValue:value]); + if ([self.constantValue isKindOfClass:[NSArray class]] || [self.constantValue isKindOfClass:[NSSet class]]) { + std::vector convertedValues; + for (id item in self.constantValue) { + id constantValue = item; + if ([item isKindOfClass:[NSExpression class]]) { + constantValue = [constantValue constantValue]; + } + convertedValues.push_back([self mgl_convertedValueWithValue:constantValue]); } return convertedValues; } - [NSException raise:@"Values not handled" format:@""]; + [NSException raise:NSInvalidArgumentException + format:@"Constant value expression must contain an array or set."]; return { }; } @@ -42,7 +46,10 @@ // We still do this conversion in order to provide a valid value. static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - NSLog(@"One-time warning: Float values are converted to double and can introduce imprecision; please use double values explicitly in predicate arguments."); + NSLog(@"Float value in expression will be converted to a double; some imprecision may result. " + @"Use double values explicitly when specifying constant expression values and " + @"when specifying arguments to predicate and expression format strings. " + @"This will be logged only once."); }); return { (double)number.doubleValue }; } else if ([number compare:@(0)] == NSOrderedDescending || @@ -55,8 +62,8 @@ // We use long long here to avoid any truncation. return { (int64_t)number.longLongValue }; } - } else if (value != [NSNull null]) { - [NSException raise:@"Value not handled" + } else if (value && value != [NSNull null]) { + [NSException raise:NSInvalidArgumentException format:@"Can’t convert %s:%@ to mbgl::Value", [value objCType], value]; } return { }; -- cgit v1.2.1 From aef36956ee062ea2992c43d658cebf52954b5213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Mon, 26 Dec 2016 15:07:43 -0800 Subject: [ios, macos] Cleaned up expression conversion --- platform/darwin/src/NSExpression+MGLAdditions.mm | 60 ++++++++++-------------- 1 file changed, 25 insertions(+), 35 deletions(-) (limited to 'platform/darwin/src/NSExpression+MGLAdditions.mm') diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index 97f3e11dba..f11a1919cf 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -2,31 +2,25 @@ @implementation NSExpression (MGLAdditions) -- (std::vector)mgl_filterValues -{ +- (std::vector)mgl_aggregateMBGLValue { if ([self.constantValue isKindOfClass:[NSArray class]] || [self.constantValue isKindOfClass:[NSSet class]]) { std::vector convertedValues; - for (id item in self.constantValue) { - id constantValue = item; - if ([item isKindOfClass:[NSExpression class]]) { - constantValue = [constantValue constantValue]; + for (id value in self.constantValue) { + NSExpression *expression = value; + if (![expression isKindOfClass:[NSExpression class]]) { + expression = [NSExpression expressionForConstantValue:expression]; } - convertedValues.push_back([self mgl_convertedValueWithValue:constantValue]); + convertedValues.push_back(expression.mgl_constantMBGLValue); } return convertedValues; } [NSException raise:NSInvalidArgumentException format:@"Constant value expression must contain an array or set."]; - return { }; -} - -- (mbgl::Value)mgl_filterValue -{ - return [self mgl_convertedValueWithValue:self.constantValue]; + return {}; } -- (mbgl::Value)mgl_convertedValueWithValue:(id)value -{ +- (mbgl::Value)mgl_constantMBGLValue { + id value = self.constantValue; if ([value isKindOfClass:NSString.class]) { return { std::string([(NSString *)value UTF8String]) }; } else if ([value isKindOfClass:NSNumber.class]) { @@ -66,30 +60,26 @@ [NSException raise:NSInvalidArgumentException format:@"Can’t convert %s:%@ to mbgl::Value", [value objCType], value]; } - return { }; + return {}; } -- (mbgl::FeatureIdentifier)mgl_featureIdentifier -{ - id value = self.constantValue; - mbgl::Value mbglValue = [self mgl_filterValue]; +- (mbgl::FeatureIdentifier)mgl_featureIdentifier { + mbgl::Value mbglValue = self.mgl_constantMBGLValue; - if ([value isKindOfClass:NSString.class]) { + if (mbglValue.is()) { return mbglValue.get(); - } else if ([value isKindOfClass:NSNumber.class]) { - NSNumber *number = (NSNumber *)value; - if ((strcmp([number objCType], @encode(char)) == 0) || - (strcmp([number objCType], @encode(BOOL)) == 0)) { - return mbglValue.get(); - } else if ( strcmp([number objCType], @encode(double)) == 0 || - strcmp([number objCType], @encode(float)) == 0) { - return mbglValue.get(); - } else if ([number compare:@(0)] == NSOrderedDescending || - [number compare:@(0)] == NSOrderedSame) { - return mbglValue.get(); - } else if ([number compare:@(0)] == NSOrderedAscending) { - return mbglValue.get(); - } + } + if (mbglValue.is()) { + return mbglValue.get(); + } + if (mbglValue.is()) { + return mbglValue.get(); + } + if (mbglValue.is()) { + return mbglValue.get(); + } + if (mbglValue.is()) { + return mbglValue.get(); } return {}; -- cgit v1.2.1