Skip to content

Commit c05368e

Browse files
committed
encoder fixes
1 parent 4b25340 commit c05368e

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Sources/PostgresKit/PostgresDataEncoder.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ import Foundation
33
public struct PostgresDataEncoder {
44
public init() { }
55

6-
public func encode(_ type: Encodable) throws -> PostgresData {
7-
if let custom = type as? PostgresDataConvertible {
8-
return custom.postgresData ?? .null
6+
public func encode(_ value: Encodable) throws -> PostgresData {
7+
if let custom = value as? PostgresDataConvertible {
8+
return custom.postgresData!
99
} else {
1010
do {
1111
let encoder = _Encoder()
12-
try type.encode(to: encoder)
12+
try value.encode(to: encoder)
1313
return encoder.data
1414
} catch is DoJSON {
1515
let json = JSONEncoder()
16-
let data = try json.encode(Wrapper(type))
16+
let data = try json.encode(Wrapper(value))
1717
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
18-
#warning("TODO: use nio foundation compat write")
1918
buffer.writeBytes(data)
2019
return PostgresData(type: .jsonb, value: buffer)
2120
}
@@ -175,7 +174,11 @@ public struct PostgresDataEncoder {
175174
}
176175

177176
mutating func encode<T>(_ value: T) throws where T : Encodable {
178-
try value.encode(to: self.encoder)
177+
if let value = value as? PostgresDataConvertible, let data = value.postgresData {
178+
self.encoder.data = data
179+
} else {
180+
try value.encode(to: self.encoder)
181+
}
179182
}
180183
}
181184
}

0 commit comments

Comments
 (0)