Skip to content

Commit

Permalink
Merge pull request #36 from eLud/master
Browse files Browse the repository at this point in the history
Made Bool conforming to SQLiteDataConvertible
  • Loading branch information
tanner0101 committed May 15, 2018
2 parents a4a40e8 + 1f9d60c commit 1ce3af0
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/SQLite/Data/SQLiteData.swift
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 1ce3af0

Please sign in to comment.