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

Chnage logic of Treat logical strings as truthy values #60

Open
wants to merge 1 commit into
base: f/logical_bool
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions Sources/BetterCodable/LosslessValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,27 @@ public struct LosslessBooleanStrategy<Value: LosslessStringCodable>: LosslessDec
func decodeTruthyString() -> (Decoder) -> LosslessStringCodable? {
return {
(try? String.init(from: $0)).flatMap { value in
switch value {
case "true", "yes", "1", "y", "t": return true
default: return false

if ["true", "yes", "1", "y", "t"].contains(value.lowercased()) {
return true
}
else {
let tmp = NSNumber(value: ((Int(value) ?? 0) > 0))
return Bool(truncating:tmp)
}
}
}
}

@inline(__always)
func decodeBoolFromNSNumber() -> (Decoder) -> LosslessStringCodable? {
return { (try? Int.init(from: $0)).flatMap { Bool(exactly: NSNumber(value: $0)) } }
return { (try? Int.init(from: $0)).flatMap { Bool(exactly: NSNumber(value: ($0 > 0)) ) } }
}

return [
decodeTruthyString(),
decodeBoolFromNSNumber(),
decode(Bool.self),
decode(Int.self),
decode(Int8.self),
decode(Int16.self),
decode(Int64.self),
decode(UInt.self),
decode(UInt8.self),
decode(UInt16.self),
decode(UInt64.self),
decode(Double.self),
decode(Float.self),

]
}
}
Expand Down
11 changes: 9 additions & 2 deletions Tests/BetterCodableTests/LosslessValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,31 @@ class LosslessValueTests: XCTestCase {
@LosslessBoolValue var c: Bool
@LosslessBoolValue var d: Bool
@LosslessBoolValue var e: Bool
@LosslessBoolValue var f: Bool
@LosslessBoolValue var g: Bool
}

let json = #"{ "a": "true", "b": "yes", "c": "1", "d": "y", "e": "t" }"#.data(using: .utf8)!
let json = #"{ "a": "TRUE", "b": "yes", "c": "1", "d": "y", "e": "t","f":"11", "g":11 }"#.data(using: .utf8)!

let result = try JSONDecoder().decode(Response.self, from: json)

XCTAssertEqual(result.a, true)
XCTAssertEqual(result.b, true)
XCTAssertEqual(result.c, true)
XCTAssertEqual(result.d, true)
XCTAssertEqual(result.e, true)
XCTAssertEqual(result.f, true)
XCTAssertEqual(result.g, true)

let json2 = #"{ "a": "false", "b": "no", "c": "0", "d": "n", "e": "f" }"#.data(using: .utf8)!
let json2 = #"{ "a": "FALSE", "b": "no", "c": "0", "d": "n", "e": "f","f":"-11", "g":-11 }"#.data(using: .utf8)!
let result2 = try JSONDecoder().decode(Response.self, from: json2)

XCTAssertEqual(result2.a, false)
XCTAssertEqual(result2.b, false)
XCTAssertEqual(result2.c, false)
XCTAssertEqual(result2.d, false)
XCTAssertEqual(result2.e, false)
XCTAssertEqual(result2.f, false)
XCTAssertEqual(result2.g, false)
}
}