1
1
import Foundation
2
2
3
3
/// Representable by a `T[]` column on the PostgreSQL database.
4
- public protocol PostgreSQLArrayCustomConvertible : PostgreSQLDataCustomConvertible , Codable {
4
+ public protocol PostgreSQLArrayCustomConvertible : PostgreSQLDataCustomConvertible {
5
5
/// The associated array element type
6
- associatedtype PostgreSQLArrayElement : PostgreSQLDataCustomConvertible
6
+ associatedtype PostgreSQLArrayElement // : PostgreSQLDataCustomConvertible
7
7
8
8
/// Convert an array of elements to self.
9
9
static func convertFromPostgreSQLArray( _ data: [ PostgreSQLArrayElement ] ) -> Self
@@ -13,11 +13,6 @@ public protocol PostgreSQLArrayCustomConvertible: PostgreSQLDataCustomConvertibl
13
13
}
14
14
15
15
extension PostgreSQLArrayCustomConvertible {
16
- /// See `PostgreSQLDataCustomConvertible.postgreSQLDataType`
17
- public static var postgreSQLDataType : PostgreSQLDataType {
18
- return PostgreSQLArrayElement . postgreSQLDataArrayType
19
- }
20
-
21
16
/// See `PostgreSQLDataCustomConvertible.convertFromPostgreSQLData(_:)`
22
17
public static func convertFromPostgreSQLData( _ data: PostgreSQLData ) throws -> Self {
23
18
guard var value = data. data else {
@@ -35,8 +30,8 @@ extension PostgreSQLArrayCustomConvertible {
35
30
let count = Int ( value. extract ( Int32 . self) . bigEndian)
36
31
let subValue = value. extract ( count: count)
37
32
let psqlData = PostgreSQLData ( type: metadata. type, format: data. format, data: subValue)
38
- let element = try PostgreSQLArrayElement . convertFromPostgreSQLData ( psqlData)
39
- array. append ( element)
33
+ let element = try requirePostgreSQLDataCustomConvertible ( PostgreSQLArrayElement . self ) . convertFromPostgreSQLData ( psqlData)
34
+ array. append ( element as! PostgreSQLArrayElement )
40
35
}
41
36
} else {
42
37
array = [ ]
@@ -48,13 +43,13 @@ extension PostgreSQLArrayCustomConvertible {
48
43
/// See `PostgreSQLDataCustomConvertible.convertToPostgreSQLData()`
49
44
public func convertToPostgreSQLData( ) throws -> PostgreSQLData {
50
45
let elements = try convertToPostgreSQLArray ( ) . map {
51
- try $0 . convertToPostgreSQLData ( )
46
+ try requirePostgreSQLDataCustomConvertible ( $0 ) . convertToPostgreSQLData ( )
52
47
}
53
48
54
49
var data = Data ( )
55
50
data += Int32 ( 1 ) . data // non-null
56
51
data += Int32 ( 0 ) . data // b
57
- data += PostgreSQLArrayElement . postgreSQLDataType. raw. data // type
52
+ data += requirePostgreSQLDataCustomConvertible ( PostgreSQLArrayElement . self ) . postgreSQLDataType. raw. data // type
58
53
data += Int32 ( elements. count) . data // length
59
54
data += Int32 ( 1 ) . data // dimensions
60
55
@@ -67,7 +62,7 @@ extension PostgreSQLArrayCustomConvertible {
67
62
}
68
63
}
69
64
70
- return PostgreSQLData ( type: PostgreSQLArrayElement . postgreSQLDataArrayType, format: . binary, data: data)
65
+ return PostgreSQLData ( type: requirePostgreSQLDataCustomConvertible ( PostgreSQLArrayElement . self ) . postgreSQLDataArrayType, format: . binary, data: data)
71
66
}
72
67
}
73
68
@@ -107,10 +102,15 @@ extension PostgreSQLArrayMetadata: CustomStringConvertible {
107
102
}
108
103
}
109
104
110
- extension Array : PostgreSQLArrayCustomConvertible where Element : Codable , Element : PostgreSQLDataCustomConvertible {
105
+ extension Array : PostgreSQLArrayCustomConvertible {
111
106
/// See `PostgreSQLArrayCustomConvertible.postgreSQLDataArrayType`
112
107
public static var postgreSQLDataArrayType : PostgreSQLDataType {
113
- return Element . postgreSQLDataArrayType
108
+ fatalError ( " Multi-dimensional arrays are not yet supported. " )
109
+ }
110
+
111
+ /// See `PostgreSQLDataCustomConvertible.postgreSQLDataType`
112
+ public static var postgreSQLDataType : PostgreSQLDataType {
113
+ return requirePostgreSQLDataCustomConvertible ( Element . self) . postgreSQLDataArrayType
114
114
}
115
115
116
116
/// See `PostgreSQLArrayCustomConvertible.PostgreSQLArrayElement`
@@ -126,3 +126,17 @@ extension Array: PostgreSQLArrayCustomConvertible where Element: Codable, Elemen
126
126
return self
127
127
}
128
128
}
129
+
130
+ func requirePostgreSQLDataCustomConvertible< T> ( _ type: T . Type ) -> PostgreSQLDataCustomConvertible . Type {
131
+ guard let custom = T . self as? PostgreSQLDataCustomConvertible . Type else {
132
+ fatalError ( " ` \( T . self) ` does not conform to `PostgreSQLDataCustomConvertible` " )
133
+ }
134
+ return custom
135
+ }
136
+
137
+ func requirePostgreSQLDataCustomConvertible< T> ( _ type: T ) -> PostgreSQLDataCustomConvertible {
138
+ guard let custom = type as? PostgreSQLDataCustomConvertible else {
139
+ fatalError ( " ` \( T . self) ` does not conform to `PostgreSQLDataCustomConvertible` " )
140
+ }
141
+ return custom
142
+ }
0 commit comments