Skip to content

Commit e8cc0d1

Browse files
committed
update to new codable syntax
1 parent c732dbf commit e8cc0d1

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

Sources/FluentPostgreSQL/PostgreSQLDataDecoder.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
internal struct PostgreSQLDataDecoder: SingleValueDecodingContainer {
22
var codingPath: [CodingKey]
3-
var userInfo: [CodingUserInfoKey: Any]
43
let data: PostgreSQLData
54
init(data: PostgreSQLData) {
65
self.data = data
7-
self.userInfo = [:]
86
self.codingPath = []
97
}
108

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
internal struct PostgreSQLDataEncoder: SingleValueEncodingContainer {
2+
var codingPath: [CodingKey]
3+
var data: PostgreSQLData?
4+
init() {
5+
self.codingPath = []
6+
}
7+
mutating func encodeNil() throws { data = PostgreSQLData(type: .void) }
8+
mutating func encode(_ value: Bool) throws { data = try value.convertToPostgreSQLData() }
9+
mutating func encode(_ value: Int) throws { data = try value.convertToPostgreSQLData() }
10+
mutating func encode(_ value: Int16) throws { data = try value.convertToPostgreSQLData() }
11+
mutating func encode(_ value: Int32) throws { data = try value.convertToPostgreSQLData() }
12+
mutating func encode(_ value: Int64) throws { data = try value.convertToPostgreSQLData() }
13+
mutating func encode(_ value: UInt) throws { data = try value.convertToPostgreSQLData() }
14+
mutating func encode(_ value: UInt8) throws { data = try value.convertToPostgreSQLData() }
15+
mutating func encode(_ value: UInt16) throws { data = try value.convertToPostgreSQLData() }
16+
mutating func encode(_ value: UInt32) throws { data = try value.convertToPostgreSQLData() }
17+
mutating func encode(_ value: UInt64) throws { data = try value.convertToPostgreSQLData() }
18+
mutating func encode(_ value: Double) throws { data = try value.convertToPostgreSQLData() }
19+
mutating func encode(_ value: Float) throws { data = try value.convertToPostgreSQLData() }
20+
mutating func encode(_ value: String) throws { data = try value.convertToPostgreSQLData() }
21+
mutating func encode<T>(_ value: T) throws where T : Encodable {
22+
guard let convertible = value as? PostgreSQLDataCustomConvertible else {
23+
let type = Swift.type(of: value)
24+
throw PostgreSQLError(
25+
identifier: "convertible",
26+
reason: "Unsupported encodable type: \(type)",
27+
suggestedFixes: [
28+
"Conform \(type) to PostgreSQLDataCustomConvertible"
29+
]
30+
)
31+
}
32+
data = try convertible.convertToPostgreSQLData()
33+
}
34+
}

Sources/FluentPostgreSQL/PostgreSQLDatabase+QuerySupporting.swift

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,18 @@ extension PostgreSQLDatabase: QuerySupporting {
2626
}
2727
modelData = try dict.values.map { codableData -> PostgreSQLData in
2828
switch codableData {
29-
case .null: return PostgreSQLData(type: .void)
30-
case .encodable(let encodable):
31-
guard let convertible = encodable as? PostgreSQLDataCustomConvertible else {
32-
let type = Swift.type(of: encodable)
33-
throw PostgreSQLError(
34-
identifier: "convertible",
35-
reason: "Unsupported encodable type: \(type)",
36-
suggestedFixes: [
37-
"Conform \(type) to PostgreSQLDataCustomConvertible"
38-
]
39-
)
40-
}
41-
return try convertible.convertToPostgreSQLData()
42-
case .array, .dictionary:
43-
throw PostgreSQLError(identifier: "codable", reason: "Unsupported encodable type: \(codableData)")
29+
case .single(let closure):
30+
var encoder: SingleValueEncodingContainer = PostgreSQLDataEncoder()
31+
try closure(&encoder)
32+
return (encoder as! PostgreSQLDataEncoder).data!
33+
case .array, .dictionary, .null:
34+
throw PostgreSQLError(
35+
identifier: "nestedData",
36+
reason: "Unsupported nested data in query.",
37+
suggestedFixes: [
38+
"Use a nested struct instead"
39+
]
40+
)
4441
}
4542
}
4643
default:
@@ -82,7 +79,7 @@ extension PostgreSQLDatabase: QuerySupporting {
8279
// Run the query
8380
return try connection.query(sqlString, parameters) { row in
8481
let codableDict = row.mapValues { psqlData -> DecodableData in
85-
return .decoder(PostgreSQLDataDecoder(data: psqlData))
82+
return .single({ PostgreSQLDataDecoder(data: psqlData) })
8683
}
8784
do {
8885
let decoded = try CodableDataDecoder().decode(D.self, from: .dictionary(codableDict))

0 commit comments

Comments
 (0)