From 7b2a31505e91607243aa96c472612130579368ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fre=CC=81de=CC=81ric=20Ruaudel?= Date: Tue, 26 Jun 2018 10:35:50 +0200 Subject: [PATCH 1/3] add a test to check encoding decoding of an empty array --- Tests/SQLiteTests/SQLiteTests.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Tests/SQLiteTests/SQLiteTests.swift b/Tests/SQLiteTests/SQLiteTests.swift index 0f2a523..d268dc0 100644 --- a/Tests/SQLiteTests/SQLiteTests.swift +++ b/Tests/SQLiteTests/SQLiteTests.swift @@ -157,6 +157,18 @@ class SQLiteTests: XCTestCase { } group.wait() } + + func testEmptyArrayEncodingDecoding() throws { + let emptyArray = [String]() + let encoder = SQLiteDataEncoder() + + let data = try encoder.encode(emptyArray) + + let decoder = SQLiteDataDecoder() + + let result = try decoder.decode([String].self, from: data) + XCTAssertEqual(result, emptyArray, "Should convert back to empty Array") + } static let allTests = [ ("testBenchmark", testBenchmark), From db41d502eb4b9087104e01146b2aed5e3f430a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fre=CC=81de=CC=81ric=20Ruaudel?= Date: Tue, 26 Jun 2018 10:39:44 +0200 Subject: [PATCH 2/3] add a test to verify that non empty array encode and decode back correctly --- Tests/SQLiteTests/SQLiteTests.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Tests/SQLiteTests/SQLiteTests.swift b/Tests/SQLiteTests/SQLiteTests.swift index d268dc0..d119ff6 100644 --- a/Tests/SQLiteTests/SQLiteTests.swift +++ b/Tests/SQLiteTests/SQLiteTests.swift @@ -158,6 +158,18 @@ class SQLiteTests: XCTestCase { group.wait() } + func testNonEmptyArrayEncodingDecoding() throws { + let nonEmptyArray = ["foo", "bar"] + let encoder = SQLiteDataEncoder() + + let data = try encoder.encode(nonEmptyArray) + + let decoder = SQLiteDataDecoder() + + let result = try decoder.decode([String].self, from: data) + XCTAssertEqual(result, nonEmptyArray, "Should convert back to original array") + } + func testEmptyArrayEncodingDecoding() throws { let emptyArray = [String]() let encoder = SQLiteDataEncoder() From 04afb7c7dc84a3925ad0d0922c2472cdbefc7e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fre=CC=81de=CC=81ric=20Ruaudel?= Date: Tue, 26 Jun 2018 10:40:20 +0200 Subject: [PATCH 3/3] fix empty array encoding crash in case of an empty container, the encoder returns nil so we need to check for that and throw a _DoJSONError to fall back to JSON encoding like with non empty arrays --- Sources/SQLite/Codable/SQLiteDataEncoder.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/SQLite/Codable/SQLiteDataEncoder.swift b/Sources/SQLite/Codable/SQLiteDataEncoder.swift index b1769fe..0a90e57 100644 --- a/Sources/SQLite/Codable/SQLiteDataEncoder.swift +++ b/Sources/SQLite/Codable/SQLiteDataEncoder.swift @@ -24,7 +24,8 @@ public struct SQLiteDataEncoder { let encoder = _Encoder() do { try value.encode(to: encoder) - return encoder.data! + guard let data = encoder.data else { throw _DoJSONError() } + return data } catch is _DoJSONError { struct AnyEncodable: Encodable { var encodable: Encodable