Skip to content

Commit 020658a

Browse files
authored
bytes (vapor#38)
* bytes * update linuxmain
1 parent aa528ef commit 020658a

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import struct Foundation.Data
2+
3+
extension PostgresData {
4+
public init<Bytes>(bytes: Bytes)
5+
where Bytes: Sequence, Bytes.Element == UInt8
6+
{
7+
var buffer = ByteBufferAllocator().buffer(capacity: 1)
8+
buffer.writeBytes(bytes)
9+
self.init(type: .bytea, formatCode: .binary, value: buffer)
10+
}
11+
12+
public var bytes: [UInt8]? {
13+
guard var value = self.value else {
14+
return nil
15+
}
16+
guard let bytes = value.readBytes(length: value.readableBytes) else {
17+
return nil
18+
}
19+
return bytes
20+
}
21+
}
22+
23+
extension Data: PostgresDataConvertible {
24+
public static var postgresDataType: PostgresDataType {
25+
return .bytea
26+
}
27+
28+
public var postgresData: PostgresData? {
29+
return .init(bytes: self)
30+
}
31+
32+
public init?(postgresData: PostgresData) {
33+
guard let bytes = postgresData.bytes else {
34+
return nil
35+
}
36+
self.init(bytes)
37+
}
38+
}

Tests/PostgresNIOTests/NIOPostgresTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,15 @@ final class NIOPostgresTests: XCTestCase {
386386
XCTAssertEqual(rows[0].column("bool")?.bool, false)
387387
}
388388
}
389+
390+
func testBytesSerialize() throws {
391+
let conn = try PostgresConnection.test(on: eventLoop).wait()
392+
defer { try! conn.close().wait() }
393+
let rows = try conn.query("select $1::bytea as bytes", [
394+
PostgresData(bytes: [1, 2, 3])
395+
]).wait()
396+
XCTAssertEqual(rows[0].column("bytes")?.bytes, [1, 2, 3])
397+
}
389398

390399
func testJSONBSerialize() throws {
391400
struct Object: Codable {

Tests/PostgresNIOTests/XCTestManifests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ extension NIOPostgresTests {
99
("testAverageLengthNumeric", testAverageLengthNumeric),
1010
("testBindInteger", testBindInteger),
1111
("testBoolSerialize", testBoolSerialize),
12-
("testJSONBSerialize", testJSONBSerialize),
13-
("testJSONBConvertible", testJSONBConvertible),
12+
("testBytesSerialize", testBytesSerialize),
1413
("testColumnsInJoin", testColumnsInJoin),
1514
("testConnectAndClose", testConnectAndClose),
1615
("testDates", testDates),
@@ -20,6 +19,8 @@ extension NIOPostgresTests {
2019
("testIntegerArraySerialize", testIntegerArraySerialize),
2120
("testIntegers", testIntegers),
2221
("testInvalidPassword", testInvalidPassword),
22+
("testJSONBConvertible", testJSONBConvertible),
23+
("testJSONBSerialize", testJSONBSerialize),
2324
("testMoney", testMoney),
2425
("testNullIntegerArrayParse", testNullIntegerArrayParse),
2526
("testNumericParsing", testNumericParsing),

0 commit comments

Comments
 (0)