forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresData+UUID.swift
48 lines (42 loc) · 1.23 KB
/
PostgresData+UUID.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 Foundation
import NIOCore
extension PostgresData {
public init(uuid: UUID) {
var buffer = ByteBufferAllocator().buffer(capacity: 16)
buffer.writeUUIDBytes(uuid)
self.init(type: .uuid, formatCode: .binary, value: buffer)
}
public var uuid: UUID? {
guard var value = self.value else {
return nil
}
switch self.formatCode {
case .binary:
switch self.type {
case .uuid:
return value.readUUIDBytes()
case .varchar, .text:
return self.string.flatMap { UUID(uuidString: $0) }
default:
return nil
}
case .text:
return nil
}
}
}
@available(*, deprecated, message: "Deprecating conformance to `PostgresDataConvertible`, since it is deprecated.")
extension UUID: PostgresDataConvertible {
public static var postgresDataType: PostgresDataType {
return .uuid
}
public init?(postgresData: PostgresData) {
guard let uuid = postgresData.uuid else {
return nil
}
self = uuid
}
public var postgresData: PostgresData? {
return .init(uuid: self)
}
}