forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUUID+PostgresCodable.swift
87 lines (78 loc) · 2.62 KB
/
UUID+PostgresCodable.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import NIOCore
import struct Foundation.UUID
import typealias Foundation.uuid_t
extension UUID: PostgresEncodable {
public static var psqlType: PostgresDataType {
.uuid
}
public static var psqlFormat: PostgresFormat {
.binary
}
@inlinable
public func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PostgresEncodingContext<JSONEncoder>
) {
let uuid = self.uuid
byteBuffer.writeBytes([
uuid.0, uuid.1, uuid.2, uuid.3,
uuid.4, uuid.5, uuid.6, uuid.7,
uuid.8, uuid.9, uuid.10, uuid.11,
uuid.12, uuid.13, uuid.14, uuid.15,
])
}
}
extension UUID: PostgresDecodable {
@inlinable
public init<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws {
switch (format, type) {
case (.binary, .uuid):
guard let uuid = buffer.readUUID() else {
throw PostgresDecodingError.Code.failure
}
self = uuid
case (.binary, .varchar),
(.binary, .text),
(.text, .uuid),
(.text, .text),
(.text, .varchar):
guard buffer.readableBytes == 36 else {
throw PostgresDecodingError.Code.failure
}
guard let uuid = buffer.readString(length: 36).flatMap({ UUID(uuidString: $0) }) else {
throw PostgresDecodingError.Code.failure
}
self = uuid
default:
throw PostgresDecodingError.Code.typeMismatch
}
}
}
extension UUID: PostgresCodable {}
extension ByteBuffer {
@usableFromInline
mutating func readUUID() -> UUID? {
guard self.readableBytes >= MemoryLayout<uuid_t>.size else {
return nil
}
let value: UUID = self.getUUID(at: self.readerIndex)! /* must work as we have enough bytes */
// should be MoveReaderIndex
self.moveReaderIndex(forwardBy: MemoryLayout<uuid_t>.size)
return value
}
func getUUID(at index: Int) -> UUID? {
var uuid: uuid_t = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
return self.viewBytes(at: index, length: MemoryLayout.size(ofValue: uuid)).map { bufferBytes in
withUnsafeMutableBytes(of: &uuid) { target in
precondition(target.count <= bufferBytes.count)
target.copyBytes(from: bufferBytes)
}
return UUID(uuid: uuid)
}
}
}