mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Merge pull request #766 from DiligentRobot/development
Add Codable support to DatabaseDateComponents
This commit is contained in:
2 files changed
+50
-1
No files matched your search
@@ -8,7 +8,7 @@ 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 {
|
||||
@@ -159,4 +159,30 @@ public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnC
|
||||
|
||||
return SQLiteDateParser().components(from: string)
|
||||
}
|
||||
|
||||
// MARK: - Codable adoption
|
||||
|
||||
|
||||
/// 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.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.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - encoder: The encoder to write data to.
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(String.fromDatabaseValue(databaseValue)!)
|
||||
}
|
||||
}
|
||||
@@ -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)!, "\"\(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 = "\"\(year)-\(month)-\(day)\"".data(using: .utf8)!
|
||||
let decodedDatabaseDateComponents = try! JSONDecoder().decode(DatabaseDateComponents.self, from: json)
|
||||
XCTAssertEqual(decodedDatabaseDateComponents.dateComponents, dbDateComponents.dateComponents)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user