From 7173fce8d1adee208df5656d2ca4a4353459e416 Mon Sep 17 00:00:00 2001 From: Gordon Fontenot Date: Sun, 6 Nov 2016 08:02:09 -0600 Subject: [PATCH] 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. --- Sources/Argo/Extensions/NSNumber.swift | 2 +- Tests/ArgoTests/Tests/TypeTests.swift | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Sources/Argo/Extensions/NSNumber.swift b/Sources/Argo/Extensions/NSNumber.swift index 71888dd..88701fc 100644 --- a/Sources/Argo/Extensions/NSNumber.swift +++ b/Sources/Argo/Extensions/NSNumber.swift @@ -2,6 +2,6 @@ import Foundation extension NSNumber { var isBool: Bool { - return CFBooleanGetTypeID() == CFGetTypeID(self) + return type(of: self) == type(of: NSNumber(value: true)) } } diff --git a/Tests/ArgoTests/Tests/TypeTests.swift b/Tests/ArgoTests/Tests/TypeTests.swift index b870072..8a5abf0 100644 --- a/Tests/ArgoTests/Tests/TypeTests.swift +++ b/Tests/ArgoTests/Tests/TypeTests.swift @@ -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)) + } }