Skip to content

Commit 7a6536f

Browse files
authored
UUID fixes (#31)
* implement CustomDebugStringConvertible * add UUID PostgresDataConvertible conformance * add Optional PostgresDataConvertible conformance * add missing return statement * don't run linux-perf test by default
1 parent 62e6a72 commit 7a6536f

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
extension Optional: PostgresDataConvertible where Wrapped: PostgresDataConvertible {
2+
public static var postgresDataType: PostgresDataType {
3+
return Wrapped.postgresDataType
4+
}
5+
6+
public init?(postgresData: PostgresData) {
7+
self = Wrapped.init(postgresData: postgresData)
8+
}
9+
10+
public var postgresData: PostgresData? {
11+
switch self {
12+
case .some(let wrapped):
13+
return wrapped.postgresData
14+
case .none:
15+
return PostgresData.null
16+
}
17+
}
18+
}

Sources/PostgresNIO/Data/PostgresData+UUID.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,20 @@ extension PostgresData {
2020
return value.readUUID()
2121
}
2222
}
23+
24+
extension UUID: PostgresDataConvertible {
25+
public static var postgresDataType: PostgresDataType {
26+
return .uuid
27+
}
28+
29+
public init?(postgresData: PostgresData) {
30+
guard let uuid = postgresData.uuid else {
31+
return nil
32+
}
33+
self = uuid
34+
}
35+
36+
public var postgresData: PostgresData? {
37+
return .init(uuid: self)
38+
}
39+
}

Sources/PostgresNIO/Data/PostgresData.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import NIO
22

3-
public struct PostgresData: CustomStringConvertible {
3+
public struct PostgresData: CustomStringConvertible, CustomDebugStringConvertible {
44
public static var null: PostgresData {
55
return .init(type: .null)
66
}
@@ -30,23 +30,29 @@ public struct PostgresData: CustomStringConvertible {
3030
if let string = self.string {
3131
return string
3232
} else {
33-
return self.debugDescription
33+
let string: String
34+
if var value = self.value {
35+
switch self.formatCode {
36+
case .text:
37+
let raw = value.readString(length: value.readableBytes) ?? ""
38+
string = "\"\(raw)\""
39+
case .binary:
40+
string = "0x" + value.readableBytesView.hexdigest()
41+
}
42+
} else {
43+
string = "<null>"
44+
}
45+
return string + " (\(self.type))"
3446
}
3547
}
36-
37-
var debugDescription: String {
38-
let string: String
39-
if var value = self.value {
40-
switch self.formatCode {
41-
case .text:
42-
let raw = value.readString(length: value.readableBytes) ?? ""
43-
string = "\"\(raw)\""
44-
case .binary:
45-
string = "0x" + value.readableBytesView.hexdigest()
46-
}
48+
49+
public var debugDescription: String {
50+
let valueDescription: String
51+
if let value = self.value {
52+
valueDescription = "\(value.readableBytes.description) bytes"
4753
} else {
48-
string = "<null>"
54+
valueDescription = "nil"
4955
}
50-
return string + " (\(self.type))"
56+
return "PostgresData(type: \(self.type), value: \(valueDescription))"
5157
}
5258
}

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ workflows:
6666
- 10-bionic
6767
- 9-bionic
6868
- linux-release
69-
- linux-performance
69+
# - linux-performance

0 commit comments

Comments
 (0)