Skip to content

Commit 13ae955

Browse files
authored
improve postgres data descriptions + add string array tests (#49)
1 parent 985a3f5 commit 13ae955

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed
Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import NIO
2+
import Foundation
23

34
public struct PostgresData: CustomStringConvertible, CustomDebugStringConvertible {
45
public static var null: PostgresData {
@@ -27,32 +28,54 @@ public struct PostgresData: CustomStringConvertible, CustomDebugStringConvertibl
2728
}
2829

2930
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
3265
} 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()
4473
}
45-
return string + " (\(self.type))"
74+
return "\(raw) (\(self.type))"
4675
}
4776
}
4877

4978
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
5780
}
5881
}

Tests/PostgresNIOTests/NIOPostgresTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,36 @@ final class NIOPostgresTests: XCTestCase {
526526
XCTAssertEqual("stringInTable2", row.column("t2_stringValue")?.string, "stringInTable2")
527527
XCTAssertEqual(dateInTable2, row.column("t2_dateValue")?.date)
528528
}
529+
530+
func testStringArrays() throws {
531+
let query = """
532+
SELECT
533+
$1::uuid as "id",
534+
$2::bigint as "revision",
535+
$3::timestamp as "updated_at",
536+
$4::timestamp as "created_at",
537+
$5::text as "name",
538+
$6::text[] as "countries",
539+
$7::text[] as "languages",
540+
$8::text[] as "currencies"
541+
"""
542+
543+
let conn = try PostgresConnection.test(on: eventLoop).wait()
544+
defer { try! conn.close().wait() }
545+
let rows = try conn.query(query, [
546+
PostgresData(uuid: UUID(uuidString: "D2710E16-EB07-4FD6-A87E-B1BE41C9BD3D")!),
547+
PostgresData(int: Int(0)),
548+
PostgresData(date: Date(timeIntervalSince1970: 0)),
549+
PostgresData(date: Date(timeIntervalSince1970: 0)),
550+
PostgresData(string: "Foo"),
551+
PostgresData(array: ["US"]),
552+
PostgresData(array: ["en"]),
553+
PostgresData(array: ["USD", "DKK"]),
554+
]).wait()
555+
XCTAssertEqual(rows[0].column("countries")?.array(of: String.self), ["US"])
556+
XCTAssertEqual(rows[0].column("languages")?.array(of: String.self), ["en"])
557+
XCTAssertEqual(rows[0].column("currencies")?.array(of: String.self), ["USD", "DKK"])
558+
}
529559

530560
// MARK: Performance
531561

Tests/PostgresNIOTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extension NIOPostgresTests {
3939
("testSelectTypes", testSelectTypes),
4040
("testSimpleQueryVersion", testSimpleQueryVersion),
4141
("testSQLError", testSQLError),
42+
("testStringArrays", testStringArrays),
4243
("testUUID", testUUID),
4344
]
4445
}

0 commit comments

Comments
 (0)