From bffe4d32f5464a4469ef7636aedb00b09e292e3e Mon Sep 17 00:00:00 2001 From: Matt Tancock Date: Fri, 17 Apr 2020 16:16:34 +0100 Subject: [PATCH 1/3] Add Codable support to DatabaseDateComponents --- .../Foundation/DatabaseDateComponents.swift | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift index 0057f4a6f..965f67411 100644 --- a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift +++ b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift @@ -8,10 +8,10 @@ import SQLite3 #endif /// DatabaseDateComponents reads and stores DateComponents in the database. -public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnConvertible { +public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnConvertible, Codable { /// The available formats for reading and storing date components. - public enum Format: String { + public enum Format: String, Codable { /// The format "yyyy-MM-dd". case YMD = "yyyy-MM-dd" @@ -159,4 +159,31 @@ public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnC return SQLiteDateParser().components(from: string) } + + // MARK: - Codable adoption + + /// CodingKeys for codable conformance. + enum CodingKeys: String, CodingKey { + case dateComponents, format + } + + /// Creates a new instance by decoding from the given decoder. + /// + /// - parameters: + /// - decoder: The decoder to read data from. + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + dateComponents = try container.decode(DateComponents.self, forKey: .dateComponents) + format = try container.decode(Format.self, forKey: .format) + } + + /// Encodes this value into the given encoder. + /// + /// - parameters: + /// - encoder: The encoder to write data to. + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(dateComponents, forKey: .dateComponents) + try container.encode(format, forKey: .format) + } } From 18154f29bedc846dc6c1dae5ccc5494f2f3749d7 Mon Sep 17 00:00:00 2001 From: Matt Tancock Date: Mon, 20 Apr 2020 13:36:23 +0100 Subject: [PATCH 2/3] Simplify encoding format and add tests --- .../Foundation/DatabaseDateComponents.swift | 9 ++++---- .../FoundationDateComponentsTests.swift | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift index 965f67411..1963dbb1f 100644 --- a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift +++ b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift @@ -164,7 +164,7 @@ public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnC /// CodingKeys for codable conformance. enum CodingKeys: String, CodingKey { - case dateComponents, format + case date } /// Creates a new instance by decoding from the given decoder. @@ -173,8 +173,8 @@ public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnC /// - decoder: The decoder to read data from. public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - dateComponents = try container.decode(DateComponents.self, forKey: .dateComponents) - format = try container.decode(Format.self, forKey: .format) + let stringValue = try container.decode(String.self, forKey: .date) + self = DatabaseDateComponents.fromDatabaseValue(stringValue.databaseValue)! } /// Encodes this value into the given encoder. @@ -183,7 +183,6 @@ public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnC /// - encoder: The encoder to write data to. public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(dateComponents, forKey: .dateComponents) - try container.encode(format, forKey: .format) + try container.encode(String.fromDatabaseValue(databaseValue)!, forKey: .date) } } diff --git a/Tests/GRDBTests/FoundationDateComponentsTests.swift b/Tests/GRDBTests/FoundationDateComponentsTests.swift index e49b05389..aec1f882e 100644 --- a/Tests/GRDBTests/FoundationDateComponentsTests.swift +++ b/Tests/GRDBTests/FoundationDateComponentsTests.swift @@ -506,4 +506,27 @@ class FoundationDateComponentsTests : GRDBTestCase { let databaseDateComponents = DatabaseDateComponents.fromDatabaseValue("foo".databaseValue) XCTAssertTrue(databaseDateComponents == nil) } + + func testJSONEncodingOfDatabaseDateComponents() { + let year = 2018 + let month = 12 + let day = 31 + let dbDateComponents = DatabaseDateComponents( + DateComponents(year: year, month: month, day: day, hour: nil, minute: nil, second: nil, nanosecond: nil), + format: .YMD) + let encoded = try! JSONEncoder().encode(dbDateComponents) + XCTAssertEqual(String(data: encoded, encoding: .utf8)!, "{\"date\":\"\(year)-\(month)-\(day)\"}") + } + + func testJSONDecodingOfDatabaseDateComponents() { + let year = 2018 + let month = 12 + let day = 31 + let dbDateComponents = DatabaseDateComponents( + DateComponents(year: year, month: month, day: day, hour: nil, minute: nil, second: nil, nanosecond: nil), + format: .YMD) + let json = "{\"date\":\"\(year)-\(month)-\(day)\"}".data(using: .utf8)! + let decodedDatabaseDateComponents = try! JSONDecoder().decode(DatabaseDateComponents.self, from: json) + XCTAssertEqual(decodedDatabaseDateComponents.dateComponents, dbDateComponents.dateComponents) + } } From 62c6187ad41d076092404277209e49ab59bc1c33 Mon Sep 17 00:00:00 2001 From: Matt Tancock Date: Mon, 20 Apr 2020 14:42:02 +0100 Subject: [PATCH 3/3] Remove Format's Codable conformance. Use a singleValueContainer --- .../Foundation/DatabaseDateComponents.swift | 20 +++++++++---------- .../FoundationDateComponentsTests.swift | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift index 1963dbb1f..ccdcf4032 100644 --- a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift +++ b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift @@ -11,7 +11,7 @@ import SQLite3 public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnConvertible, Codable { /// The available formats for reading and storing date components. - public enum Format: String, Codable { + public enum Format: String { /// The format "yyyy-MM-dd". case YMD = "yyyy-MM-dd" @@ -162,19 +162,19 @@ public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnC // MARK: - Codable adoption - /// CodingKeys for codable conformance. - enum CodingKeys: String, CodingKey { - case date - } /// Creates a new instance by decoding from the given decoder. /// /// - parameters: /// - decoder: The decoder to read data from. public init(from decoder: Decoder) throws { - let container = try decoder.container(keyedBy: CodingKeys.self) - let stringValue = try container.decode(String.self, forKey: .date) - self = DatabaseDateComponents.fromDatabaseValue(stringValue.databaseValue)! + let container = try decoder.singleValueContainer() + let stringValue = try container.decode(String.self) + guard let decodedValue = DatabaseDateComponents.fromDatabaseValue(stringValue.databaseValue) else { + throw DecodingError.dataCorruptedError(in: container, + debugDescription: "Unable to initialise databaseDateComponent") + } + self = decodedValue } /// Encodes this value into the given encoder. @@ -182,7 +182,7 @@ public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnC /// - parameters: /// - encoder: The encoder to write data to. public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(String.fromDatabaseValue(databaseValue)!, forKey: .date) + var container = encoder.singleValueContainer() + try container.encode(String.fromDatabaseValue(databaseValue)!) } } diff --git a/Tests/GRDBTests/FoundationDateComponentsTests.swift b/Tests/GRDBTests/FoundationDateComponentsTests.swift index aec1f882e..696412109 100644 --- a/Tests/GRDBTests/FoundationDateComponentsTests.swift +++ b/Tests/GRDBTests/FoundationDateComponentsTests.swift @@ -515,7 +515,7 @@ class FoundationDateComponentsTests : GRDBTestCase { DateComponents(year: year, month: month, day: day, hour: nil, minute: nil, second: nil, nanosecond: nil), format: .YMD) let encoded = try! JSONEncoder().encode(dbDateComponents) - XCTAssertEqual(String(data: encoded, encoding: .utf8)!, "{\"date\":\"\(year)-\(month)-\(day)\"}") + XCTAssertEqual(String(data: encoded, encoding: .utf8)!, "\"\(year)-\(month)-\(day)\"") } func testJSONDecodingOfDatabaseDateComponents() { @@ -525,7 +525,7 @@ class FoundationDateComponentsTests : GRDBTestCase { let dbDateComponents = DatabaseDateComponents( DateComponents(year: year, month: month, day: day, hour: nil, minute: nil, second: nil, nanosecond: nil), format: .YMD) - let json = "{\"date\":\"\(year)-\(month)-\(day)\"}".data(using: .utf8)! + let json = "\"\(year)-\(month)-\(day)\"".data(using: .utf8)! let decodedDatabaseDateComponents = try! JSONDecoder().decode(DatabaseDateComponents.self, from: json) XCTAssertEqual(decodedDatabaseDateComponents.dateComponents, dbDateComponents.dateComponents) }