diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f0349a75..eadebf04 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,7 +36,7 @@ jobs: - run: swift test --enable-test-discovery --sanitize=thread fluent: container: - image: vapor/swift:5.1 + image: vapor/swift:5.2 services: psql: image: postgres diff --git a/Sources/PostgresKit/PostgresDataDecoder.swift b/Sources/PostgresKit/PostgresDataDecoder.swift index 714c1e4d..b467a505 100644 --- a/Sources/PostgresKit/PostgresDataDecoder.swift +++ b/Sources/PostgresKit/PostgresDataDecoder.swift @@ -56,7 +56,6 @@ public final class PostgresDataDecoder { } func unkeyedContainer() throws -> UnkeyedDecodingContainer { - print(self.data.type) guard let data = self.data.array else { throw Error.unexpectedDataType(self.data.type, expected: "array") } @@ -108,15 +107,7 @@ public final class PostgresDataDecoder { mutating func decode(_ type: T.Type) throws -> T where T : Decodable { defer { self.currentIndex += 1 } let data = self.data[self.currentIndex] - let jsonData: Data - if let jsonb = data.jsonb { - jsonData = jsonb - } else if let json = data.json { - jsonData = json - } else { - throw Error.unexpectedDataType(data.type, expected: "json") - } - return try self.json.decode(T.self, from: jsonData) + return try PostgresDataDecoder(json: self.json).decode(T.self, from: data) } mutating func nestedContainer( diff --git a/Sources/PostgresKit/PostgresDataEncoder.swift b/Sources/PostgresKit/PostgresDataEncoder.swift index 9f4518d4..0f5f2f0a 100644 --- a/Sources/PostgresKit/PostgresDataEncoder.swift +++ b/Sources/PostgresKit/PostgresDataEncoder.swift @@ -8,15 +8,17 @@ public final class PostgresDataEncoder { } public func encode(_ value: Encodable) throws -> PostgresData { - if let custom = value as? PostgresDataConvertible { - return custom.postgresData! + if let custom = value as? PostgresDataConvertible, let data = custom.postgresData { + return data } else { let context = _Context() try value.encode(to: _Encoder(context: context)) if let value = context.value { return value } else if let array = context.array { - return PostgresData(array: array, elementType: .jsonb) + let elementType = array.first?.type ?? .jsonb + assert(array.filter { $0.type != elementType }.isEmpty, "Array does not contain all: \(elementType)") + return PostgresData(array: array, elementType: elementType) } else { return try PostgresData(jsonb: self.json.encode(_Wrapper(value))) }