Skip to content

Commit ee024b7

Browse files
authored
Use NIOFoundationCompat for UUID <-> ByteBuffer (vapor#319)
1 parent 7daf026 commit ee024b7

File tree

4 files changed

+6
-42
lines changed

4 files changed

+6
-42
lines changed

Diff for: Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let package = Package(
1414
],
1515
dependencies: [
1616
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.2"),
17-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.42.0"),
17+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.44.0"),
1818
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.13.1"),
1919
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.22.1"),
2020
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),

Diff for: Sources/PostgresNIO/Data/PostgresData+String.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension PostgresData {
2222
case .numeric:
2323
return self.numeric?.string
2424
case .uuid:
25-
return value.readUUID()!.uuidString
25+
return value.readUUIDBytes()!.uuidString
2626
case .timestamp, .timestamptz, .date:
2727
return self.date?.description
2828
case .money:

Diff for: Sources/PostgresNIO/Data/PostgresData+UUID.swift

+2-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import NIOCore
44
extension PostgresData {
55
public init(uuid: UUID) {
66
var buffer = ByteBufferAllocator().buffer(capacity: 16)
7-
buffer.writeBytes([
8-
uuid.uuid.0, uuid.uuid.1, uuid.uuid.2, uuid.uuid.3,
9-
uuid.uuid.4, uuid.uuid.5, uuid.uuid.6, uuid.uuid.7,
10-
uuid.uuid.8, uuid.uuid.9, uuid.uuid.10, uuid.uuid.11,
11-
uuid.uuid.12, uuid.uuid.13, uuid.uuid.14, uuid.uuid.15,
12-
])
7+
buffer.writeUUIDBytes(uuid)
138
self.init(type: .uuid, formatCode: .binary, value: buffer)
149
}
1510

@@ -22,7 +17,7 @@ extension PostgresData {
2217
case .binary:
2318
switch self.type {
2419
case .uuid:
25-
return value.readUUID()
20+
return value.readUUIDBytes()
2621
case .varchar, .text:
2722
return self.string.flatMap { UUID(uuidString: $0) }
2823
default:

Diff for: Sources/PostgresNIO/New/Data/UUID+PostgresCodable.swift

+2-33
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ extension UUID: PostgresEncodable {
1616
into byteBuffer: inout ByteBuffer,
1717
context: PostgresEncodingContext<JSONEncoder>
1818
) {
19-
let uuid = self.uuid
20-
byteBuffer.writeBytes([
21-
uuid.0, uuid.1, uuid.2, uuid.3,
22-
uuid.4, uuid.5, uuid.6, uuid.7,
23-
uuid.8, uuid.9, uuid.10, uuid.11,
24-
uuid.12, uuid.13, uuid.14, uuid.15,
25-
])
19+
byteBuffer.writeUUIDBytes(self)
2620
}
2721
}
2822

@@ -36,7 +30,7 @@ extension UUID: PostgresDecodable {
3630
) throws {
3731
switch (format, type) {
3832
case (.binary, .uuid):
39-
guard let uuid = buffer.readUUID() else {
33+
guard let uuid = buffer.readUUIDBytes() else {
4034
throw PostgresDecodingError.Code.failure
4135
}
4236
self = uuid
@@ -60,28 +54,3 @@ extension UUID: PostgresDecodable {
6054
}
6155

6256
extension UUID: PostgresCodable {}
63-
64-
extension ByteBuffer {
65-
@usableFromInline
66-
mutating func readUUID() -> UUID? {
67-
guard self.readableBytes >= MemoryLayout<uuid_t>.size else {
68-
return nil
69-
}
70-
71-
let value: UUID = self.getUUID(at: self.readerIndex)! /* must work as we have enough bytes */
72-
// should be MoveReaderIndex
73-
self.moveReaderIndex(forwardBy: MemoryLayout<uuid_t>.size)
74-
return value
75-
}
76-
77-
func getUUID(at index: Int) -> UUID? {
78-
var uuid: uuid_t = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
79-
return self.viewBytes(at: index, length: MemoryLayout.size(ofValue: uuid)).map { bufferBytes in
80-
withUnsafeMutableBytes(of: &uuid) { target in
81-
precondition(target.count <= bufferBytes.count)
82-
target.copyBytes(from: bufferBytes)
83-
}
84-
return UUID(uuid: uuid)
85-
}
86-
}
87-
}

0 commit comments

Comments
 (0)