forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUUID+PSQLCodable.swift
60 lines (53 loc) · 2.17 KB
/
UUID+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
49
50
51
52
53
54
55
56
57
58
59
60
import struct Foundation.UUID
import typealias Foundation.uuid_t
extension UUID: PSQLCodable {
var psqlType: PSQLDataType {
.uuid
}
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
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,
])
}
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> UUID {
switch type {
case .uuid:
guard let uuid = buffer.readUUID() else {
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
}
return uuid
case .varchar, .text:
guard let uuid = buffer.readString(length: buffer.readableBytes).flatMap({ UUID(uuidString: $0) }) else {
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
}
return uuid
default:
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
}
}
}
extension ByteBuffer {
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)
}
}
}