Skip to content

Commit 3d6666d

Browse files
authored
Make Postgres Encodable public (vapor#248)
1 parent 136cf4c commit 3d6666d

25 files changed

+224
-196
lines changed

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

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,103 @@
11
import NIOCore
22
import struct Foundation.UUID
33

4+
// MARK: Protocols
5+
46
/// A type, of which arrays can be encoded into and decoded from a postgres binary format
5-
protocol PSQLArrayElement: PostgresCodable {
7+
public protocol PostgresArrayEncodable: PostgresEncodable {
68
static var psqlArrayType: PostgresDataType { get }
7-
static var psqlArrayElementType: PostgresDataType { get }
89
}
910

10-
extension Bool: PSQLArrayElement {
11-
static var psqlArrayType: PostgresDataType { .boolArray }
12-
static var psqlArrayElementType: PostgresDataType { .bool }
11+
/// A type that can be decoded into a Swift Array of its own type from a Postgres array.
12+
public protocol PostgresArrayDecodable: PostgresDecodable {}
13+
14+
// MARK: Element conformances
15+
16+
extension Bool: PostgresArrayDecodable {}
17+
18+
extension Bool: PostgresArrayEncodable {
19+
public static var psqlArrayType: PostgresDataType { .boolArray }
1320
}
1421

15-
extension ByteBuffer: PSQLArrayElement {
16-
static var psqlArrayType: PostgresDataType { .byteaArray }
17-
static var psqlArrayElementType: PostgresDataType { .bytea }
22+
extension ByteBuffer: PostgresArrayDecodable {}
23+
24+
extension ByteBuffer: PostgresArrayEncodable {
25+
public static var psqlArrayType: PostgresDataType { .byteaArray }
1826
}
1927

20-
extension UInt8: PSQLArrayElement {
21-
static var psqlArrayType: PostgresDataType { .charArray }
22-
static var psqlArrayElementType: PostgresDataType { .char }
28+
extension UInt8: PostgresArrayDecodable {}
29+
30+
extension UInt8: PostgresArrayEncodable {
31+
public static var psqlArrayType: PostgresDataType { .charArray }
2332
}
2433

25-
extension Int16: PSQLArrayElement {
26-
static var psqlArrayType: PostgresDataType { .int2Array }
27-
static var psqlArrayElementType: PostgresDataType { .int2 }
34+
35+
extension Int16: PostgresArrayDecodable {}
36+
37+
extension Int16: PostgresArrayEncodable {
38+
public static var psqlArrayType: PostgresDataType { .int2Array }
2839
}
2940

30-
extension Int32: PSQLArrayElement {
31-
static var psqlArrayType: PostgresDataType { .int4Array }
32-
static var psqlArrayElementType: PostgresDataType { .int4 }
41+
extension Int32: PostgresArrayDecodable {}
42+
43+
extension Int32: PostgresArrayEncodable {
44+
public static var psqlArrayType: PostgresDataType { .int4Array }
3345
}
3446

35-
extension Int64: PSQLArrayElement {
36-
static var psqlArrayType: PostgresDataType { .int8Array }
37-
static var psqlArrayElementType: PostgresDataType { .int8 }
47+
extension Int64: PostgresArrayDecodable {}
48+
49+
extension Int64: PostgresArrayEncodable {
50+
public static var psqlArrayType: PostgresDataType { .int8Array }
3851
}
3952

40-
extension Int: PSQLArrayElement {
41-
#if (arch(i386) || arch(arm))
42-
static var psqlArrayType: PostgresDataType { .int4Array }
43-
static var psqlArrayElementType: PostgresDataType { .int4 }
44-
#else
45-
static var psqlArrayType: PostgresDataType { .int8Array }
46-
static var psqlArrayElementType: PostgresDataType { .int8 }
47-
#endif
53+
extension Int: PostgresArrayDecodable {}
54+
55+
extension Int: PostgresArrayEncodable {
56+
public static var psqlArrayType: PostgresDataType {
57+
if MemoryLayout<Int>.size == 8 {
58+
return .int8Array
59+
}
60+
return .int4Array
61+
}
4862
}
4963

50-
extension Float: PSQLArrayElement {
51-
static var psqlArrayType: PostgresDataType { .float4Array }
52-
static var psqlArrayElementType: PostgresDataType { .float4 }
64+
extension Float: PostgresArrayDecodable {}
65+
66+
extension Float: PostgresArrayEncodable {
67+
public static var psqlArrayType: PostgresDataType { .float4Array }
5368
}
5469

55-
extension Double: PSQLArrayElement {
56-
static var psqlArrayType: PostgresDataType { .float8Array }
57-
static var psqlArrayElementType: PostgresDataType { .float8 }
70+
extension Double: PostgresArrayDecodable {}
71+
72+
extension Double: PostgresArrayEncodable {
73+
public static var psqlArrayType: PostgresDataType { .float8Array }
5874
}
5975

60-
extension String: PSQLArrayElement {
61-
static var psqlArrayType: PostgresDataType { .textArray }
62-
static var psqlArrayElementType: PostgresDataType { .text }
76+
extension String: PostgresArrayDecodable {}
77+
78+
extension String: PostgresArrayEncodable {
79+
public static var psqlArrayType: PostgresDataType { .textArray }
6380
}
6481

65-
extension UUID: PSQLArrayElement {
66-
static var psqlArrayType: PostgresDataType { .uuidArray }
67-
static var psqlArrayElementType: PostgresDataType { .uuid }
82+
extension UUID: PostgresArrayDecodable {}
83+
84+
extension UUID: PostgresArrayEncodable {
85+
public static var psqlArrayType: PostgresDataType { .uuidArray }
6886
}
6987

70-
extension Array: PostgresEncodable where Element: PSQLArrayElement {
71-
var psqlType: PostgresDataType {
88+
// MARK: Array conformances
89+
90+
extension Array: PostgresEncodable where Element: PostgresArrayEncodable {
91+
public static var psqlType: PostgresDataType {
7292
Element.psqlArrayType
7393
}
74-
75-
var psqlFormat: PostgresFormat {
94+
95+
public static var psqlFormat: PostgresFormat {
7696
.binary
7797
}
78-
79-
func encode<JSONEncoder: PostgresJSONEncoder>(
98+
99+
@inlinable
100+
public func encode<JSONEncoder: PostgresJSONEncoder>(
80101
into buffer: inout ByteBuffer,
81102
context: PostgresEncodingContext<JSONEncoder>
82103
) throws {
@@ -85,13 +106,13 @@ extension Array: PostgresEncodable where Element: PSQLArrayElement {
85106
// b
86107
buffer.writeInteger(0, as: Int32.self)
87108
// array element type
88-
buffer.writeInteger(Element.psqlArrayElementType.rawValue)
109+
buffer.writeInteger(Element.psqlType.rawValue)
89110

90111
// continue if the array is not empty
91112
guard !self.isEmpty else {
92113
return
93114
}
94-
115+
95116
// length of array
96117
buffer.writeInteger(numericCast(self.count), as: Int32.self)
97118
// dimensions
@@ -103,20 +124,6 @@ extension Array: PostgresEncodable where Element: PSQLArrayElement {
103124
}
104125
}
105126

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 {}
120127

121128
extension Array: PostgresDecodable where Element: PostgresArrayDecodable, Element == Element._DecodableType {
122129
public init<JSONDecoder: PostgresJSONDecoder>(

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ extension Bool: PostgresDecodable {
4444
}
4545

4646
extension Bool: PostgresEncodable {
47-
var psqlType: PostgresDataType {
47+
public static var psqlType: PostgresDataType {
4848
.bool
4949
}
5050

51-
var psqlFormat: PostgresFormat {
51+
public static var psqlFormat: PostgresFormat {
5252
.binary
5353
}
54-
55-
func encode<JSONEncoder: PostgresJSONEncoder>(
54+
55+
@inlinable
56+
public func encode<JSONEncoder: PostgresJSONEncoder>(
5657
into byteBuffer: inout ByteBuffer,
5758
context: PostgresEncodingContext<JSONEncoder>
5859
) {

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import NIOCore
33
import NIOFoundationCompat
44

55
extension PostgresEncodable where Self: Sequence, Self.Element == UInt8 {
6-
var psqlType: PostgresDataType {
6+
public static var psqlType: PostgresDataType {
77
.bytea
88
}
99

10-
var psqlFormat: PostgresFormat {
10+
public static var psqlFormat: PostgresFormat {
1111
.binary
1212
}
13-
14-
func encode<JSONEncoder: PostgresJSONEncoder>(
13+
14+
@inlinable
15+
public func encode<JSONEncoder: PostgresJSONEncoder>(
1516
into byteBuffer: inout ByteBuffer,
1617
context: PostgresEncodingContext<JSONEncoder>
1718
) {
@@ -20,15 +21,16 @@ extension PostgresEncodable where Self: Sequence, Self.Element == UInt8 {
2021
}
2122

2223
extension ByteBuffer: PostgresEncodable {
23-
var psqlType: PostgresDataType {
24+
public static var psqlType: PostgresDataType {
2425
.bytea
2526
}
2627

27-
var psqlFormat: PostgresFormat {
28+
public static var psqlFormat: PostgresFormat {
2829
.binary
2930
}
30-
31-
func encode<JSONEncoder: PostgresJSONEncoder>(
31+
32+
@inlinable
33+
public func encode<JSONEncoder: PostgresJSONEncoder>(
3234
into byteBuffer: inout ByteBuffer,
3335
context: PostgresEncodingContext<JSONEncoder>
3436
) {
@@ -52,15 +54,16 @@ extension ByteBuffer: PostgresDecodable {
5254
extension ByteBuffer: PostgresCodable {}
5355

5456
extension Data: PostgresEncodable {
55-
var psqlType: PostgresDataType {
57+
public static var psqlType: PostgresDataType {
5658
.bytea
5759
}
5860

59-
var psqlFormat: PostgresFormat {
61+
public static var psqlFormat: PostgresFormat {
6062
.binary
6163
}
6264

63-
func encode<JSONEncoder: PostgresJSONEncoder>(
65+
@inlinable
66+
public func encode<JSONEncoder: PostgresJSONEncoder>(
6467
into byteBuffer: inout ByteBuffer,
6568
context: PostgresEncodingContext<JSONEncoder>
6669
) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import NIOCore
22
import struct Foundation.Date
33

44
extension Date: PostgresEncodable {
5-
var psqlType: PostgresDataType {
5+
public static var psqlType: PostgresDataType {
66
.timestamptz
77
}
88

9-
var psqlFormat: PostgresFormat {
9+
public static var psqlFormat: PostgresFormat {
1010
.binary
1111
}
12-
13-
func encode<JSONEncoder: PostgresJSONEncoder>(
12+
13+
@inlinable
14+
public func encode<JSONEncoder: PostgresJSONEncoder>(
1415
into byteBuffer: inout ByteBuffer,
1516
context: PostgresEncodingContext<JSONEncoder>
1617
) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import NIOCore
22
import struct Foundation.Decimal
33

44
extension Decimal: PostgresEncodable {
5-
var psqlType: PostgresDataType {
5+
public static var psqlType: PostgresDataType {
66
.numeric
77
}
88

9-
var psqlFormat: PostgresFormat {
9+
public static var psqlFormat: PostgresFormat {
1010
.binary
1111
}
12-
13-
func encode<JSONEncoder: PostgresJSONEncoder>(
12+
13+
public func encode<JSONEncoder: PostgresJSONEncoder>(
1414
into byteBuffer: inout ByteBuffer,
1515
context: PostgresEncodingContext<JSONEncoder>
1616
) {

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import NIOCore
22

33
extension Float: PostgresEncodable {
4-
var psqlType: PostgresDataType {
4+
public static var psqlType: PostgresDataType {
55
.float4
66
}
77

8-
var psqlFormat: PostgresFormat {
8+
public static var psqlFormat: PostgresFormat {
99
.binary
1010
}
11-
12-
func encode<JSONEncoder: PostgresJSONEncoder>(
11+
12+
@inlinable
13+
public func encode<JSONEncoder: PostgresJSONEncoder>(
1314
into byteBuffer: inout ByteBuffer,
1415
context: PostgresEncodingContext<JSONEncoder>
1516
) {
@@ -50,15 +51,16 @@ extension Float: PostgresDecodable {
5051
extension Float: PostgresCodable {}
5152

5253
extension Double: PostgresEncodable {
53-
var psqlType: PostgresDataType {
54+
public static var psqlType: PostgresDataType {
5455
.float8
5556
}
5657

57-
var psqlFormat: PostgresFormat {
58+
public static var psqlFormat: PostgresFormat {
5859
.binary
5960
}
60-
61-
func encode<JSONEncoder: PostgresJSONEncoder>(
61+
62+
@inlinable
63+
public func encode<JSONEncoder: PostgresJSONEncoder>(
6264
into byteBuffer: inout ByteBuffer,
6365
context: PostgresEncodingContext<JSONEncoder>
6466
) {

0 commit comments

Comments
 (0)