Skip to content

Commit 37694b6

Browse files
authored
array of double fixes (#65)
* fix double serialization in arrays * add double array test
1 parent 4dbce11 commit 37694b6

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

Sources/PostgresNIO/Data/PostgresData+Double.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
extension PostgresData {
22
public init(double: Double) {
33
var buffer = ByteBufferAllocator().buffer(capacity: 0)
4-
buffer.writeString(double.description)
5-
self.init(type: .float8, formatCode: .text, value: buffer)
4+
buffer.writeDouble(double)
5+
self.init(type: .float8, formatCode: .binary, value: buffer)
66
}
77

88
public var double: Double? {

Sources/PostgresNIO/Data/PostgresData.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public struct PostgresData: CustomStringConvertible, CustomDebugStringConvertibl
5656
description = self.uuid?.description
5757
case .uuidArray:
5858
description = self.array(of: UUID.self)?.description
59+
case .int8Array:
60+
description = self.array(of: Int.self)?.description
61+
case .float8Array:
62+
description = self.array(of: Double.self)?.description
63+
case .float4Array:
64+
description = self.array(of: Float.self)?.description
65+
case .jsonb:
66+
description = String(decoding: value.readableBytesView, as: UTF8.self)
5967
default:
6068
description = nil
6169
}

Sources/PostgresNIO/Utilities/NIOUtils.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ internal extension ByteBuffer {
7474
return self.readInteger(as: UInt64.self).map { Double(bitPattern: $0) }
7575
}
7676

77+
mutating func writeFloat(_ float: Float) {
78+
self.writeInteger(float.bitPattern)
79+
}
80+
81+
mutating func writeDouble(_ double: Double) {
82+
self.writeInteger(double.bitPattern)
83+
}
84+
7785
mutating func readUUID() -> UUID? {
7886
guard self.readableBytes >= MemoryLayout<UUID>.size else {
7987
return nil

Tests/PostgresNIOTests/NIOPostgresTests.swift renamed to Tests/PostgresNIOTests/PostgresNIOTests.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Logging
22
import PostgresNIO
33
import XCTest
44

5-
final class NIOPostgresTests: XCTestCase {
5+
final class PostgresNIOTests: XCTestCase {
66
private var group: EventLoopGroup!
77
private var eventLoop: EventLoop {
88
return self.group.next()
@@ -853,6 +853,20 @@ final class NIOPostgresTests: XCTestCase {
853853
}
854854
}
855855
}
856+
857+
func testDoubleArraySerialization() throws {
858+
let conn = try PostgresConnection.test(on: eventLoop).wait()
859+
defer { try! conn.close().wait() }
860+
let doubles: [Double] = [3.14, 42]
861+
let rows = try conn.query("""
862+
select
863+
$1::double precision[] as doubles
864+
""", [
865+
.init(array: doubles)
866+
]).wait()
867+
XCTAssertEqual(rows[0].column("doubles")?.array(of: Double.self), doubles)
868+
}
869+
856870
}
857871

858872

0 commit comments

Comments
 (0)