Skip to content

Commit ba0d2bb

Browse files
authored
Make new Postgres decoding public (#244)
1 parent 2998b1c commit ba0d2bb

18 files changed

+190
-79
lines changed

Sources/PostgresNIO/Data/PostgresRow.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import class Foundation.JSONDecoder
1212
/// random access to cells in O(1) create a new ``PostgresRandomAccessRow`` with the given row and
1313
/// access it instead.
1414
public struct PostgresRow {
15+
@usableFromInline
1516
let lookupTable: [String: Int]
17+
@usableFromInline
1618
let data: DataRow
17-
19+
@usableFromInline
1820
let columns: [RowDescription.Column]
1921

2022
init(data: DataRow, lookupTable: [String: Int], columns: [RowDescription.Column]) {

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,23 @@ extension Array: PostgresEncodable where Element: PSQLArrayElement {
103103
}
104104
}
105105

106-
extension Array: PostgresDecodable where Element: PSQLArrayElement {
107-
init<JSONDecoder: PostgresJSONDecoder>(
106+
/// A type that can be decoded into a Swift Array of its own type from a Postgres array.
107+
public protocol PostgresArrayDecodable: PostgresDecodable {}
108+
109+
extension Bool: PostgresArrayDecodable {}
110+
extension ByteBuffer: PostgresArrayDecodable {}
111+
extension UInt8: PostgresArrayDecodable {}
112+
extension Int16: PostgresArrayDecodable {}
113+
extension Int32: PostgresArrayDecodable {}
114+
extension Int64: PostgresArrayDecodable {}
115+
extension Int: PostgresArrayDecodable {}
116+
extension Float: PostgresArrayDecodable {}
117+
extension Double: PostgresArrayDecodable {}
118+
extension String: PostgresArrayDecodable {}
119+
extension UUID: PostgresArrayDecodable {}
120+
121+
extension Array: PostgresDecodable where Element: PostgresArrayDecodable, Element == Element._DecodableType {
122+
public init<JSONDecoder: PostgresJSONDecoder>(
108123
from buffer: inout ByteBuffer,
109124
type: PostgresDataType,
110125
format: PostgresFormat,
@@ -114,48 +129,44 @@ extension Array: PostgresDecodable where Element: PSQLArrayElement {
114129
// currently we only support decoding arrays in binary format.
115130
throw PostgresCastingError.Code.failure
116131
}
117-
132+
118133
guard let (isNotEmpty, b, element) = buffer.readMultipleIntegers(endianness: .big, as: (Int32, Int32, UInt32).self),
119134
0 <= isNotEmpty, isNotEmpty <= 1, b == 0
120135
else {
121136
throw PostgresCastingError.Code.failure
122137
}
123-
138+
124139
let elementType = PostgresDataType(element)
125-
140+
126141
guard isNotEmpty == 1 else {
127142
self = []
128143
return
129144
}
130-
145+
131146
guard let (expectedArrayCount, dimensions) = buffer.readMultipleIntegers(endianness: .big, as: (Int32, Int32).self),
132147
expectedArrayCount > 0,
133148
dimensions == 1
134149
else {
135150
throw PostgresCastingError.Code.failure
136151
}
137-
152+
138153
var result = Array<Element>()
139154
result.reserveCapacity(Int(expectedArrayCount))
140-
155+
141156
for _ in 0 ..< expectedArrayCount {
142-
guard let elementLength = buffer.readInteger(as: Int32.self) else {
157+
guard let elementLength = buffer.readInteger(as: Int32.self), elementLength >= 0 else {
143158
throw PostgresCastingError.Code.failure
144159
}
145-
160+
146161
guard var elementBuffer = buffer.readSlice(length: numericCast(elementLength)) else {
147162
throw PostgresCastingError.Code.failure
148163
}
149-
164+
150165
let element = try Element.init(from: &elementBuffer, type: elementType, format: format, context: context)
151-
166+
152167
result.append(element)
153168
}
154-
169+
155170
self = result
156171
}
157172
}
158-
159-
extension Array: PostgresCodable where Element: PSQLArrayElement {
160-
161-
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import NIOCore
22

33
extension Bool: PostgresDecodable {
4-
init<JSONDecoder: PostgresJSONDecoder>(
4+
@inlinable
5+
public init<JSONDecoder: PostgresJSONDecoder>(
56
from buffer: inout ByteBuffer,
67
type: PostgresDataType,
78
format: PostgresFormat,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ extension ByteBuffer: PostgresEncodable {
3838
}
3939

4040
extension ByteBuffer: PostgresDecodable {
41-
init<JSONDecoder: PostgresJSONDecoder>(
41+
@inlinable
42+
public init<JSONDecoder: PostgresJSONDecoder>(
4243
from buffer: inout ByteBuffer,
4344
type: PostgresDataType,
4445
format: PostgresFormat,
@@ -68,7 +69,8 @@ extension Data: PostgresEncodable {
6869
}
6970

7071
extension Data: PostgresDecodable {
71-
init<JSONDecoder: PostgresJSONDecoder>(
72+
@inlinable
73+
public init<JSONDecoder: PostgresJSONDecoder>(
7274
from buffer: inout ByteBuffer,
7375
type: PostgresDataType,
7476
format: PostgresFormat,

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ extension Date: PostgresEncodable {
1919
}
2020

2121
// MARK: Private Constants
22-
23-
private static let _microsecondsPerSecond: Int64 = 1_000_000
24-
private static let _secondsInDay: Int64 = 24 * 60 * 60
22+
23+
@usableFromInline
24+
static let _microsecondsPerSecond: Int64 = 1_000_000
25+
@usableFromInline
26+
static let _secondsInDay: Int64 = 24 * 60 * 60
2527

2628
/// values are stored as seconds before or after midnight 2000-01-01
27-
private static let _psqlDateStart = Date(timeIntervalSince1970: 946_684_800)
29+
@usableFromInline
30+
static let _psqlDateStart = Date(timeIntervalSince1970: 946_684_800)
2831
}
2932

3033
extension Date: PostgresDecodable {
31-
init<JSONDecoder: PostgresJSONDecoder>(
34+
@inlinable
35+
public init<JSONDecoder: PostgresJSONDecoder>(
3236
from buffer: inout ByteBuffer,
3337
type: PostgresDataType,
3438
format: PostgresFormat,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension Decimal: PostgresEncodable {
2525
}
2626

2727
extension Decimal: PostgresDecodable {
28-
init<JSONDecoder: PostgresJSONDecoder>(
28+
public init<JSONDecoder: PostgresJSONDecoder>(
2929
from buffer: inout ByteBuffer,
3030
type: PostgresDataType,
3131
format: PostgresFormat,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ extension Float: PostgresEncodable {
1818
}
1919

2020
extension Float: PostgresDecodable {
21-
init<JSONDecoder: PostgresJSONDecoder>(
21+
@inlinable
22+
public init<JSONDecoder: PostgresJSONDecoder>(
2223
from buffer: inout ByteBuffer,
2324
type: PostgresDataType,
2425
format: PostgresFormat,
@@ -66,7 +67,8 @@ extension Double: PostgresEncodable {
6667
}
6768

6869
extension Double: PostgresDecodable {
69-
init<JSONDecoder: PostgresJSONDecoder>(
70+
@inlinable
71+
public init<JSONDecoder: PostgresJSONDecoder>(
7072
from buffer: inout ByteBuffer,
7173
type: PostgresDataType,
7274
format: PostgresFormat,

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ extension UInt8: PostgresEncodable {
2020
}
2121

2222
extension UInt8: PostgresDecodable {
23-
init<JSONDecoder: PostgresJSONDecoder>(
23+
@inlinable
24+
public init<JSONDecoder: PostgresJSONDecoder>(
2425
from buffer: inout ByteBuffer,
2526
type: PostgresDataType,
2627
format: PostgresFormat,
@@ -62,7 +63,8 @@ extension Int16: PostgresEncodable {
6263
}
6364

6465
extension Int16: PostgresDecodable {
65-
init<JSONDecoder: PostgresJSONDecoder>(
66+
@inlinable
67+
public init<JSONDecoder: PostgresJSONDecoder>(
6668
from buffer: inout ByteBuffer,
6769
type: PostgresDataType,
6870
format: PostgresFormat,
@@ -107,7 +109,8 @@ extension Int32: PostgresEncodable {
107109
}
108110

109111
extension Int32: PostgresDecodable {
110-
init<JSONDecoder: PostgresJSONDecoder>(
112+
@inlinable
113+
public init<JSONDecoder: PostgresJSONDecoder>(
111114
from buffer: inout ByteBuffer,
112115
type: PostgresDataType,
113116
format: PostgresFormat,
@@ -157,7 +160,8 @@ extension Int64: PostgresEncodable {
157160
}
158161

159162
extension Int64: PostgresDecodable {
160-
init<JSONDecoder: PostgresJSONDecoder>(
163+
@inlinable
164+
public init<JSONDecoder: PostgresJSONDecoder>(
161165
from buffer: inout ByteBuffer,
162166
type: PostgresDataType,
163167
format: PostgresFormat,
@@ -219,7 +223,8 @@ extension Int: PostgresEncodable {
219223
}
220224

221225
extension Int: PostgresDecodable {
222-
init<JSONDecoder: PostgresJSONDecoder>(
226+
@inlinable
227+
public init<JSONDecoder: PostgresJSONDecoder>(
223228
from buffer: inout ByteBuffer,
224229
type: PostgresDataType,
225230
format: PostgresFormat,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ extension String: PostgresEncodable {
1919
}
2020

2121
extension String: PostgresDecodable {
22-
init<JSONDecoder: PostgresJSONDecoder>(
22+
23+
@inlinable
24+
public init<JSONDecoder: PostgresJSONDecoder>(
2325
from buffer: inout ByteBuffer,
2426
type: PostgresDataType,
2527
format: PostgresFormat,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ extension UUID: PostgresEncodable {
2626
}
2727

2828
extension UUID: PostgresDecodable {
29-
init<JSONDecoder: PostgresJSONDecoder>(
29+
@inlinable
30+
public init<JSONDecoder: PostgresJSONDecoder>(
3031
from buffer: inout ByteBuffer,
3132
type: PostgresDataType,
3233
format: PostgresFormat,
@@ -60,6 +61,7 @@ extension UUID: PostgresDecodable {
6061
extension UUID: PostgresCodable {}
6162

6263
extension ByteBuffer {
64+
@usableFromInline
6365
mutating func readUUID() -> UUID? {
6466
guard self.readableBytes >= MemoryLayout<uuid_t>.size else {
6567
return nil

0 commit comments

Comments
 (0)