Skip to content

Commit

Permalink
Improve the way we detect Boolean values
Browse files Browse the repository at this point in the history
Previously, we were relying on functionality found in CoreFoundation in
order to determine if an NSNumber instance was representing an
underlying Boolean value. This worked well, but unfortunately
CoreFoundation isn't available on Linux, which means that Argo would
never be able to compile. We'd like Argo to be as widely availably as
possible, so we need to find another solution.

Luckily, it looks like we can use `type(of:)` to determine this. That
function ships with Swift itself and so _should_ mean that we're now
able to compile on Linux without any behavioral change.

I added another test to ensure that our decoding is operating as we'd
expect. The previous test seemed like it was vulnerable to false
positives.
  • Loading branch information
gfontenot committed Dec 5, 2016
1 parent 0216fcd commit 7173fce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Argo/Extensions/NSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Foundation

extension NSNumber {
var isBool: Bool {
return CFBooleanGetTypeID() == CFGetTypeID(self)
return type(of: self) == type(of: NSNumber(value: true))
}
}
6 changes: 6 additions & 0 deletions Tests/ArgoTests/Tests/TypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ class TypeTests: XCTestCase {
XCTAssert(bools?.bool == true)
XCTAssert(bools?.number == true)
}

func testBooleanIdentification() {
let j = json(fromFile: "booleans").map(JSON.init)!
let boolean: JSON? = (j <| "realBool").value
XCTAssert(boolean == .bool(true))
}
}

0 comments on commit 7173fce

Please sign in to comment.