forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLData+Bool.swift
42 lines (36 loc) · 1.74 KB
/
PostgreSQLData+Bool.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import Foundation
extension Bool: PostgreSQLDataConvertible {
/// See `PostgreSQLDataCustomConvertible.postgreSQLDataType`
public static var postgreSQLDataType: PostgreSQLDataType { return .bool }
/// See `PostgreSQLDataCustomConvertible.postgreSQLDataArrayType`
public static var postgreSQLDataArrayType: PostgreSQLDataType { return ._bool }
/// See `PostgreSQLDataCustomConvertible.convertFromPostgreSQLData(_:)`
public static func convertFromPostgreSQLData(_ data: PostgreSQLData) throws -> Bool {
guard let value = data.data else {
throw PostgreSQLError(identifier: "bool", reason: "Could not decode String from `null` data.", source: .capture())
}
guard value.count == 1 else {
throw PostgreSQLError(identifier: "bool", reason: "Could not decode Bool from value: \(value)", source: .capture())
}
switch data.format {
case .text:
switch value[0] {
case .t: return true
case .f: return false
default: throw PostgreSQLError(identifier: "bool", reason: "Could not decode Bool from text: \(value)", source: .capture())
}
case .binary:
switch value[0] {
case 1: return true
case 0: return false
default: throw PostgreSQLError(identifier: "bool", reason: "Could not decode Bool from binary: \(value)", source: .capture())
}
}
}
/// See `PostgreSQLDataCustomConvertible.convertToPostgreSQLData()`
public func convertToPostgreSQLData() throws -> PostgreSQLData {
return PostgreSQLData(type: .bool, format: .binary, data: self ? _true : _false)
}
}
private let _true = Data([0x01])
private let _false = Data([0x00])