-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add property to set encoding format for Date
properties.
#110
Changes from 8 commits
1f294b2
e87200e
9a94bf8
0c36a9b
f827388
50ef9b3
1b33f52
9365403
7bed56b
83f19a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,18 @@ import KituraContracts | |
import Foundation | ||
import Dispatch | ||
|
||
/// Defines the supported formats for persisiting properties of type `Date`. | ||
public enum DateEncodingFormat { | ||
// time - Corresponds to the `time` column type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we intend to have these comments (describing each case) to be a part of the API docs, they'd need to start with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think we must document these in the API docs :) |
||
case time | ||
// date - Corresponds to the `date` column type | ||
case date | ||
// timestamp - Corresponds to the `timestamp` column type. | ||
case timestamp | ||
// double - This is the default encoding type and corresponds to Swifts encoding of `Date`. | ||
case double | ||
} | ||
|
||
/// Protocol Model conforming to Codable defining the available operations | ||
public protocol Model: Codable { | ||
/// Defines the tableName in the Database | ||
|
@@ -34,6 +46,9 @@ public protocol Model: Codable { | |
/// Defines the keypath to the Models id field | ||
static var idKeypath: IDKeyPath {get} | ||
|
||
/// Defines the format in which `Date` properties of the `Model` will be written to the Database. Defaults to .double | ||
static var dateEncodingFormat: DateEncodingFormat { get } | ||
|
||
/// Call to create the table in the database synchronously | ||
static func createTableSync(using db: Database?) throws -> Bool | ||
|
||
|
@@ -120,6 +135,8 @@ public extension Model { | |
|
||
static var idKeypath: IDKeyPath { return nil } | ||
|
||
static var dateEncodingFormat: DateEncodingFormat { return .double } | ||
|
||
private static func executeTask(using db: Database? = nil, task: @escaping ((Connection?, QueryError?) -> ())) { | ||
guard let database = db ?? Database.default else { | ||
|
||
|
@@ -233,7 +250,7 @@ public extension Model { | |
var values: [String : Any] | ||
do { | ||
table = try Self.getTable() | ||
values = try DatabaseEncoder().encode(self) | ||
values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingFormat) | ||
} catch let error { | ||
onCompletion(nil, Self.convertError(error)) | ||
return | ||
|
@@ -252,7 +269,7 @@ public extension Model { | |
var values: [String : Any] | ||
do { | ||
table = try Self.getTable() | ||
values = try DatabaseEncoder().encode(self) | ||
values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingFormat) | ||
} catch let error { | ||
onCompletion(nil, nil, Self.convertError(error)) | ||
return | ||
|
@@ -270,7 +287,7 @@ public extension Model { | |
var values: [String: Any] | ||
do { | ||
table = try Self.getTable() | ||
values = try DatabaseEncoder().encode(self) | ||
values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingFormat) | ||
} catch let error { | ||
onCompletion(nil, Self.convertError(error)) | ||
return | ||
|
@@ -500,7 +517,7 @@ public extension Model { | |
|
||
var decodedModel: Self | ||
do { | ||
decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue) | ||
decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue, dateEncodingStrategy: Self.dateEncodingFormat) | ||
} catch { | ||
onCompletion(nil, Self.convertError(error)) | ||
return | ||
|
@@ -563,7 +580,7 @@ public extension Model { | |
|
||
var decodedModel: Self | ||
do { | ||
decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue) | ||
decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue, dateEncodingStrategy: Self.dateEncodingFormat) | ||
} catch { | ||
onCompletion(nil, nil, Self.convertError(error)) | ||
return | ||
|
@@ -619,7 +636,7 @@ public extension Model { | |
for dictionary in dictionariesTitleToValue { | ||
var decodedModel: Self | ||
do { | ||
decodedModel = try DatabaseDecoder().decode(Self.self, dictionary) | ||
decodedModel = try DatabaseDecoder().decode(Self.self, dictionary, dateEncodingStrategy: Self.dateEncodingFormat) | ||
} catch { | ||
onCompletion(nil, Self.convertError(error)) | ||
return | ||
|
@@ -684,7 +701,7 @@ public extension Model { | |
for dictionary in dictionariesTitleToValue { | ||
var decodedModel: Self | ||
do { | ||
decodedModel = try DatabaseDecoder().decode(Self.self, dictionary) | ||
decodedModel = try DatabaseDecoder().decode(Self.self, dictionary, dateEncodingStrategy: Self.dateEncodingFormat) | ||
} catch let error { | ||
onCompletion(nil, Self.convertError(error)) | ||
return | ||
|
@@ -754,7 +771,7 @@ public extension Model { | |
|
||
static func getTable() throws -> Table { | ||
let idKeyPathSet: Bool = Self.idKeypath != nil | ||
return try Database.tableInfo.getTable((Self.idColumnName, Self.idColumnType, idKeyPathSet), Self.tableName, for: Self.self) | ||
return try Database.tableInfo.getTable((Self.idColumnName, Self.idColumnType, idKeyPathSet), Self.tableName, for: Self.self, with: Self.dateEncodingFormat) | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ” at the end of this line looks like a typo?