Skip to content

Commit e17c930

Browse files
authored
Make PSQLDecodingContext generic (vapor#217)
1 parent 3ee1245 commit e17c930

16 files changed

+135
-54
lines changed

Sources/PostgresNIO/New/Data/Array+PSQLCodable.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ extension Array: PSQLEncodable where Element: PSQLArrayElement {
101101
}
102102

103103
extension Array: PSQLDecodable where Element: PSQLArrayElement {
104-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Array<Element> {
104+
static func decode<JSONDecoder: PostgresJSONDecoder>(
105+
from buffer: inout ByteBuffer,
106+
type: PostgresDataType,
107+
format: PostgresFormat,
108+
context: PostgresDecodingContext<JSONDecoder>
109+
) throws -> Array<Element> {
105110
guard case .binary = format else {
106111
// currently we only support decoding arrays in binary format.
107112
throw PostgresCastingError.Code.failure

Sources/PostgresNIO/New/Data/Bool+PSQLCodable.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ extension Bool: PSQLCodable {
99
.binary
1010
}
1111

12-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Bool {
12+
static func decode<JSONDecoder: PostgresJSONDecoder>(
13+
from buffer: inout ByteBuffer,
14+
type: PostgresDataType,
15+
format: PostgresFormat,
16+
context: PostgresDecodingContext<JSONDecoder>
17+
) throws -> Self {
1318
guard type == .bool else {
1419
throw PostgresCastingError.Code.typeMismatch
1520
}

Sources/PostgresNIO/New/Data/Bytes+PSQLCodable.swift

+12-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ extension ByteBuffer: PSQLCodable {
3030
byteBuffer.writeBuffer(&copyOfSelf)
3131
}
3232

33-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
33+
static func decode<JSONDecoder: PostgresJSONDecoder>(
34+
from buffer: inout ByteBuffer,
35+
type: PostgresDataType,
36+
format: PostgresFormat,
37+
context: PostgresDecodingContext<JSONDecoder>
38+
) throws -> Self {
3439
return buffer
3540
}
3641
}
@@ -48,7 +53,12 @@ extension Data: PSQLCodable {
4853
byteBuffer.writeBytes(self)
4954
}
5055

51-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
56+
static func decode<JSONDecoder: PostgresJSONDecoder>(
57+
from buffer: inout ByteBuffer,
58+
type: PostgresDataType,
59+
format: PostgresFormat,
60+
context: PostgresDecodingContext<JSONDecoder>
61+
) throws -> Self {
5262
return buffer.readData(length: buffer.readableBytes, byteTransferStrategy: .automatic)!
5363
}
5464
}

Sources/PostgresNIO/New/Data/Date+PSQLCodable.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ extension Date: PSQLCodable {
1010
.binary
1111
}
1212

13-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
13+
static func decode<JSONDecoder: PostgresJSONDecoder>(
14+
from buffer: inout ByteBuffer,
15+
type: PostgresDataType,
16+
format: PostgresFormat,
17+
context: PostgresDecodingContext<JSONDecoder>
18+
) throws -> Self {
1419
switch type {
1520
case .timestamp, .timestamptz:
1621
guard buffer.readableBytes == 8, let microseconds = buffer.readInteger(as: Int64.self) else {

Sources/PostgresNIO/New/Data/Decimal+PSQLCodable.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ extension Decimal: PSQLCodable {
1010
.binary
1111
}
1212

13-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
13+
static func decode<JSONDecoder: PostgresJSONDecoder>(
14+
from buffer: inout ByteBuffer,
15+
type: PostgresDataType,
16+
format: PostgresFormat,
17+
context: PostgresDecodingContext<JSONDecoder>
18+
) throws -> Self {
1419
switch (format, type) {
1520
case (.binary, .numeric):
1621
guard let numeric = PostgresNumeric(buffer: &buffer) else {

Sources/PostgresNIO/New/Data/Float+PSQLCodable.swift

+12-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ extension Float: PSQLCodable {
99
.binary
1010
}
1111

12-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
12+
static func decode<JSONDecoder: PostgresJSONDecoder>(
13+
from buffer: inout ByteBuffer,
14+
type: PostgresDataType,
15+
format: PostgresFormat,
16+
context: PostgresDecodingContext<JSONDecoder>
17+
) throws -> Self {
1318
switch (format, type) {
1419
case (.binary, .float4):
1520
guard buffer.readableBytes == 4, let float = buffer.psqlReadFloat() else {
@@ -45,7 +50,12 @@ extension Double: PSQLCodable {
4550
.binary
4651
}
4752

48-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
53+
static func decode<JSONDecoder: PostgresJSONDecoder>(
54+
from buffer: inout ByteBuffer,
55+
type: PostgresDataType,
56+
format: PostgresFormat,
57+
context: PostgresDecodingContext<JSONDecoder>
58+
) throws -> Self {
4959
switch (format, type) {
5060
case (.binary, .float4):
5161
guard buffer.readableBytes == 4, let float = buffer.psqlReadFloat() else {

Sources/PostgresNIO/New/Data/Int+PSQLCodable.swift

+30-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ extension UInt8: PSQLCodable {
99
.binary
1010
}
1111

12-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
12+
static func decode<JSONDecoder: PostgresJSONDecoder>(
13+
from buffer: inout ByteBuffer,
14+
type: PostgresDataType,
15+
format: PostgresFormat,
16+
context: PostgresDecodingContext<JSONDecoder>
17+
) throws -> Self {
1318
switch type {
1419
case .bpchar, .char:
1520
guard buffer.readableBytes == 1, let value = buffer.readInteger(as: UInt8.self) else {
@@ -37,7 +42,12 @@ extension Int16: PSQLCodable {
3742
.binary
3843
}
3944

40-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
45+
static func decode<JSONDecoder: PostgresJSONDecoder>(
46+
from buffer: inout ByteBuffer,
47+
type: PostgresDataType,
48+
format: PostgresFormat,
49+
context: PostgresDecodingContext<JSONDecoder>
50+
) throws -> Self {
4151
switch (format, type) {
4252
case (.binary, .int2):
4353
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
@@ -68,7 +78,12 @@ extension Int32: PSQLCodable {
6878
.binary
6979
}
7080

71-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
81+
static func decode<JSONDecoder: PostgresJSONDecoder>(
82+
from buffer: inout ByteBuffer,
83+
type: PostgresDataType,
84+
format: PostgresFormat,
85+
context: PostgresDecodingContext<JSONDecoder>
86+
) throws -> Self {
7287
switch (format, type) {
7388
case (.binary, .int2):
7489
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
@@ -104,7 +119,12 @@ extension Int64: PSQLCodable {
104119
.binary
105120
}
106121

107-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
122+
static func decode<JSONDecoder: PostgresJSONDecoder>(
123+
from buffer: inout ByteBuffer,
124+
type: PostgresDataType,
125+
format: PostgresFormat,
126+
context: PostgresDecodingContext<JSONDecoder>
127+
) throws -> Self {
108128
switch (format, type) {
109129
case (.binary, .int2):
110130
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
@@ -152,7 +172,12 @@ extension Int: PSQLCodable {
152172
.binary
153173
}
154174

155-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
175+
static func decode<JSONDecoder: PostgresJSONDecoder>(
176+
from buffer: inout ByteBuffer,
177+
type: PostgresDataType,
178+
format: PostgresFormat,
179+
context: PostgresDecodingContext<JSONDecoder>
180+
) throws -> Self {
156181
switch (format, type) {
157182
case (.binary, .int2):
158183
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {

Sources/PostgresNIO/New/Data/JSON+PSQLCodable.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ extension PSQLCodable where Self: Codable {
1414
.binary
1515
}
1616

17-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
17+
static func decode<JSONDecoder: PostgresJSONDecoder>(
18+
from buffer: inout ByteBuffer,
19+
type: PostgresDataType,
20+
format: PostgresFormat,
21+
context: PostgresDecodingContext<JSONDecoder>
22+
) throws -> Self {
1823
switch (format, type) {
1924
case (.binary, .jsonb):
2025
guard JSONBVersionByte == buffer.readInteger(as: UInt8.self) else {

Sources/PostgresNIO/New/Data/Optional+PSQLCodable.swift

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,25 @@ import NIOCore
33
extension Optional: PSQLDecodable where Wrapped: PSQLDecodable, Wrapped.DecodableType == Wrapped {
44
typealias DecodableType = Wrapped
55

6-
static func decode(
6+
static func decode<JSONDecoder: PostgresJSONDecoder>(
77
from byteBuffer: inout ByteBuffer,
88
type: PostgresDataType,
99
format: PostgresFormat,
10-
context: PSQLDecodingContext
10+
context: PostgresDecodingContext<JSONDecoder>
1111
) throws -> Optional<Wrapped> {
1212
preconditionFailure("This should not be called")
1313
}
1414

15-
static func decodeRaw(
15+
static func decodeRaw<JSONDecoder: PostgresJSONDecoder>(
1616
from byteBuffer: inout ByteBuffer?,
1717
type: PostgresDataType,
1818
format: PostgresFormat,
19-
context: PSQLDecodingContext
19+
context: PostgresDecodingContext<JSONDecoder>
2020
) throws -> Self {
21-
switch byteBuffer {
22-
case .some(var buffer):
23-
return try DecodableType.decode(from: &buffer, type: type, format: format, context: context)
24-
case .none:
21+
guard var buffer = byteBuffer else {
2522
return nil
2623
}
24+
return try DecodableType.decode(from: &buffer, type: type, format: format, context: context)
2725
}
2826
}
2927

Sources/PostgresNIO/New/Data/RawRepresentable+PSQLCodable.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ extension PSQLCodable where Self: RawRepresentable, RawValue: PSQLCodable {
99
self.rawValue.psqlFormat
1010
}
1111

12-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
12+
static func decode<JSONDecoder: PostgresJSONDecoder>(
13+
from buffer: inout ByteBuffer,
14+
type: PostgresDataType,
15+
format: PostgresFormat,
16+
context: PostgresDecodingContext<JSONDecoder>
17+
) throws -> Self {
1318
guard let rawValue = try? RawValue.decode(from: &buffer, type: type, format: format, context: context),
1419
let selfValue = Self.init(rawValue: rawValue) else {
1520
throw PostgresCastingError.Code.failure

Sources/PostgresNIO/New/Data/String+PSQLCodable.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ extension String: PSQLCodable {
1414
byteBuffer.writeString(self)
1515
}
1616

17-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
17+
static func decode<JSONDecoder: PostgresJSONDecoder>(
18+
from buffer: inout ByteBuffer,
19+
type: PostgresDataType,
20+
format: PostgresFormat,
21+
context: PostgresDecodingContext<JSONDecoder>
22+
) throws -> Self {
1823
switch (format, type) {
1924
case (_, .varchar),
2025
(_, .text),

Sources/PostgresNIO/New/Data/UUID+PSQLCodable.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ extension UUID: PSQLCodable {
2222
])
2323
}
2424

25-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
25+
static func decode<JSONDecoder: PostgresJSONDecoder>(
26+
from buffer: inout ByteBuffer,
27+
type: PostgresDataType,
28+
format: PostgresFormat,
29+
context: PostgresDecodingContext<JSONDecoder>
30+
) throws -> Self {
2631
switch (format, type) {
2732
case (.binary, .uuid):
2833
guard let uuid = buffer.readUUID() else {

Sources/PostgresNIO/New/PSQLCodable.swift

+17-19
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,31 @@ protocol PSQLDecodable {
3333
/// - context: A `PSQLDecodingContext` providing context for decoding. This includes a `JSONDecoder`
3434
/// to use when decoding json and metadata to create better errors.
3535
/// - Returns: A decoded object
36-
static func decode(from byteBuffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self
36+
static func decode<JSONDecoder: PostgresJSONDecoder>(
37+
from byteBuffer: inout ByteBuffer,
38+
type: PostgresDataType,
39+
format: PostgresFormat,
40+
context: PostgresDecodingContext<JSONDecoder>
41+
) throws -> Self
3742

3843
/// Decode an entity from the `byteBuffer` in postgres wire format.
3944
/// This method has a default implementation and may be overriden
4045
/// only for special cases, like `Optional`s.
41-
static func decodeRaw(from byteBuffer: inout ByteBuffer?, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self
46+
static func decodeRaw<JSONDecoder: PostgresJSONDecoder>(
47+
from byteBuffer: inout ByteBuffer?,
48+
type: PostgresDataType,
49+
format: PostgresFormat,
50+
context: PostgresDecodingContext<JSONDecoder>
51+
) throws -> Self
4252
}
4353

4454
extension PSQLDecodable {
4555
@inlinable
46-
public static func decodeRaw(
56+
static func decodeRaw<JSONDecoder: PostgresJSONDecoder>(
4757
from byteBuffer: inout ByteBuffer?,
4858
type: PostgresDataType,
4959
format: PostgresFormat,
50-
context: PSQLDecodingContext
60+
context: PostgresDecodingContext<JSONDecoder>
5161
) throws -> Self {
5262
guard var buffer = byteBuffer else {
5363
throw PostgresCastingError.Code.missingData
@@ -79,22 +89,10 @@ struct PSQLEncodingContext {
7989
let jsonEncoder: PostgresJSONEncoder
8090
}
8191

82-
struct PSQLDecodingContext {
92+
struct PostgresDecodingContext<JSONDecoder: PostgresJSONDecoder> {
93+
let jsonDecoder: JSONDecoder
8394

84-
let jsonDecoder: PostgresJSONDecoder
85-
86-
let columnIndex: Int
87-
let columnName: String
88-
89-
let file: String
90-
let line: Int
91-
92-
init(jsonDecoder: PostgresJSONDecoder, columnName: String, columnIndex: Int, file: String, line: Int) {
95+
init(jsonDecoder: JSONDecoder) {
9396
self.jsonDecoder = jsonDecoder
94-
self.columnName = columnName
95-
self.columnIndex = columnIndex
96-
97-
self.file = file
98-
self.line = line
9997
}
10098
}

Sources/PostgresNIO/New/PSQLRow.swift

+1-6
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ extension PSQLRow {
4848
precondition(index < self.data.columnCount)
4949

5050
let column = self.columns[index]
51-
let context = PSQLDecodingContext(
52-
jsonDecoder: jsonDecoder,
53-
columnName: column.name,
54-
columnIndex: index,
55-
file: file,
56-
line: line)
51+
let context = PostgresDecodingContext(jsonDecoder: jsonDecoder)
5752

5853
// Safe to force unwrap here, as we have ensured above that the row has enough columns
5954
var cellSlice = self.data[column: index]!

Sources/PostgresNIO/Postgres+PSQLCompat.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ extension PostgresData: PSQLEncodable {
2626
}
2727

2828
extension PostgresData: PSQLDecodable {
29-
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
29+
static func decode<JSONDecoder: PostgresJSONDecoder>(
30+
from buffer: inout ByteBuffer,
31+
type: PostgresDataType,
32+
format: PostgresFormat,
33+
context: PostgresDecodingContext<JSONDecoder>
34+
) throws -> Self {
3035
let myBuffer = buffer.readSlice(length: buffer.readableBytes)!
3136

3237
return PostgresData(type: PostgresDataType(UInt32(type.rawValue)), typeModifier: nil, formatCode: .binary, value: myBuffer)

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ extension PSQLFrontendMessageEncoder {
77
}
88
}
99

10-
extension PSQLDecodingContext {
11-
static func forTests(columnName: String = "unknown", columnIndex: Int = 0, jsonDecoder: PostgresJSONDecoder = JSONDecoder(), file: String = #file, line: Int = #line) -> Self {
12-
Self(jsonDecoder: JSONDecoder(), columnName: columnName, columnIndex: columnIndex, file: file, line: line)
10+
extension PostgresDecodingContext where JSONDecoder == Foundation.JSONDecoder {
11+
static func forTests() -> Self {
12+
Self(jsonDecoder: JSONDecoder())
1313
}
1414
}
1515

0 commit comments

Comments
 (0)