Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PSQLDecodingContext generic #217

Merged
merged 3 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sources/PostgresNIO/New/Data/Array+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ extension Array: PSQLEncodable where Element: PSQLArrayElement {
}

extension Array: PSQLDecodable where Element: PSQLArrayElement {
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Array<Element> {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Array<Element> {
guard case .binary = format else {
// currently we only support decoding arrays in binary format.
throw PostgresCastingError.Code.failure
Expand Down
7 changes: 6 additions & 1 deletion Sources/PostgresNIO/New/Data/Bool+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ extension Bool: PSQLCodable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Bool {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
guard type == .bool else {
throw PostgresCastingError.Code.typeMismatch
}
Expand Down
14 changes: 12 additions & 2 deletions Sources/PostgresNIO/New/Data/Bytes+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ extension ByteBuffer: PSQLCodable {
byteBuffer.writeBuffer(&copyOfSelf)
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
return buffer
}
}
Expand All @@ -48,7 +53,12 @@ extension Data: PSQLCodable {
byteBuffer.writeBytes(self)
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
return buffer.readData(length: buffer.readableBytes, byteTransferStrategy: .automatic)!
}
}
7 changes: 6 additions & 1 deletion Sources/PostgresNIO/New/Data/Date+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ extension Date: PSQLCodable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch type {
case .timestamp, .timestamptz:
guard buffer.readableBytes == 8, let microseconds = buffer.readInteger(as: Int64.self) else {
Expand Down
7 changes: 6 additions & 1 deletion Sources/PostgresNIO/New/Data/Decimal+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ extension Decimal: PSQLCodable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch (format, type) {
case (.binary, .numeric):
guard let numeric = PostgresNumeric(buffer: &buffer) else {
Expand Down
14 changes: 12 additions & 2 deletions Sources/PostgresNIO/New/Data/Float+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ extension Float: PSQLCodable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch (format, type) {
case (.binary, .float4):
guard buffer.readableBytes == 4, let float = buffer.psqlReadFloat() else {
Expand Down Expand Up @@ -45,7 +50,12 @@ extension Double: PSQLCodable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch (format, type) {
case (.binary, .float4):
guard buffer.readableBytes == 4, let float = buffer.psqlReadFloat() else {
Expand Down
35 changes: 30 additions & 5 deletions Sources/PostgresNIO/New/Data/Int+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ extension UInt8: PSQLCodable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch type {
case .bpchar, .char:
guard buffer.readableBytes == 1, let value = buffer.readInteger(as: UInt8.self) else {
Expand Down Expand Up @@ -37,7 +42,12 @@ extension Int16: PSQLCodable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch (format, type) {
case (.binary, .int2):
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
Expand Down Expand Up @@ -68,7 +78,12 @@ extension Int32: PSQLCodable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch (format, type) {
case (.binary, .int2):
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
Expand Down Expand Up @@ -104,7 +119,12 @@ extension Int64: PSQLCodable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch (format, type) {
case (.binary, .int2):
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
Expand Down Expand Up @@ -152,7 +172,12 @@ extension Int: PSQLCodable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch (format, type) {
case (.binary, .int2):
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
Expand Down
7 changes: 6 additions & 1 deletion Sources/PostgresNIO/New/Data/JSON+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ extension PSQLCodable where Self: Codable {
.binary
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch (format, type) {
case (.binary, .jsonb):
guard JSONBVersionByte == buffer.readInteger(as: UInt8.self) else {
Expand Down
14 changes: 6 additions & 8 deletions Sources/PostgresNIO/New/Data/Optional+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ import NIOCore
extension Optional: PSQLDecodable where Wrapped: PSQLDecodable, Wrapped.DecodableType == Wrapped {
typealias DecodableType = Wrapped

static func decode(
static func decode<JSONDecoder: PostgresJSONDecoder>(
from byteBuffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PSQLDecodingContext
context: PostgresDecodingContext<JSONDecoder>
) throws -> Optional<Wrapped> {
preconditionFailure("This should not be called")
}

static func decodeRaw(
static func decodeRaw<JSONDecoder: PostgresJSONDecoder>(
from byteBuffer: inout ByteBuffer?,
type: PostgresDataType,
format: PostgresFormat,
context: PSQLDecodingContext
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch byteBuffer {
case .some(var buffer):
return try DecodableType.decode(from: &buffer, type: type, format: format, context: context)
case .none:
guard var buffer = byteBuffer else {
return nil
}
return try DecodableType.decode(from: &buffer, type: type, format: format, context: context)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ extension PSQLCodable where Self: RawRepresentable, RawValue: PSQLCodable {
self.rawValue.psqlFormat
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
guard let rawValue = try? RawValue.decode(from: &buffer, type: type, format: format, context: context),
let selfValue = Self.init(rawValue: rawValue) else {
throw PostgresCastingError.Code.failure
Expand Down
7 changes: 6 additions & 1 deletion Sources/PostgresNIO/New/Data/String+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ extension String: PSQLCodable {
byteBuffer.writeString(self)
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch (format, type) {
case (_, .varchar),
(_, .text),
Expand Down
7 changes: 6 additions & 1 deletion Sources/PostgresNIO/New/Data/UUID+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ extension UUID: PSQLCodable {
])
}

static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
switch (format, type) {
case (.binary, .uuid):
guard let uuid = buffer.readUUID() else {
Expand Down
36 changes: 17 additions & 19 deletions Sources/PostgresNIO/New/PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,31 @@ protocol PSQLDecodable {
/// - context: A `PSQLDecodingContext` providing context for decoding. This includes a `JSONDecoder`
/// to use when decoding json and metadata to create better errors.
/// - Returns: A decoded object
static func decode(from byteBuffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self
static func decode<JSONDecoder: PostgresJSONDecoder>(
from byteBuffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self

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

extension PSQLDecodable {
@inlinable
public static func decodeRaw(
static func decodeRaw<JSONDecoder: PostgresJSONDecoder>(
from byteBuffer: inout ByteBuffer?,
type: PostgresDataType,
format: PostgresFormat,
context: PSQLDecodingContext
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
guard var buffer = byteBuffer else {
throw PostgresCastingError.Code.missingData
Expand Down Expand Up @@ -79,22 +89,10 @@ struct PSQLEncodingContext {
let jsonEncoder: PostgresJSONEncoder
}

struct PSQLDecodingContext {
struct PostgresDecodingContext<JSONDecoder: PostgresJSONDecoder> {
let jsonDecoder: JSONDecoder

let jsonDecoder: PostgresJSONDecoder

let columnIndex: Int
let columnName: String

let file: String
let line: Int

init(jsonDecoder: PostgresJSONDecoder, columnName: String, columnIndex: Int, file: String, line: Int) {
init(jsonDecoder: JSONDecoder) {
self.jsonDecoder = jsonDecoder
self.columnName = columnName
self.columnIndex = columnIndex

self.file = file
self.line = line
}
}
7 changes: 1 addition & 6 deletions Sources/PostgresNIO/New/PSQLRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ extension PSQLRow {
precondition(index < self.data.columnCount)

let column = self.columns[index]
let context = PSQLDecodingContext(
jsonDecoder: jsonDecoder,
columnName: column.name,
columnIndex: index,
file: file,
line: line)
let context = PostgresDecodingContext(jsonDecoder: jsonDecoder)

// Safe to force unwrap here, as we have ensured above that the row has enough columns
var cellSlice = self.data[column: index]!
Expand Down
7 changes: 6 additions & 1 deletion Sources/PostgresNIO/Postgres+PSQLCompat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ extension PostgresData: PSQLEncodable {
}

extension PostgresData: PSQLDecodable {
static func decode(from buffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PSQLDecodingContext) throws -> Self {
static func decode<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
let myBuffer = buffer.readSlice(length: buffer.readableBytes)!

return PostgresData(type: PostgresDataType(UInt32(type.rawValue)), typeModifier: nil, formatCode: .binary, value: myBuffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ extension PSQLFrontendMessageEncoder {
}
}

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

Expand Down