forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLData+Bool.swift
37 lines (33 loc) · 1.23 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
extension Bool: PostgreSQLDataConvertible {
/// See `PostgreSQLDataConvertible`.
public static func convertFromPostgreSQLData(_ data: PostgreSQLData) throws -> Bool {
switch data.storage {
case .text(let value):
guard value.count == 1 else {
throw PostgreSQLError.decode(Bool.self, from: data)
}
switch value[value.startIndex] {
case "t": return true
case "f": return false
default: throw PostgreSQLError.decode(Bool.self, from: data)
}
case .binary(let value):
guard value.count == 1 else {
throw PostgreSQLError.decode(Bool.self, from: data)
}
switch value[0] {
case 1: return true
case 0: return false
default: throw PostgreSQLError.decode(Bool.self, from: data)
}
case .null: throw PostgreSQLError.decode(Bool.self, from: data)
}
}
/// See `PostgreSQLDataConvertible`.
public func convertToPostgreSQLData() throws -> PostgreSQLData {
return PostgreSQLData(.bool, binary: self ? _true : _false)
}
}
// MARK: Private
private let _true = Data([0x01])
private let _false = Data([0x00])