Skip to content

Commit 7660f79

Browse files
authored
Remove PSQLJSONEncoder (vapor#215)
1 parent eaef208 commit 7660f79

10 files changed

+23
-32
lines changed

Sources/PostgresNIO/Connection/PostgresConnection+Connect.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension PostgresConnection {
1212
) -> EventLoopFuture<PostgresConnection> {
1313

1414
let coders = PSQLConnection.Configuration.Coders(
15-
jsonEncoder: PostgresJSONEncoderWrapper(_defaultJSONEncoder)
15+
jsonEncoder: _defaultJSONEncoder
1616
)
1717

1818
let configuration = PSQLConnection.Configuration(

Sources/PostgresNIO/New/Messages/Bind.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension PSQLFrontendMessage {
1212
/// The number of parameter values that follow (possibly zero). This must match the number of parameters needed by the query.
1313
var parameters: [PSQLEncodable]
1414

15-
func encode(into buffer: inout ByteBuffer, using jsonEncoder: PSQLJSONEncoder) throws {
15+
func encode(into buffer: inout ByteBuffer, using jsonEncoder: PostgresJSONEncoder) throws {
1616
buffer.writeNullTerminatedString(self.portalName)
1717
buffer.writeNullTerminatedString(self.preparedStatementName)
1818

Sources/PostgresNIO/New/PSQL+JSON.swift

-10
This file was deleted.

Sources/PostgresNIO/New/PSQLCodable.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extension PSQLEncodable {
5555
}
5656

5757
struct PSQLEncodingContext {
58-
let jsonEncoder: PSQLJSONEncoder
58+
let jsonEncoder: PostgresJSONEncoder
5959
}
6060

6161
struct PSQLDecodingContext {

Sources/PostgresNIO/New/PSQLConnection.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ final class PSQLConnection {
1313
struct Configuration {
1414

1515
struct Coders {
16-
var jsonEncoder: PSQLJSONEncoder
16+
var jsonEncoder: PostgresJSONEncoder
1717

18-
init(jsonEncoder: PSQLJSONEncoder) {
18+
init(jsonEncoder: PostgresJSONEncoder) {
1919
self.jsonEncoder = jsonEncoder
2020
}
2121

Sources/PostgresNIO/New/PSQLFrontendMessageEncoder.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
struct PSQLFrontendMessageEncoder: MessageToByteEncoder {
33
typealias OutboundIn = PSQLFrontendMessage
44

5-
let jsonEncoder: PSQLJSONEncoder
5+
let jsonEncoder: PostgresJSONEncoder
66

7-
init(jsonEncoder: PSQLJSONEncoder) {
7+
init(jsonEncoder: PostgresJSONEncoder) {
88
self.jsonEncoder = jsonEncoder
99
}
1010

Sources/PostgresNIO/Postgres+PSQLCompat.swift

-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
import NIOCore
22

3-
struct PostgresJSONEncoderWrapper: PSQLJSONEncoder {
4-
let downstream: PostgresJSONEncoder
5-
6-
init(_ downstream: PostgresJSONEncoder) {
7-
self.downstream = downstream
8-
}
9-
10-
func encode<T>(_ value: T, into buffer: inout ByteBuffer) throws where T : Encodable {
11-
let data = try self.downstream.encode(value)
12-
buffer.writeData(data)
13-
}
14-
}
15-
163
extension PostgresData: PSQLEncodable {
174
var psqlType: PostgresDataType {
185
self.type

Sources/PostgresNIO/Utilities/PostgresJSONEncoder.swift

+10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import Foundation
2+
import NIOFoundationCompat
23

34
/// A protocol that mimicks the Foundation `JSONEncoder.encode(_:)` function.
45
/// Conform a non-Foundation JSON encoder to this protocol if you want PostgresNIO to be
56
/// able to use it when encoding JSON & JSONB values (see `PostgresNIO._defaultJSONEncoder`)
67
public protocol PostgresJSONEncoder {
78
func encode<T>(_ value: T) throws -> Data where T : Encodable
9+
10+
func encode<T: Encodable>(_ value: T, into buffer: inout ByteBuffer) throws
11+
}
12+
13+
extension PostgresJSONEncoder {
14+
public func encode<T: Encodable>(_ value: T, into buffer: inout ByteBuffer) throws {
15+
let data = try self.encode(value)
16+
buffer.writeData(data)
17+
}
818
}
919

1020
extension JSONEncoder: PostgresJSONEncoder {}

Tests/PostgresNIOTests/New/Data/JSON+PSQLCodableTests.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@ class JSON_PSQLCodableTests: XCTestCase {
7373
}
7474

7575
func testCustomEncoderIsUsed() {
76-
class TestEncoder: PSQLJSONEncoder {
76+
class TestEncoder: PostgresJSONEncoder {
7777
var encodeHits = 0
7878

7979
func encode<T>(_ value: T, into buffer: inout ByteBuffer) throws where T : Encodable {
8080
self.encodeHits += 1
8181
}
82+
83+
func encode<T>(_ value: T) throws -> Data where T : Encodable {
84+
preconditionFailure()
85+
}
8286
}
8387

8488
let hello = Hello(name: "world")

Tests/PostgresNIOTests/New/Extensions/PSQLCoding+TestUtils.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension PSQLDecodingContext {
1414
}
1515

1616
extension PSQLEncodingContext {
17-
static func forTests(jsonEncoder: PSQLJSONEncoder = JSONEncoder()) -> Self {
17+
static func forTests(jsonEncoder: PostgresJSONEncoder = JSONEncoder()) -> Self {
1818
Self(jsonEncoder: jsonEncoder)
1919
}
2020
}

0 commit comments

Comments
 (0)