|
1 | 1 | import NIO
|
| 2 | +import Foundation |
2 | 3 |
|
3 | 4 | public struct PostgresData: CustomStringConvertible, CustomDebugStringConvertible {
|
4 | 5 | public static var null: PostgresData {
|
@@ -27,32 +28,54 @@ public struct PostgresData: CustomStringConvertible, CustomDebugStringConvertibl
|
27 | 28 | }
|
28 | 29 |
|
29 | 30 | public var description: String {
|
30 |
| - if let string = self.string { |
31 |
| - return string |
| 31 | + guard var value = self.value else { |
| 32 | + return "<null>" |
| 33 | + } |
| 34 | + let description: String? |
| 35 | + |
| 36 | + switch self.type { |
| 37 | + case .bool: |
| 38 | + description = self.bool?.description |
| 39 | + case .float4: |
| 40 | + description = self.float?.description |
| 41 | + case .float8, .numeric: |
| 42 | + description = self.double?.description |
| 43 | + case .int2: |
| 44 | + description = self.int16?.description |
| 45 | + case .int4, .regproc, .oid: |
| 46 | + description = self.int32?.description |
| 47 | + case .int8: |
| 48 | + description = self.int64?.description |
| 49 | + case .timestamp, .timestamptz, .date, .time, .timetz: |
| 50 | + description = self.date?.description |
| 51 | + case .text: |
| 52 | + description = self.string?.debugDescription |
| 53 | + case .textArray: |
| 54 | + description = self.array(of: String.self)?.description |
| 55 | + case .uuid: |
| 56 | + description = self.uuid?.description |
| 57 | + case .uuidArray: |
| 58 | + description = self.array(of: UUID.self)?.description |
| 59 | + default: |
| 60 | + description = nil |
| 61 | + } |
| 62 | + |
| 63 | + if let description = description { |
| 64 | + return description |
32 | 65 | } else {
|
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>" |
| 66 | + let raw: String |
| 67 | + switch self.formatCode { |
| 68 | + case .text: |
| 69 | + raw = (value.readString(length: value.readableBytes) ?? "") |
| 70 | + .debugDescription |
| 71 | + case .binary: |
| 72 | + raw = "0x" + value.readableBytesView.hexdigest() |
44 | 73 | }
|
45 |
| - return string + " (\(self.type))" |
| 74 | + return "\(raw) (\(self.type))" |
46 | 75 | }
|
47 | 76 | }
|
48 | 77 |
|
49 | 78 | public var debugDescription: String {
|
50 |
| - let valueDescription: String |
51 |
| - if let value = self.value { |
52 |
| - valueDescription = "\(value.readableBytes.description) bytes" |
53 |
| - } else { |
54 |
| - valueDescription = "nil" |
55 |
| - } |
56 |
| - return "PostgresData(type: \(self.type), value: \(valueDescription))" |
| 79 | + return self.description |
57 | 80 | }
|
58 | 81 | }
|
0 commit comments