Skip to content

Commit

Permalink
Rename PostgresCastingError to PostgresDecodingError and make pub…
Browse files Browse the repository at this point in the history
…lic (#286)

Fixes #278.

Co-authored-by: Fabian Fett <fabianfett@apple.com>
  • Loading branch information
nicholas-otto and fabianfett authored Apr 27, 2022
1 parent a89a275 commit a7a160b
Show file tree
Hide file tree
Showing 28 changed files with 306 additions and 314 deletions.
6 changes: 2 additions & 4 deletions Sources/PostgresNIO/Data/PostgresRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ extension PostgresRandomAccessRow {
var cellSlice = self.cells[index]
do {
return try T._decodeRaw(from: &cellSlice, type: column.dataType, format: column.format, context: context)
} catch let code as PostgresCastingError.Code {
throw PostgresCastingError(
} catch let code as PostgresDecodingError.Code {
throw PostgresDecodingError(
code: code,
columnName: self.columns[index].name,
columnIndex: index,
Expand Down Expand Up @@ -330,5 +330,3 @@ extension PostgresRow: Sendable {}

extension PostgresRandomAccessRow: Sendable {}
#endif


10 changes: 5 additions & 5 deletions Sources/PostgresNIO/New/Data/Array+PostgresCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ extension Array: PostgresDecodable where Element: PostgresArrayDecodable, Elemen
) throws {
guard case .binary = format else {
// currently we only support decoding arrays in binary format.
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}

guard let (isNotEmpty, b, element) = buffer.readMultipleIntegers(endianness: .big, as: (Int32, Int32, UInt32).self),
0 <= isNotEmpty, isNotEmpty <= 1, b == 0
else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}

let elementType = PostgresDataType(element)
Expand All @@ -154,19 +154,19 @@ extension Array: PostgresDecodable where Element: PostgresArrayDecodable, Elemen
expectedArrayCount > 0,
dimensions == 1
else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}

var result = Array<Element>()
result.reserveCapacity(Int(expectedArrayCount))

for _ in 0 ..< expectedArrayCount {
guard let elementLength = buffer.readInteger(as: Int32.self), elementLength >= 0 else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}

guard var elementBuffer = buffer.readSlice(length: numericCast(elementLength)) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}

let element = try Element.init(from: &elementBuffer, type: elementType, format: format, context: context)
Expand Down
12 changes: 6 additions & 6 deletions Sources/PostgresNIO/New/Data/Bool+PostgresCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ extension Bool: PostgresDecodable {
context: PostgresDecodingContext<JSONDecoder>
) throws {
guard type == .bool else {
throw PostgresCastingError.Code.typeMismatch
throw PostgresDecodingError.Code.typeMismatch
}

switch format {
case .binary:
guard buffer.readableBytes == 1 else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}

switch buffer.readInteger(as: UInt8.self) {
Expand All @@ -24,11 +24,11 @@ extension Bool: PostgresDecodable {
case .some(1):
self = true
default:
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
case .text:
guard buffer.readableBytes == 1 else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}

switch buffer.readInteger(as: UInt8.self) {
Expand All @@ -37,7 +37,7 @@ extension Bool: PostgresDecodable {
case .some(UInt8(ascii: "t")):
self = true
default:
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
}
}
Expand All @@ -47,7 +47,7 @@ extension Bool: PostgresEncodable {
public static var psqlType: PostgresDataType {
.bool
}

public static var psqlFormat: PostgresFormat {
.binary
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/PostgresNIO/New/Data/Date+PostgresCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extension Date: PostgresEncodable {
public static var psqlType: PostgresDataType {
.timestamptz
}

public static var psqlFormat: PostgresFormat {
.binary
}
Expand All @@ -18,14 +18,14 @@ extension Date: PostgresEncodable {
let seconds = self.timeIntervalSince(Self._psqlDateStart) * Double(Self._microsecondsPerSecond)
byteBuffer.writeInteger(Int64(seconds))
}

// MARK: Private Constants

@usableFromInline
static let _microsecondsPerSecond: Int64 = 1_000_000
@usableFromInline
static let _secondsInDay: Int64 = 24 * 60 * 60

/// values are stored as seconds before or after midnight 2000-01-01
@usableFromInline
static let _psqlDateStart = Date(timeIntervalSince1970: 946_684_800)
Expand All @@ -42,18 +42,18 @@ extension Date: PostgresDecodable {
switch type {
case .timestamp, .timestamptz:
guard buffer.readableBytes == 8, let microseconds = buffer.readInteger(as: Int64.self) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
let seconds = Double(microseconds) / Double(Self._microsecondsPerSecond)
self = Date(timeInterval: seconds, since: Self._psqlDateStart)
case .date:
guard buffer.readableBytes == 4, let days = buffer.readInteger(as: Int32.self) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
let seconds = Int64(days) * Self._secondsInDay
self = Date(timeInterval: Double(seconds), since: Self._psqlDateStart)
default:
throw PostgresCastingError.Code.typeMismatch
throw PostgresDecodingError.Code.typeMismatch
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/PostgresNIO/New/Data/Decimal+PostgresCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extension Decimal: PostgresEncodable {
public static var psqlType: PostgresDataType {
.numeric
}

public static var psqlFormat: PostgresFormat {
.binary
}
Expand Down Expand Up @@ -34,16 +34,16 @@ extension Decimal: PostgresDecodable {
switch (format, type) {
case (.binary, .numeric):
guard let numeric = PostgresNumeric(buffer: &buffer) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = numeric.decimal
case (.text, .numeric):
guard let string = buffer.readString(length: buffer.readableBytes), let value = Decimal(string: string) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
default:
throw PostgresCastingError.Code.typeMismatch
throw PostgresDecodingError.Code.typeMismatch
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions Sources/PostgresNIO/New/Data/Float+PostgresCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extension Float: PostgresEncodable {
public static var psqlType: PostgresDataType {
.float4
}

public static var psqlFormat: PostgresFormat {
.binary
}
Expand All @@ -29,21 +29,21 @@ extension Float: PostgresDecodable {
switch (format, type) {
case (.binary, .float4):
guard buffer.readableBytes == 4, let float = buffer.psqlReadFloat() else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = float
case (.binary, .float8):
guard buffer.readableBytes == 8, let double = buffer.psqlReadDouble() else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = Float(double)
case (.text, .float4), (.text, .float8):
guard let string = buffer.readString(length: buffer.readableBytes), let value = Float(string) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
default:
throw PostgresCastingError.Code.typeMismatch
throw PostgresDecodingError.Code.typeMismatch
}
}
}
Expand All @@ -54,7 +54,7 @@ extension Double: PostgresEncodable {
public static var psqlType: PostgresDataType {
.float8
}

public static var psqlFormat: PostgresFormat {
.binary
}
Expand All @@ -79,21 +79,21 @@ extension Double: PostgresDecodable {
switch (format, type) {
case (.binary, .float4):
guard buffer.readableBytes == 4, let float = buffer.psqlReadFloat() else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = Double(float)
case (.binary, .float8):
guard buffer.readableBytes == 8, let double = buffer.psqlReadDouble() else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = double
case (.text, .float4), (.text, .float8):
guard let string = buffer.readString(length: buffer.readableBytes), let value = Double(string) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
default:
throw PostgresCastingError.Code.typeMismatch
throw PostgresDecodingError.Code.typeMismatch
}
}
}
Expand Down
42 changes: 21 additions & 21 deletions Sources/PostgresNIO/New/Data/Int+PostgresCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ extension UInt8: PostgresDecodable {
switch type {
case .bpchar, .char:
guard buffer.readableBytes == 1, let value = buffer.readInteger(as: UInt8.self) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}

self = value
default:
throw PostgresCastingError.Code.typeMismatch
throw PostgresDecodingError.Code.typeMismatch
}
}
}
Expand Down Expand Up @@ -74,16 +74,16 @@ extension Int16: PostgresDecodable {
switch (format, type) {
case (.binary, .int2):
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
case (.text, .int2):
guard let string = buffer.readString(length: buffer.readableBytes), let value = Int16(string) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
default:
throw PostgresCastingError.Code.typeMismatch
throw PostgresDecodingError.Code.typeMismatch
}
}
}
Expand All @@ -96,7 +96,7 @@ extension Int32: PostgresEncodable {
public static var psqlType: PostgresDataType {
.int4
}

public static var psqlFormat: PostgresFormat {
.binary
}
Expand All @@ -121,21 +121,21 @@ extension Int32: PostgresDecodable {
switch (format, type) {
case (.binary, .int2):
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = Int32(value)
case (.binary, .int4):
guard buffer.readableBytes == 4, let value = buffer.readInteger(as: Int32.self) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = Int32(value)
case (.text, .int2), (.text, .int4):
guard let string = buffer.readString(length: buffer.readableBytes), let value = Int32(string) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
default:
throw PostgresCastingError.Code.typeMismatch
throw PostgresDecodingError.Code.typeMismatch
}
}
}
Expand Down Expand Up @@ -173,26 +173,26 @@ extension Int64: PostgresDecodable {
switch (format, type) {
case (.binary, .int2):
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = Int64(value)
case (.binary, .int4):
guard buffer.readableBytes == 4, let value = buffer.readInteger(as: Int32.self) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = Int64(value)
case (.binary, .int8):
guard buffer.readableBytes == 8, let value = buffer.readInteger(as: Int64.self) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
case (.text, .int2), (.text, .int4), (.text, .int8):
guard let string = buffer.readString(length: buffer.readableBytes), let value = Int64(string) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
default:
throw PostgresCastingError.Code.typeMismatch
throw PostgresDecodingError.Code.typeMismatch
}
}
}
Expand All @@ -212,7 +212,7 @@ extension Int: PostgresEncodable {
preconditionFailure("Int is expected to be an Int32 or Int64")
}
}

public static var psqlFormat: PostgresFormat {
.binary
}
Expand All @@ -237,26 +237,26 @@ extension Int: PostgresDecodable {
switch (format, type) {
case (.binary, .int2):
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = Int(value)
case (.binary, .int4):
guard buffer.readableBytes == 4, let value = buffer.readInteger(as: Int32.self).flatMap({ Int(exactly: $0) }) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
case (.binary, .int8):
guard buffer.readableBytes == 8, let value = buffer.readInteger(as: Int.self).flatMap({ Int(exactly: $0) }) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
case (.text, .int2), (.text, .int4), (.text, .int8):
guard let string = buffer.readString(length: buffer.readableBytes), let value = Int(string) else {
throw PostgresCastingError.Code.failure
throw PostgresDecodingError.Code.failure
}
self = value
default:
throw PostgresCastingError.Code.typeMismatch
throw PostgresDecodingError.Code.typeMismatch
}
}
}
Expand Down
Loading

0 comments on commit a7a160b

Please sign in to comment.