Skip to content

Commit a7a160b

Browse files
Rename PostgresCastingError to PostgresDecodingError and make public (vapor#286)
Fixes vapor#278. Co-authored-by: Fabian Fett <fabianfett@apple.com>
1 parent a89a275 commit a7a160b

28 files changed

+306
-314
lines changed

Sources/PostgresNIO/Data/PostgresRow.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ extension PostgresRandomAccessRow {
250250
var cellSlice = self.cells[index]
251251
do {
252252
return try T._decodeRaw(from: &cellSlice, type: column.dataType, format: column.format, context: context)
253-
} catch let code as PostgresCastingError.Code {
254-
throw PostgresCastingError(
253+
} catch let code as PostgresDecodingError.Code {
254+
throw PostgresDecodingError(
255255
code: code,
256256
columnName: self.columns[index].name,
257257
columnIndex: index,
@@ -330,5 +330,3 @@ extension PostgresRow: Sendable {}
330330

331331
extension PostgresRandomAccessRow: Sendable {}
332332
#endif
333-
334-

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ extension Array: PostgresDecodable where Element: PostgresArrayDecodable, Elemen
134134
) throws {
135135
guard case .binary = format else {
136136
// currently we only support decoding arrays in binary format.
137-
throw PostgresCastingError.Code.failure
137+
throw PostgresDecodingError.Code.failure
138138
}
139139

140140
guard let (isNotEmpty, b, element) = buffer.readMultipleIntegers(endianness: .big, as: (Int32, Int32, UInt32).self),
141141
0 <= isNotEmpty, isNotEmpty <= 1, b == 0
142142
else {
143-
throw PostgresCastingError.Code.failure
143+
throw PostgresDecodingError.Code.failure
144144
}
145145

146146
let elementType = PostgresDataType(element)
@@ -154,19 +154,19 @@ extension Array: PostgresDecodable where Element: PostgresArrayDecodable, Elemen
154154
expectedArrayCount > 0,
155155
dimensions == 1
156156
else {
157-
throw PostgresCastingError.Code.failure
157+
throw PostgresDecodingError.Code.failure
158158
}
159159

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

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

168168
guard var elementBuffer = buffer.readSlice(length: numericCast(elementLength)) else {
169-
throw PostgresCastingError.Code.failure
169+
throw PostgresDecodingError.Code.failure
170170
}
171171

172172
let element = try Element.init(from: &elementBuffer, type: elementType, format: format, context: context)

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ extension Bool: PostgresDecodable {
99
context: PostgresDecodingContext<JSONDecoder>
1010
) throws {
1111
guard type == .bool else {
12-
throw PostgresCastingError.Code.typeMismatch
12+
throw PostgresDecodingError.Code.typeMismatch
1313
}
1414

1515
switch format {
1616
case .binary:
1717
guard buffer.readableBytes == 1 else {
18-
throw PostgresCastingError.Code.failure
18+
throw PostgresDecodingError.Code.failure
1919
}
2020

2121
switch buffer.readInteger(as: UInt8.self) {
@@ -24,11 +24,11 @@ extension Bool: PostgresDecodable {
2424
case .some(1):
2525
self = true
2626
default:
27-
throw PostgresCastingError.Code.failure
27+
throw PostgresDecodingError.Code.failure
2828
}
2929
case .text:
3030
guard buffer.readableBytes == 1 else {
31-
throw PostgresCastingError.Code.failure
31+
throw PostgresDecodingError.Code.failure
3232
}
3333

3434
switch buffer.readInteger(as: UInt8.self) {
@@ -37,7 +37,7 @@ extension Bool: PostgresDecodable {
3737
case .some(UInt8(ascii: "t")):
3838
self = true
3939
default:
40-
throw PostgresCastingError.Code.failure
40+
throw PostgresDecodingError.Code.failure
4141
}
4242
}
4343
}
@@ -47,7 +47,7 @@ extension Bool: PostgresEncodable {
4747
public static var psqlType: PostgresDataType {
4848
.bool
4949
}
50-
50+
5151
public static var psqlFormat: PostgresFormat {
5252
.binary
5353
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extension Date: PostgresEncodable {
55
public static var psqlType: PostgresDataType {
66
.timestamptz
77
}
8-
8+
99
public static var psqlFormat: PostgresFormat {
1010
.binary
1111
}
@@ -18,14 +18,14 @@ extension Date: PostgresEncodable {
1818
let seconds = self.timeIntervalSince(Self._psqlDateStart) * Double(Self._microsecondsPerSecond)
1919
byteBuffer.writeInteger(Int64(seconds))
2020
}
21-
21+
2222
// MARK: Private Constants
2323

2424
@usableFromInline
2525
static let _microsecondsPerSecond: Int64 = 1_000_000
2626
@usableFromInline
2727
static let _secondsInDay: Int64 = 24 * 60 * 60
28-
28+
2929
/// values are stored as seconds before or after midnight 2000-01-01
3030
@usableFromInline
3131
static let _psqlDateStart = Date(timeIntervalSince1970: 946_684_800)
@@ -42,18 +42,18 @@ extension Date: PostgresDecodable {
4242
switch type {
4343
case .timestamp, .timestamptz:
4444
guard buffer.readableBytes == 8, let microseconds = buffer.readInteger(as: Int64.self) else {
45-
throw PostgresCastingError.Code.failure
45+
throw PostgresDecodingError.Code.failure
4646
}
4747
let seconds = Double(microseconds) / Double(Self._microsecondsPerSecond)
4848
self = Date(timeInterval: seconds, since: Self._psqlDateStart)
4949
case .date:
5050
guard buffer.readableBytes == 4, let days = buffer.readInteger(as: Int32.self) else {
51-
throw PostgresCastingError.Code.failure
51+
throw PostgresDecodingError.Code.failure
5252
}
5353
let seconds = Int64(days) * Self._secondsInDay
5454
self = Date(timeInterval: Double(seconds), since: Self._psqlDateStart)
5555
default:
56-
throw PostgresCastingError.Code.typeMismatch
56+
throw PostgresDecodingError.Code.typeMismatch
5757
}
5858
}
5959
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extension Decimal: PostgresEncodable {
55
public static var psqlType: PostgresDataType {
66
.numeric
77
}
8-
8+
99
public static var psqlFormat: PostgresFormat {
1010
.binary
1111
}
@@ -34,16 +34,16 @@ extension Decimal: PostgresDecodable {
3434
switch (format, type) {
3535
case (.binary, .numeric):
3636
guard let numeric = PostgresNumeric(buffer: &buffer) else {
37-
throw PostgresCastingError.Code.failure
37+
throw PostgresDecodingError.Code.failure
3838
}
3939
self = numeric.decimal
4040
case (.text, .numeric):
4141
guard let string = buffer.readString(length: buffer.readableBytes), let value = Decimal(string: string) else {
42-
throw PostgresCastingError.Code.failure
42+
throw PostgresDecodingError.Code.failure
4343
}
4444
self = value
4545
default:
46-
throw PostgresCastingError.Code.typeMismatch
46+
throw PostgresDecodingError.Code.typeMismatch
4747
}
4848
}
4949
}

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extension Float: PostgresEncodable {
44
public static var psqlType: PostgresDataType {
55
.float4
66
}
7-
7+
88
public static var psqlFormat: PostgresFormat {
99
.binary
1010
}
@@ -29,21 +29,21 @@ extension Float: PostgresDecodable {
2929
switch (format, type) {
3030
case (.binary, .float4):
3131
guard buffer.readableBytes == 4, let float = buffer.psqlReadFloat() else {
32-
throw PostgresCastingError.Code.failure
32+
throw PostgresDecodingError.Code.failure
3333
}
3434
self = float
3535
case (.binary, .float8):
3636
guard buffer.readableBytes == 8, let double = buffer.psqlReadDouble() else {
37-
throw PostgresCastingError.Code.failure
37+
throw PostgresDecodingError.Code.failure
3838
}
3939
self = Float(double)
4040
case (.text, .float4), (.text, .float8):
4141
guard let string = buffer.readString(length: buffer.readableBytes), let value = Float(string) else {
42-
throw PostgresCastingError.Code.failure
42+
throw PostgresDecodingError.Code.failure
4343
}
4444
self = value
4545
default:
46-
throw PostgresCastingError.Code.typeMismatch
46+
throw PostgresDecodingError.Code.typeMismatch
4747
}
4848
}
4949
}
@@ -54,7 +54,7 @@ extension Double: PostgresEncodable {
5454
public static var psqlType: PostgresDataType {
5555
.float8
5656
}
57-
57+
5858
public static var psqlFormat: PostgresFormat {
5959
.binary
6060
}
@@ -79,21 +79,21 @@ extension Double: PostgresDecodable {
7979
switch (format, type) {
8080
case (.binary, .float4):
8181
guard buffer.readableBytes == 4, let float = buffer.psqlReadFloat() else {
82-
throw PostgresCastingError.Code.failure
82+
throw PostgresDecodingError.Code.failure
8383
}
8484
self = Double(float)
8585
case (.binary, .float8):
8686
guard buffer.readableBytes == 8, let double = buffer.psqlReadDouble() else {
87-
throw PostgresCastingError.Code.failure
87+
throw PostgresDecodingError.Code.failure
8888
}
8989
self = double
9090
case (.text, .float4), (.text, .float8):
9191
guard let string = buffer.readString(length: buffer.readableBytes), let value = Double(string) else {
92-
throw PostgresCastingError.Code.failure
92+
throw PostgresDecodingError.Code.failure
9393
}
9494
self = value
9595
default:
96-
throw PostgresCastingError.Code.typeMismatch
96+
throw PostgresDecodingError.Code.typeMismatch
9797
}
9898
}
9999
}

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

+21-21
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ extension UInt8: PostgresDecodable {
3131
switch type {
3232
case .bpchar, .char:
3333
guard buffer.readableBytes == 1, let value = buffer.readInteger(as: UInt8.self) else {
34-
throw PostgresCastingError.Code.failure
34+
throw PostgresDecodingError.Code.failure
3535
}
3636

3737
self = value
3838
default:
39-
throw PostgresCastingError.Code.typeMismatch
39+
throw PostgresDecodingError.Code.typeMismatch
4040
}
4141
}
4242
}
@@ -74,16 +74,16 @@ extension Int16: PostgresDecodable {
7474
switch (format, type) {
7575
case (.binary, .int2):
7676
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
77-
throw PostgresCastingError.Code.failure
77+
throw PostgresDecodingError.Code.failure
7878
}
7979
self = value
8080
case (.text, .int2):
8181
guard let string = buffer.readString(length: buffer.readableBytes), let value = Int16(string) else {
82-
throw PostgresCastingError.Code.failure
82+
throw PostgresDecodingError.Code.failure
8383
}
8484
self = value
8585
default:
86-
throw PostgresCastingError.Code.typeMismatch
86+
throw PostgresDecodingError.Code.typeMismatch
8787
}
8888
}
8989
}
@@ -96,7 +96,7 @@ extension Int32: PostgresEncodable {
9696
public static var psqlType: PostgresDataType {
9797
.int4
9898
}
99-
99+
100100
public static var psqlFormat: PostgresFormat {
101101
.binary
102102
}
@@ -121,21 +121,21 @@ extension Int32: PostgresDecodable {
121121
switch (format, type) {
122122
case (.binary, .int2):
123123
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
124-
throw PostgresCastingError.Code.failure
124+
throw PostgresDecodingError.Code.failure
125125
}
126126
self = Int32(value)
127127
case (.binary, .int4):
128128
guard buffer.readableBytes == 4, let value = buffer.readInteger(as: Int32.self) else {
129-
throw PostgresCastingError.Code.failure
129+
throw PostgresDecodingError.Code.failure
130130
}
131131
self = Int32(value)
132132
case (.text, .int2), (.text, .int4):
133133
guard let string = buffer.readString(length: buffer.readableBytes), let value = Int32(string) else {
134-
throw PostgresCastingError.Code.failure
134+
throw PostgresDecodingError.Code.failure
135135
}
136136
self = value
137137
default:
138-
throw PostgresCastingError.Code.typeMismatch
138+
throw PostgresDecodingError.Code.typeMismatch
139139
}
140140
}
141141
}
@@ -173,26 +173,26 @@ extension Int64: PostgresDecodable {
173173
switch (format, type) {
174174
case (.binary, .int2):
175175
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
176-
throw PostgresCastingError.Code.failure
176+
throw PostgresDecodingError.Code.failure
177177
}
178178
self = Int64(value)
179179
case (.binary, .int4):
180180
guard buffer.readableBytes == 4, let value = buffer.readInteger(as: Int32.self) else {
181-
throw PostgresCastingError.Code.failure
181+
throw PostgresDecodingError.Code.failure
182182
}
183183
self = Int64(value)
184184
case (.binary, .int8):
185185
guard buffer.readableBytes == 8, let value = buffer.readInteger(as: Int64.self) else {
186-
throw PostgresCastingError.Code.failure
186+
throw PostgresDecodingError.Code.failure
187187
}
188188
self = value
189189
case (.text, .int2), (.text, .int4), (.text, .int8):
190190
guard let string = buffer.readString(length: buffer.readableBytes), let value = Int64(string) else {
191-
throw PostgresCastingError.Code.failure
191+
throw PostgresDecodingError.Code.failure
192192
}
193193
self = value
194194
default:
195-
throw PostgresCastingError.Code.typeMismatch
195+
throw PostgresDecodingError.Code.typeMismatch
196196
}
197197
}
198198
}
@@ -212,7 +212,7 @@ extension Int: PostgresEncodable {
212212
preconditionFailure("Int is expected to be an Int32 or Int64")
213213
}
214214
}
215-
215+
216216
public static var psqlFormat: PostgresFormat {
217217
.binary
218218
}
@@ -237,26 +237,26 @@ extension Int: PostgresDecodable {
237237
switch (format, type) {
238238
case (.binary, .int2):
239239
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
240-
throw PostgresCastingError.Code.failure
240+
throw PostgresDecodingError.Code.failure
241241
}
242242
self = Int(value)
243243
case (.binary, .int4):
244244
guard buffer.readableBytes == 4, let value = buffer.readInteger(as: Int32.self).flatMap({ Int(exactly: $0) }) else {
245-
throw PostgresCastingError.Code.failure
245+
throw PostgresDecodingError.Code.failure
246246
}
247247
self = value
248248
case (.binary, .int8):
249249
guard buffer.readableBytes == 8, let value = buffer.readInteger(as: Int.self).flatMap({ Int(exactly: $0) }) else {
250-
throw PostgresCastingError.Code.failure
250+
throw PostgresDecodingError.Code.failure
251251
}
252252
self = value
253253
case (.text, .int2), (.text, .int4), (.text, .int8):
254254
guard let string = buffer.readString(length: buffer.readableBytes), let value = Int(string) else {
255-
throw PostgresCastingError.Code.failure
255+
throw PostgresDecodingError.Code.failure
256256
}
257257
self = value
258258
default:
259-
throw PostgresCastingError.Code.typeMismatch
259+
throw PostgresDecodingError.Code.typeMismatch
260260
}
261261
}
262262
}

0 commit comments

Comments
 (0)