Repository navigation
fixes #6248: handle all NSNumber types - #6290
Conversation
| } else if (strcmp([number objCType], @encode(double)) == 0) { | ||
| return { number.doubleValue }; | ||
| } else if ((strcmp([number objCType], @encode(char))) == 0) { | ||
| } else if (strcmp([number objCType], @encode(bool)) == 0) { |
There was a problem hiding this comment.
BOOL is another type that is represented differently between 32- and 64-bit systems. 7e208c4 broke Boolean conversions on 32-bit systems while fixing it for 64-bit systems; this change does the opposite, breaking 64-bit but fixing 32-bit. We need both cases, ideally predicated (hah!) on the architecture via conditional compilation.
There was a problem hiding this comment.
As best I can tell, 7e208c4 is actually ok and we want ... if ((strcmp([number objCType], @encode(char))) == 0).
We used to compare [number objCType] (returns c for boolean on both 32 and 64 bit system to @encode(bool) which returns c on 32 bit and b on 64 bit. 7e208c4 changed to compare [number objCType] to @encode(char) which always returns c.
There was a problem hiding this comment.
I tested this branch with https://gist.github.com/boundsj/c39973346a27104a3fad1647a37606e6 (it uses a predicate with a boolean value) and this commit works by accident in that applied example since the boolean is converted into a charValue (first conversion in the new code above) and passed to core and core treats that as true. However, I think something like [unclusteredLayer setPredicate:[NSPredicate predicateWithFormat:@"%K != NO", @"cluster"]]; would not work as expected since the NO value (when converted to a charValue) would still be true.
There was a problem hiding this comment.
Yeah there are some weird things going on with booleans still, more to come. Been digging into this.
There was a problem hiding this comment.
Re: weird things, namely, regardless of platform and handling, there is currently no way to tell if a char value (e.g., 17) should be handled as a number and not a boolean. When we do actually get a boolean parsed out of an NSExpression, the class cluster type inside NSNumber is NSCFBoolean, so I'm currently working on inspecting that a bit better.
We convert NSNumbers (and NSStrings) to the appropriate mbgl value so that we can use NSPredicates to describe mbgl filters we want to apply to style layers at runtime. This change fixes an issue where the conversion from an NSNumber that represented a bool was not recognized as such. encode(bool) returns a 'c' or 'b' on 32 bit and 64 bit systems respectively and objCType of an NSNumber representing a bool always returns 'c'. Now the implementation checks for 'c' always and NSNumbers representing bool don't fall through and trigger the exception.
|
Can you add tests exercising all the additional code paths? |
|
|
Ok, 3043409 fixes things, I think. Well, it gets the types right now. Filters still aren't working in my use case ( I think we want the behavior in this commit for Regardless of 32- or 64-bit, we interpret both Note also the commentary in 3043409#diff-698c604c23220e922e9df03699e408abR58 about how we never seem to get back actually unsigned More to come. |
| } else if (thirtyTwoBit && strcmp([number objCType], @encode(float)) == 0) { | ||
| // Float values are converted to double on 32-bit platforms and | ||
| // introduce precision problems. | ||
| [NSException raise:@"Float values introduce imprecision on 32-bit systems" |
There was a problem hiding this comment.
This might be too harsh for what could end up being a pretty common developer scenario. Can we avoid the overprecision issue by using doubleValue instead of floatValue? As a last resort, how about logging a one-time warning instead of raising an exception?
There was a problem hiding this comment.
Taking the doubleValue effectively gets the same result as the double cast, so no-go. Using float is just dangerous territory here, but yeah, a log warning could suffice.
There was a problem hiding this comment.
Eh, this happens on 64-bit, too. I just got 23.232300 as a -numberWithFloat: turned into 23.2322998046875. So we need to warn regardless of platform.
Begun in c3f90c3. |
|
| source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"test-source" geoJSONData:geoJSONData]; | ||
| [self.mapView.style addSource:source]; | ||
| layer = [[MGLLineStyleLayer alloc] initWithLayerIdentifier:@"test-layer" sourceIdentifier:@"test-source"]; | ||
| [self.mapView.style addLayer:layer]; |
There was a problem hiding this comment.
@incanus I just realized that the reason the simple test was breaking is that, since the tearDown method calls removeLayer and since that code path throws an exception if you attempt to remove a layer that has not been added, adding any test that does not add the test's layer ivar will cause the error we saw. I think we can just chalk this up to shared setup and cleanup methods sometimes causing issues like this. In any case, breaking the tests apart was the way to go here.
|
This is looking basically done, pending some test adjustments in order to pass on 32-bit systems. Getting some failures around the |
| // so warn the user to avoid them. This would require them to | ||
| // explicitly use -[NSNumber numberWithFloat:] arguments anyway. | ||
| // We still do this conversion in order to provide a valid value. | ||
| mbgl::Log::Warning(mbgl::Event::Style, "float values are converted to double and can introduce imprecision; please use double values explicitly in predicate arguments"); |
There was a problem hiding this comment.
Why mbgl::Log? Since this is in platform-specific code, we can issue an NSLog() directly. We should also stick it in a dispatch_once(), like we did in #6163, to avoid potentially spamming the console.
|
By the way, we currently test the conversion in the other direction, from |
|
Ok, things are much cleaner, more correct, and tests are now passing on both 32- and 64-bit test simulators as of f6350f6 and d0762f7. @jfirebaugh helped me realize that @waphumanoid, would you mind confirming whether this fixes #6248 for you? Assuming this checks out and tests pass for b9c1e1e, this will be ready to 🚢. |
There was a problem hiding this comment.
We can still assert that certain values like -1 are round-tripped, and we can still do imprecise comparisons to make sure it's in the right ballpark.
There was a problem hiding this comment.
a486f3390543684c7fd63478938a00f3e52431b6
There was a problem hiding this comment.
In the message, say that the message won't be shown again. This is a common pattern for limited-time only offers one-time log messages.
There was a problem hiding this comment.
05874a4154ea9f687e98c783e2a11316a0ac6510
There was a problem hiding this comment.
We should at least exercise the code path to ensure we convert to something in the right ballpark.
There was a problem hiding this comment.
a486f3390543684c7fd63478938a00f3e52431b6
|
@incanus, Sorry I can't check it since I'm not able to compile all the project :( . I use it via podspecs |
|
@waphumanoid Not a huge deal, though you can do this if you want:
|
|
Ok, gonna rebase on |
a486f33 to
fc9aa30
Compare
|
Squashed into fc9aa30. When that passes, I'll merge locally and push up to close this out. |
fc9aa30 to
6304103
Compare
|
It passed, but based on elapsed time I had to rebase again and then still couldn't merge at the command line as things are blocked by this new review system. Can I get someone to review this as of 6304103 so I can land this? |
|
@incanus, Thanks, I did compile the master branch (with a little issue which I will post separately) and give it some tests. It works as expected so far. Still testing and looking forward to seeing an implementation for |
This is being tracked in #6244. |
No description provided.