1- public protocol PostgreSQLValueRepresentable {
2- var postgreSQLValue : PostgreSQLQuery . Value { get }
3- }
4-
5-
61/// Converts `Encodable` objects to `PostgreSQLData`.
72///
83/// let data = try PostgreSQLDataEncoder().encode("hello")
94/// print(data) // PostgreSQLData
105///
11- public struct PostgreSQLValueEncoder {
6+ public struct PostgreSQLDataEncoder {
127 /// Creates a new `PostgreSQLDataEncoder`.
138 public init ( ) { }
149
@@ -20,12 +15,9 @@ public struct PostgreSQLValueEncoder {
2015 /// - parameters:
2116 /// - encodable: `Encodable` object to encode.
2217 /// - returns: Encoded `PostgreSQLData`.
23- public func encode( _ encodable: Encodable ) throws -> PostgreSQLQuery . Value {
24- if let psql = encodable as? PostgreSQLValueRepresentable {
25- return psql. postgreSQLValue
26- }
18+ public func encode( _ encodable: Encodable ) throws -> PostgreSQLData {
2719 if let convertible = encodable as? PostgreSQLDataConvertible {
28- return try . data ( convertible. convertToPostgreSQLData ( ) )
20+ return try convertible. convertToPostgreSQLData ( )
2921 }
3022
3123 do {
@@ -34,7 +26,23 @@ public struct PostgreSQLValueEncoder {
3426 if let data = encoder. data {
3527 return data
3628 } else {
37- let type = encoder. array. first? . type ?? . null
29+ let type : PostgreSQLDataFormat
30+ if let present = encoder. array. first? . type {
31+ type = present
32+ } else if
33+ let array = Swift . type ( of: encodable) as? AnyArray . Type ,
34+ let psql = array. anyElementType as? PostgreSQLDataTypeStaticRepresentable . Type
35+ {
36+ if let format = psql. postgreSQLDataType. dataFormat {
37+ type = format
38+ } else {
39+ WARNING ( " Could not determine PostgreSQL array data type: \( psql. postgreSQLDataType) " )
40+ type = . null
41+ }
42+ } else {
43+ WARNING ( " Could not determine PostgreSQL array data type: \( Swift . type ( of: encodable) ) " )
44+ type = . null
45+ }
3846 // encode array
3947 var data = Data ( )
4048 data += Data . of ( Int32 ( 1 ) . bigEndian) // non-null
@@ -51,7 +59,7 @@ public struct PostgreSQLValueEncoder {
5159 default : data += Data . of ( Int32 ( 0 ) . bigEndian)
5260 }
5361 }
54- return . data ( PostgreSQLData ( type. arrayType ?? . null, binary: data) )
62+ return PostgreSQLData ( type. arrayType ?? . null, binary: data)
5563 }
5664 } catch is _KeyedError {
5765 struct AnyEncodable : Encodable {
@@ -64,7 +72,7 @@ public struct PostgreSQLValueEncoder {
6472 try encodable. encode ( to: encoder)
6573 }
6674 }
67- return try . data ( PostgreSQLData ( . jsonb, binary: [ 0x01 ] + JSONEncoder( ) . encode ( AnyEncodable ( encodable) ) ) )
75+ return try PostgreSQLData ( . jsonb, binary: [ 0x01 ] + JSONEncoder( ) . encode ( AnyEncodable ( encodable) ) )
6876 }
6977 }
7078
@@ -74,7 +82,7 @@ public struct PostgreSQLValueEncoder {
7482 private final class _Encoder : Encoder {
7583 let codingPath : [ CodingKey ] = [ ]
7684 let userInfo : [ CodingUserInfoKey : Any ] = [ : ]
77- var data : PostgreSQLQuery . Value ?
85+ var data : PostgreSQLData ?
7886 var array : [ PostgreSQLData ]
7987
8088 init ( ) {
@@ -111,15 +119,10 @@ public struct PostgreSQLValueEncoder {
111119 }
112120
113121 mutating func encode< T> ( _ value: T ) throws where T : Encodable {
114- if let psql = value as? PostgreSQLValueRepresentable {
115- encoder. data = psql. postgreSQLValue
116- return
117- }
118122 if let convertible = value as? PostgreSQLDataConvertible {
119- encoder. data = try . data ( convertible. convertToPostgreSQLData ( ) )
123+ encoder. data = try convertible. convertToPostgreSQLData ( )
120124 return
121125 }
122-
123126 try value. encode ( to: encoder)
124127 }
125128 }
@@ -192,3 +195,13 @@ public struct PostgreSQLValueEncoder {
192195 }
193196 }
194197}
198+
199+ protocol AnyArray {
200+ static var anyElementType : Any . Type { get }
201+ }
202+
203+ extension Array : AnyArray {
204+ static var anyElementType : Any . Type {
205+ return Element . self
206+ }
207+ }
0 commit comments