From c28d3ad15bcbcd146f7e1282de41a350f29112af Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Thu, 20 Feb 2020 00:52:29 -0500 Subject: [PATCH 1/3] support encoding arrays other than json --- Sources/PostgresKit/PostgresDataDecoder.swift | 9 +++------ Sources/PostgresKit/PostgresDataEncoder.swift | 4 +++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Sources/PostgresKit/PostgresDataDecoder.swift b/Sources/PostgresKit/PostgresDataDecoder.swift index 714c1e4d..e32aa6d5 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,13 @@ 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 + return try self.json.decode(T.self, from: jsonb) } else if let json = data.json { - jsonData = json + return try self.json.decode(T.self, from: json) } else { - throw Error.unexpectedDataType(data.type, expected: "json") + return try PostgresDataDecoder().decode(T.self, from: data) } - return try self.json.decode(T.self, from: jsonData) } mutating func nestedContainer( diff --git a/Sources/PostgresKit/PostgresDataEncoder.swift b/Sources/PostgresKit/PostgresDataEncoder.swift index 9f4518d4..1c6ff472 100644 --- a/Sources/PostgresKit/PostgresDataEncoder.swift +++ b/Sources/PostgresKit/PostgresDataEncoder.swift @@ -16,7 +16,9 @@ public final class PostgresDataEncoder { 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))) } From c7596a0f4db90927c18ed91729d149c93e7c29ff Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Thu, 20 Feb 2020 00:56:53 -0500 Subject: [PATCH 2/3] simplify decoder method --- Sources/PostgresKit/PostgresDataDecoder.swift | 8 +------- Sources/PostgresKit/PostgresDataEncoder.swift | 4 ++-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Sources/PostgresKit/PostgresDataDecoder.swift b/Sources/PostgresKit/PostgresDataDecoder.swift index e32aa6d5..b467a505 100644 --- a/Sources/PostgresKit/PostgresDataDecoder.swift +++ b/Sources/PostgresKit/PostgresDataDecoder.swift @@ -107,13 +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] - if let jsonb = data.jsonb { - return try self.json.decode(T.self, from: jsonb) - } else if let json = data.json { - return try self.json.decode(T.self, from: json) - } else { - return try PostgresDataDecoder().decode(T.self, from: data) - } + 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 1c6ff472..0f5f2f0a 100644 --- a/Sources/PostgresKit/PostgresDataEncoder.swift +++ b/Sources/PostgresKit/PostgresDataEncoder.swift @@ -8,8 +8,8 @@ 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)) From 04fc6d21a793255f3f56e01b8946e7375fad1947 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Thu, 20 Feb 2020 00:57:22 -0500 Subject: [PATCH 3/3] test fluent on 5.2 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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