Skip to content

Commit cb6e8a9

Browse files
authored
row.contains nil fix (vapor#162)
1 parent 8d3a5d3 commit cb6e8a9

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Sources/PostgresKit/PostgresRow+SQL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private struct _PostgreSQLRow: SQLRow {
2222
}
2323

2424
func decodeNil(column: String) throws -> Bool {
25-
self.row.column(column) == nil
25+
self.row.column(column)?.value == nil
2626
}
2727

2828
func decode<D>(column: String, as type: D.Type) throws -> D where D : Decodable {

Tests/PostgresKitTests/PostgresKitTests.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,25 @@ class PostgresKitTests: XCTestCase {
137137
struct Foo: Codable {
138138
var bar: Int
139139
}
140-
140+
}
141+
142+
func testDecodeModelWithNil() throws {
143+
let conn = try PostgresConnection.test(on: self.eventLoop).wait()
144+
defer { try! conn.close().wait() }
145+
146+
let rows = try conn.query("SELECT 'foo'::text as foo, null as bar, 'baz'::text as baz").wait()
147+
let row = rows[0]
148+
149+
struct Test: Codable {
150+
var foo: String
151+
var bar: String?
152+
var baz: String?
153+
}
154+
155+
let test = try row.sql().decode(model: Test.self)
156+
XCTAssertEqual(test.foo, "foo")
157+
XCTAssertEqual(test.bar, nil)
158+
XCTAssertEqual(test.baz, "baz")
141159
}
142160

143161
private var eventLoopGroup: EventLoopGroup!

0 commit comments

Comments
 (0)