Skip to content

Commit

Permalink
Made Bool conforming to SQLiteDataConvertible
Browse files Browse the repository at this point in the history
  • Loading branch information
eLud committed May 10, 2018
1 parent a4a40e8 commit 1f9d60c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/SQLite/Data/SQLiteData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ extension OptionalType {

extension Optional: SQLiteDataConvertible { }

extension Bool: SQLiteDataConvertible {

/// See `SQLiteDataConvertible.convertFromSQLiteData(_:)`
public static func convertFromSQLiteData(_ data: SQLiteData) throws -> Bool {
switch data {
case .integer(let intValue):
let boolValue = intValue == 0 ? false : true
return boolValue
default: throw SQLiteError(problem: .warning, reason: "Could not convert to Bool: \(data)", source: .capture())
}
}

/// See `convertToSQLiteData()`
public func convertToSQLiteData() throws -> SQLiteData {
let intValue = self ? 1 : 0
return SQLiteData.integer(intValue)
}
}

func requireDataCustomConvertible<T>(_ type: T) -> SQLiteDataConvertible {
guard let custom = type as? SQLiteDataConvertible else {
fatalError("`\(T.self)` does not conform to `SQLiteDataConvertible`")
Expand Down

0 comments on commit 1f9d60c

Please sign in to comment.