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 authored May 15, 2018
2 parents aa16db0 + 90874a3 commit 3bbf797
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 3bbf797

Please sign in to comment.