-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathPostgresData+Bytes.swift
40 lines (35 loc) · 1.06 KB
/
PostgresData+Bytes.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
import struct Foundation.Data
import NIOCore
extension PostgresData {
public init<Bytes>(bytes: Bytes)
where Bytes: Sequence, Bytes.Element == UInt8
{
var buffer = ByteBufferAllocator().buffer(capacity: 1)
buffer.writeBytes(bytes)
self.init(type: .bytea, formatCode: .binary, value: buffer)
}
public var bytes: [UInt8]? {
guard var value = self.value else {
return nil
}
guard let bytes = value.readBytes(length: value.readableBytes) else {
return nil
}
return bytes
}
}
@available(*, deprecated, message: "Deprecating conformance to `PostgresDataConvertible`, since it is deprecated.")
extension Data: PostgresDataConvertible {
public static var postgresDataType: PostgresDataType {
return .bytea
}
public var postgresData: PostgresData? {
return .init(bytes: self)
}
public init?(postgresData: PostgresData) {
guard let bytes = postgresData.bytes else {
return nil
}
self.init(bytes)
}
}