Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only unbox NSNumbers 0, 1 to Bool for objCType 'c' #181

Merged
merged 1 commit into from
Feb 25, 2022

Conversation

macdrevx
Copy link
Contributor

Previously, initializing JSONValue with NSNumber with value 0 or 1 would produce JSONValue.boolean. This change updates the behavior to only interpret NSNumber as boolean if its objCType is 'c' and its value is 0 or 1.

@macdrevx macdrevx requested a review from 1ec5 February 25, 2022 17:50
* Previously, initializing JSONValue with NSNumber with value 0 or 1
  would produce JSONValue.boolean. This change updates the behavior to
  only interpret NSNumber as boolean if its objCType is 'c' and its
  value is 0 or 1.
Comment on lines +11 to +12
XCTAssertEqual(JSONValue(rawValue: false), .boolean(false))
XCTAssertEqual(JSONValue(rawValue: true), .boolean(true))
Copy link
Contributor Author

@macdrevx macdrevx Feb 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether supporting passing unboxed Bool was intentional @1ec5, but the old implementation did allow it, so I kept the behavior and added tests to verify it.

@1ec5 1ec5 linked an issue Feb 25, 2022 that may be closed by this pull request
Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, your solution demonstrates what I had been missing in #173 (comment).

/// In practice, choosing .boolean yields the desired result more often since it is more
/// common to work with Bool than it is Int8.
switch String(cString: number.objCType) {
case "c": // char
Copy link
Contributor

@1ec5 1ec5 Feb 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is consistent with mapbox/mapbox-gl-native#6049 (comment). so I’m confident in this fix.

@1ec5 1ec5 added the bug label Feb 25, 2022
@macdrevx macdrevx merged commit 23aaeed into main Feb 25, 2022
@macdrevx macdrevx deleted the fix-nsnumber-unboxing branch February 25, 2022 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Int property of value = 1 converts to bool unexpectedly
2 participants