Skip to content

Commit 0b5c400

Browse files
authored
Remove PSQLJSONDecoder (vapor#216)
1 parent 55d6b9d commit 0b5c400

File tree

6 files changed

+18
-26
lines changed

6 files changed

+18
-26
lines changed

Sources/PostgresNIO/New/PSQL+JSON.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,4 @@ protocol PSQLJSONEncoder {
77
func encode<T: Encodable>(_ value: T, into buffer: inout ByteBuffer) throws
88
}
99

10-
protocol PSQLJSONDecoder {
11-
func decode<T: Decodable>(_ type: T.Type, from buffer: ByteBuffer) throws -> T
12-
}
13-
1410
extension JSONEncoder: PSQLJSONEncoder {}
15-
extension JSONDecoder: PSQLJSONDecoder {}
16-

Sources/PostgresNIO/New/PSQLCodable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ struct PSQLEncodingContext {
6060

6161
struct PSQLDecodingContext {
6262

63-
let jsonDecoder: PSQLJSONDecoder
63+
let jsonDecoder: PostgresJSONDecoder
6464

6565
let columnIndex: Int
6666
let columnName: String
6767

6868
let file: String
6969
let line: Int
7070

71-
init(jsonDecoder: PSQLJSONDecoder, columnName: String, columnIndex: Int, file: String, line: Int) {
71+
init(jsonDecoder: PostgresJSONDecoder, columnName: String, columnIndex: Int, file: String, line: Int) {
7272
self.jsonDecoder = jsonDecoder
7373
self.columnName = columnName
7474
self.columnIndex = columnIndex

Sources/PostgresNIO/New/PSQLRow.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension PSQLRow {
2929
/// - type: The type to decode the data into
3030
/// - Throws: The error of the decoding implementation. See also `PSQLDecodable` protocol for this.
3131
/// - Returns: The decoded value of Type T.
32-
func decode<T: PSQLDecodable, JSONDecoder: PSQLJSONDecoder>(column: String, as type: T.Type, jsonDecoder: JSONDecoder, file: String = #file, line: Int = #line) throws -> T {
32+
func decode<T: PSQLDecodable, JSONDecoder: PostgresJSONDecoder>(column: String, as type: T.Type, jsonDecoder: JSONDecoder, file: String = #file, line: Int = #line) throws -> T {
3333
guard let index = self.lookupTable[column] else {
3434
preconditionFailure("A column '\(column)' does not exist.")
3535
}
@@ -44,7 +44,7 @@ extension PSQLRow {
4444
/// - type: The type to decode the data into
4545
/// - Throws: The error of the decoding implementation. See also `PSQLDecodable` protocol for this.
4646
/// - Returns: The decoded value of Type T.
47-
func decode<T: PSQLDecodable, JSONDecoder: PSQLJSONDecoder>(column index: Int, as type: T.Type, jsonDecoder: JSONDecoder, file: String = #file, line: Int = #line) throws -> T {
47+
func decode<T: PSQLDecodable, JSONDecoder: PostgresJSONDecoder>(column index: Int, as type: T.Type, jsonDecoder: JSONDecoder, file: String = #file, line: Int = #line) throws -> T {
4848
precondition(index < self.data.columnCount)
4949

5050
let column = self.columns[index]

Sources/PostgresNIO/Postgres+PSQLCompat.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
import NIOCore
22

3-
struct PostgresJSONDecoderWrapper: PSQLJSONDecoder {
4-
let downstream: PostgresJSONDecoder
5-
6-
init(_ downstream: PostgresJSONDecoder) {
7-
self.downstream = downstream
8-
}
9-
10-
func decode<T>(_ type: T.Type, from buffer: ByteBuffer) throws -> T where T : Decodable {
11-
var buffer = buffer
12-
let data = buffer.readData(length: buffer.readableBytes)!
13-
return try self.downstream.decode(T.self, from: data)
14-
}
15-
}
16-
173
struct PostgresJSONEncoderWrapper: PSQLJSONEncoder {
184
let downstream: PostgresJSONEncoder
195

Sources/PostgresNIO/Utilities/PostgresJSONDecoder.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
import Foundation
1+
import class Foundation.JSONDecoder
2+
import struct Foundation.Data
3+
import NIOFoundationCompat
24

35
/// A protocol that mimicks the Foundation `JSONDecoder.decode(_:from:)` function.
46
/// Conform a non-Foundation JSON decoder to this protocol if you want PostgresNIO to be
57
/// able to use it when decoding JSON & JSONB values (see `PostgresNIO._defaultJSONDecoder`)
68
public protocol PostgresJSONDecoder {
79
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable
10+
11+
func decode<T: Decodable>(_ type: T.Type, from buffer: ByteBuffer) throws -> T
12+
}
13+
14+
extension PostgresJSONDecoder {
15+
public func decode<T: Decodable>(_ type: T.Type, from buffer: ByteBuffer) throws -> T {
16+
var copy = buffer
17+
let data = copy.readData(length: buffer.readableBytes)!
18+
return try self.decode(type, from: data)
19+
}
820
}
921

1022
extension JSONDecoder: PostgresJSONDecoder {}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extension PSQLFrontendMessageEncoder {
88
}
99

1010
extension PSQLDecodingContext {
11-
static func forTests(columnName: String = "unknown", columnIndex: Int = 0, jsonDecoder: PSQLJSONDecoder = JSONDecoder(), file: String = #file, line: Int = #line) -> Self {
11+
static func forTests(columnName: String = "unknown", columnIndex: Int = 0, jsonDecoder: PostgresJSONDecoder = JSONDecoder(), file: String = #file, line: Int = #line) -> Self {
1212
Self(jsonDecoder: JSONDecoder(), columnName: columnName, columnIndex: columnIndex, file: file, line: line)
1313
}
1414
}

0 commit comments

Comments
 (0)