Skip to content

Commit ef425af

Browse files
authored
Cleanup PostgresDecodable (vapor#241)
1 parent d16467d commit ef425af

22 files changed

+346
-269
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ extension Array: PostgresEncodable where Element: PSQLArrayElement {
104104
}
105105

106106
extension Array: PostgresDecodable where Element: PSQLArrayElement {
107-
static func decode<JSONDecoder: PostgresJSONDecoder>(
107+
init<JSONDecoder: PostgresJSONDecoder>(
108108
from buffer: inout ByteBuffer,
109109
type: PostgresDataType,
110110
format: PostgresFormat,
111111
context: PostgresDecodingContext<JSONDecoder>
112-
) throws -> Array<Element> {
112+
) throws {
113113
guard case .binary = format else {
114114
// currently we only support decoding arrays in binary format.
115115
throw PostgresCastingError.Code.failure
@@ -124,7 +124,8 @@ extension Array: PostgresDecodable where Element: PSQLArrayElement {
124124
let elementType = PostgresDataType(element)
125125

126126
guard isNotEmpty == 1 else {
127-
return []
127+
self = []
128+
return
128129
}
129130

130131
guard let (expectedArrayCount, dimensions) = buffer.readMultipleIntegers(endianness: .big, as: (Int32, Int32).self),
@@ -146,12 +147,12 @@ extension Array: PostgresDecodable where Element: PSQLArrayElement {
146147
throw PostgresCastingError.Code.failure
147148
}
148149

149-
let element = try Element.decode(from: &elementBuffer, type: elementType, format: format, context: context)
150+
let element = try Element.init(from: &elementBuffer, type: elementType, format: format, context: context)
150151

151152
result.append(element)
152153
}
153154

154-
return result
155+
self = result
155156
}
156157
}
157158

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,55 @@
11
import NIOCore
22

3-
extension Bool: PostgresCodable {
4-
var psqlType: PostgresDataType {
5-
.bool
6-
}
7-
8-
var psqlFormat: PostgresFormat {
9-
.binary
10-
}
11-
12-
static func decode<JSONDecoder: PostgresJSONDecoder>(
3+
extension Bool: PostgresDecodable {
4+
init<JSONDecoder: PostgresJSONDecoder>(
135
from buffer: inout ByteBuffer,
146
type: PostgresDataType,
157
format: PostgresFormat,
168
context: PostgresDecodingContext<JSONDecoder>
17-
) throws -> Self {
9+
) throws {
1810
guard type == .bool else {
1911
throw PostgresCastingError.Code.typeMismatch
2012
}
21-
13+
2214
switch format {
2315
case .binary:
2416
guard buffer.readableBytes == 1 else {
2517
throw PostgresCastingError.Code.failure
2618
}
27-
19+
2820
switch buffer.readInteger(as: UInt8.self) {
2921
case .some(0):
30-
return false
22+
self = false
3123
case .some(1):
32-
return true
24+
self = true
3325
default:
3426
throw PostgresCastingError.Code.failure
3527
}
3628
case .text:
3729
guard buffer.readableBytes == 1 else {
3830
throw PostgresCastingError.Code.failure
3931
}
40-
32+
4133
switch buffer.readInteger(as: UInt8.self) {
4234
case .some(UInt8(ascii: "f")):
43-
return false
35+
self = false
4436
case .some(UInt8(ascii: "t")):
45-
return true
37+
self = true
4638
default:
4739
throw PostgresCastingError.Code.failure
4840
}
4941
}
5042
}
43+
}
44+
45+
extension Bool: PostgresEncodable {
46+
var psqlType: PostgresDataType {
47+
.bool
48+
}
49+
50+
var psqlFormat: PostgresFormat {
51+
.binary
52+
}
5153

5254
func encode<JSONEncoder: PostgresJSONEncoder>(
5355
into byteBuffer: inout ByteBuffer,
@@ -56,3 +58,5 @@ extension Bool: PostgresCodable {
5658
byteBuffer.writeInteger(self ? 1 : 0, as: UInt8.self)
5759
}
5860
}
61+
62+
extension Bool: PostgresCodable {}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension PostgresEncodable where Self: Sequence, Self.Element == UInt8 {
1919
}
2020
}
2121

22-
extension ByteBuffer: PostgresCodable {
22+
extension ByteBuffer: PostgresEncodable {
2323
var psqlType: PostgresDataType {
2424
.bytea
2525
}
@@ -35,18 +35,22 @@ extension ByteBuffer: PostgresCodable {
3535
var copyOfSelf = self // dirty hack
3636
byteBuffer.writeBuffer(&copyOfSelf)
3737
}
38+
}
3839

39-
static func decode<JSONDecoder: PostgresJSONDecoder>(
40+
extension ByteBuffer: PostgresDecodable {
41+
init<JSONDecoder: PostgresJSONDecoder>(
4042
from buffer: inout ByteBuffer,
4143
type: PostgresDataType,
4244
format: PostgresFormat,
4345
context: PostgresDecodingContext<JSONDecoder>
44-
) throws -> Self {
45-
return buffer
46+
) {
47+
self = buffer
4648
}
4749
}
4850

49-
extension Data: PostgresCodable {
51+
extension ByteBuffer: PostgresCodable {}
52+
53+
extension Data: PostgresEncodable {
5054
var psqlType: PostgresDataType {
5155
.bytea
5256
}
@@ -61,13 +65,17 @@ extension Data: PostgresCodable {
6165
) {
6266
byteBuffer.writeBytes(self)
6367
}
68+
}
6469

65-
static func decode<JSONDecoder: PostgresJSONDecoder>(
70+
extension Data: PostgresDecodable {
71+
init<JSONDecoder: PostgresJSONDecoder>(
6672
from buffer: inout ByteBuffer,
6773
type: PostgresDataType,
6874
format: PostgresFormat,
6975
context: PostgresDecodingContext<JSONDecoder>
70-
) throws -> Self {
71-
return buffer.readData(length: buffer.readableBytes, byteTransferStrategy: .automatic)!
76+
) {
77+
self = buffer.readData(length: buffer.readableBytes, byteTransferStrategy: .automatic)!
7278
}
7379
}
80+
81+
extension Data: PostgresCodable {}
Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import NIOCore
22
import struct Foundation.Date
33

4-
extension Date: PostgresCodable {
4+
extension Date: PostgresEncodable {
55
var psqlType: PostgresDataType {
66
.timestamptz
77
}
@@ -10,44 +10,47 @@ extension Date: PostgresCodable {
1010
.binary
1111
}
1212

13-
static func decode<JSONDecoder: PostgresJSONDecoder>(
13+
func encode<JSONEncoder: PostgresJSONEncoder>(
14+
into byteBuffer: inout ByteBuffer,
15+
context: PostgresEncodingContext<JSONEncoder>
16+
) {
17+
let seconds = self.timeIntervalSince(Self._psqlDateStart) * Double(Self._microsecondsPerSecond)
18+
byteBuffer.writeInteger(Int64(seconds))
19+
}
20+
21+
// MARK: Private Constants
22+
23+
private static let _microsecondsPerSecond: Int64 = 1_000_000
24+
private static let _secondsInDay: Int64 = 24 * 60 * 60
25+
26+
/// values are stored as seconds before or after midnight 2000-01-01
27+
private static let _psqlDateStart = Date(timeIntervalSince1970: 946_684_800)
28+
}
29+
30+
extension Date: PostgresDecodable {
31+
init<JSONDecoder: PostgresJSONDecoder>(
1432
from buffer: inout ByteBuffer,
1533
type: PostgresDataType,
1634
format: PostgresFormat,
1735
context: PostgresDecodingContext<JSONDecoder>
18-
) throws -> Self {
36+
) throws {
1937
switch type {
2038
case .timestamp, .timestamptz:
2139
guard buffer.readableBytes == 8, let microseconds = buffer.readInteger(as: Int64.self) else {
2240
throw PostgresCastingError.Code.failure
2341
}
24-
let seconds = Double(microseconds) / Double(_microsecondsPerSecond)
25-
return Date(timeInterval: seconds, since: _psqlDateStart)
42+
let seconds = Double(microseconds) / Double(Self._microsecondsPerSecond)
43+
self = Date(timeInterval: seconds, since: Self._psqlDateStart)
2644
case .date:
2745
guard buffer.readableBytes == 4, let days = buffer.readInteger(as: Int32.self) else {
2846
throw PostgresCastingError.Code.failure
2947
}
30-
let seconds = Int64(days) * _secondsInDay
31-
return Date(timeInterval: Double(seconds), since: _psqlDateStart)
48+
let seconds = Int64(days) * Self._secondsInDay
49+
self = Date(timeInterval: Double(seconds), since: Self._psqlDateStart)
3250
default:
3351
throw PostgresCastingError.Code.typeMismatch
3452
}
3553
}
36-
37-
func encode<JSONEncoder: PostgresJSONEncoder>(
38-
into byteBuffer: inout ByteBuffer,
39-
context: PostgresEncodingContext<JSONEncoder>
40-
) {
41-
let seconds = self.timeIntervalSince(Self._psqlDateStart) * Double(Self._microsecondsPerSecond)
42-
byteBuffer.writeInteger(Int64(seconds))
43-
}
44-
45-
// MARK: Private Constants
46-
47-
private static let _microsecondsPerSecond: Int64 = 1_000_000
48-
private static let _secondsInDay: Int64 = 24 * 60 * 60
49-
50-
/// values are stored as seconds before or after midnight 2000-01-01
51-
private static let _psqlDateStart = Date(timeIntervalSince1970: 946_684_800)
5254
}
5355

56+
extension Date: PostgresCodable {}
Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import NIOCore
22
import struct Foundation.Decimal
33

4-
extension Decimal: PostgresCodable {
4+
extension Decimal: PostgresEncodable {
55
var psqlType: PostgresDataType {
66
.numeric
77
}
@@ -10,38 +10,42 @@ extension Decimal: PostgresCodable {
1010
.binary
1111
}
1212

13-
static func decode<JSONDecoder: PostgresJSONDecoder>(
13+
func encode<JSONEncoder: PostgresJSONEncoder>(
14+
into byteBuffer: inout ByteBuffer,
15+
context: PostgresEncodingContext<JSONEncoder>
16+
) {
17+
let numeric = PostgresNumeric(decimal: self)
18+
byteBuffer.writeInteger(numeric.ndigits)
19+
byteBuffer.writeInteger(numeric.weight)
20+
byteBuffer.writeInteger(numeric.sign)
21+
byteBuffer.writeInteger(numeric.dscale)
22+
var value = numeric.value
23+
byteBuffer.writeBuffer(&value)
24+
}
25+
}
26+
27+
extension Decimal: PostgresDecodable {
28+
init<JSONDecoder: PostgresJSONDecoder>(
1429
from buffer: inout ByteBuffer,
1530
type: PostgresDataType,
1631
format: PostgresFormat,
1732
context: PostgresDecodingContext<JSONDecoder>
18-
) throws -> Self {
33+
) throws {
1934
switch (format, type) {
2035
case (.binary, .numeric):
2136
guard let numeric = PostgresNumeric(buffer: &buffer) else {
2237
throw PostgresCastingError.Code.failure
2338
}
24-
return numeric.decimal
39+
self = numeric.decimal
2540
case (.text, .numeric):
2641
guard let string = buffer.readString(length: buffer.readableBytes), let value = Decimal(string: string) else {
2742
throw PostgresCastingError.Code.failure
2843
}
29-
return value
44+
self = value
3045
default:
3146
throw PostgresCastingError.Code.typeMismatch
3247
}
3348
}
34-
35-
func encode<JSONEncoder: PostgresJSONEncoder>(
36-
into byteBuffer: inout ByteBuffer,
37-
context: PostgresEncodingContext<JSONEncoder>
38-
) {
39-
let numeric = PostgresNumeric(decimal: self)
40-
byteBuffer.writeInteger(numeric.ndigits)
41-
byteBuffer.writeInteger(numeric.weight)
42-
byteBuffer.writeInteger(numeric.sign)
43-
byteBuffer.writeInteger(numeric.dscale)
44-
var value = numeric.value
45-
byteBuffer.writeBuffer(&value)
46-
}
4749
}
50+
51+
extension Decimal: PostgresCodable {}

0 commit comments

Comments
 (0)