Skip to content

Commit 7173fce

Browse files
committed
Improve the way we detect Boolean values
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.
1 parent 0216fcd commit 7173fce

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Sources/Argo/Extensions/NSNumber.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import Foundation
22

33
extension NSNumber {
44
var isBool: Bool {
5-
return CFBooleanGetTypeID() == CFGetTypeID(self)
5+
return type(of: self) == type(of: NSNumber(value: true))
66
}
77
}

Tests/ArgoTests/Tests/TypeTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ class TypeTests: XCTestCase {
4141
XCTAssert(bools?.bool == true)
4242
XCTAssert(bools?.number == true)
4343
}
44+
45+
func testBooleanIdentification() {
46+
let j = json(fromFile: "booleans").map(JSON.init)!
47+
let boolean: JSON? = (j <| "realBool").value
48+
XCTAssert(boolean == .bool(true))
49+
}
4450
}

0 commit comments

Comments
 (0)