diff --git a/Sources/PostgresNIO/Data/PostgresData+Int.swift b/Sources/PostgresNIO/Data/PostgresData+Int.swift index 57132656..a1e95c3e 100644 --- a/Sources/PostgresNIO/Data/PostgresData+Int.swift +++ b/Sources/PostgresNIO/Data/PostgresData+Int.swift @@ -115,8 +115,10 @@ private extension PostgresData { switch self.formatCode { case .binary: switch self.type { - case .char: - assert(value.readableBytes == 1) + case .char, .bpchar: + guard value.readableBytes == 1 else { + return nil + } guard let uint8 = value.getInteger(at: value.readerIndex, as: UInt8.self) else { return nil } diff --git a/Sources/PostgresNIO/Data/PostgresData+String.swift b/Sources/PostgresNIO/Data/PostgresData+String.swift index a1498671..efae01c4 100644 --- a/Sources/PostgresNIO/Data/PostgresData+String.swift +++ b/Sources/PostgresNIO/Data/PostgresData+String.swift @@ -47,7 +47,7 @@ extension PostgresData { case .int2, .int4, .int8: return self.int?.description case .bpchar: - return self.character?.description + return value.readString(length: value.readableBytes) default: if self.type.isUserDefined { // custom type diff --git a/Tests/PostgresNIOTests/PostgresNIOTests.swift b/Tests/PostgresNIOTests/PostgresNIOTests.swift index 44427350..ba055045 100644 --- a/Tests/PostgresNIOTests/PostgresNIOTests.swift +++ b/Tests/PostgresNIOTests/PostgresNIOTests.swift @@ -1077,6 +1077,23 @@ final class PostgresNIOTests: XCTestCase { } } + // https://github.com/vapor/postgres-nio/issues/71 + func testChar1Serialization() throws { + let conn = try PostgresConnection.test(on: eventLoop).wait() + defer { try! conn.close().wait() } + let rows = try conn.query(""" + select + '5'::char(1) as one, + '5'::char(2) as two + """).wait() + XCTAssertEqual(rows[0].column("one")?.uint8, 53) + XCTAssertEqual(rows[0].column("one")?.uint16, 53) + XCTAssertEqual(rows[0].column("one")?.string, "5") + XCTAssertEqual(rows[0].column("two")?.uint8, nil) + XCTAssertEqual(rows[0].column("two")?.uint16, nil) + XCTAssertEqual(rows[0].column("two")?.string, "5 ") + } + func testUserDefinedType() throws { let conn = try PostgresConnection.test(on: eventLoop).wait() defer { try! conn.close().wait() }