forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptional+PSQLCodable.swift
48 lines (41 loc) · 1.5 KB
/
Optional+PSQLCodable.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
42
43
44
45
46
47
48
import NIOCore
extension Optional: PSQLDecodable where Wrapped: PSQLDecodable {
static func decode(from byteBuffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Optional<Wrapped> {
preconditionFailure("This code path should never be hit.")
// The code path for decoding an optional should be:
// -> PSQLData.decode(as: String?.self)
// -> PSQLData.decodeIfPresent(String.self)
// -> String.decode(from: type:)
}
}
extension Optional: PSQLEncodable where Wrapped: PSQLEncodable {
var psqlType: PSQLDataType {
switch self {
case .some(let value):
return value.psqlType
case .none:
return .null
}
}
var psqlFormat: PSQLFormat {
switch self {
case .some(let value):
return value.psqlFormat
case .none:
return .binary
}
}
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
preconditionFailure("Should never be hit, since `encodeRaw` is implemented.")
}
func encodeRaw(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
switch self {
case .none:
byteBuffer.writeInteger(-1, as: Int32.self)
case .some(let value):
try value.encodeRaw(into: &byteBuffer, context: context)
}
}
}
extension Optional: PSQLCodable where Wrapped: PSQLCodable {
}