Skip to content
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

Added QueryEncoder and QueryDecoder support for alternate date formats #45

Merged
merged 18 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 85 additions & 10 deletions Sources/KituraContracts/CodableQuery/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,87 @@ extension String {
}

/**
Converts the given String to a [Date]?.
Converts the given String to a [Date]? object using the dateDecodingStrategy supplied.

- Parameter formatter: The designated DateFormatter to convert the string with.
- Parameter formatter: The designated `DateFormatter` to convert the string with.
- Parameter decoderStrategy: The `JSON.dateDecodingStrategy` that should be used to decode the specifed Date. Default is set to .formatted with default dateFormatter.
- Parameter decoder: The `Decoder` parameter is only used for the custom strategy.
- Returns: The [Date]? object. Some on success / nil on failure.
*/
public func dateArray(_ formatter: DateFormatter) -> [Date]? {
let strs: [String] = self.components(separatedBy: ",")
let dates = strs.map { formatter.date(from: $0) }.filter { $0 != nil }.map { $0! }
if dates.count == strs.count {
return dates
public func dateArray(_ formatter: DateFormatter?=nil, decoderStrategy: JSONDecoder.DateDecodingStrategy = .formatted(Coder().dateFormatter), decoder: Decoder?=nil) -> [Date]? {

switch decoderStrategy {
CameronMcWilliam marked this conversation as resolved.
Show resolved Hide resolved
case .formatted(let formatter):
let strs: [String] = self.components(separatedBy: ",")
let dates = strs.map { formatter.date(from: $0) }.filter { $0 != nil }.map { $0! }
if dates.count == strs.count {
return dates
}
return nil
case .deferredToDate:
let strs: [String] = self.components(separatedBy: ",")
#if swift(>=4.1)
let dbs = strs.compactMap(Double.init)
#else
let dbs = strs.flatMap(Double.init)
#endif
let dates = dbs.map { Date(timeIntervalSinceReferenceDate: $0) }
if dates.count == dbs.count {
return dates
}
return nil
case .secondsSince1970:
let strs: [String] = self.components(separatedBy: ",")
#if swift(>=4.1)
let dbs = strs.compactMap(Double.init)
#else
let dbs = strs.flatMap(Double.init)
#endif
let dates = dbs.map { Date(timeIntervalSince1970: $0) }
if dates.count == dbs.count {
return dates
}
return nil
case .millisecondsSince1970:
let strs: [String] = self.components(separatedBy: ",")
#if swift(>=4.1)
let dbs = strs.compactMap(Double.init)
#else
let dbs = strs.flatMap(Double.init)
#endif
let dates = dbs.map { Date(timeIntervalSince1970: ($0)/1000) }
if dates.count == dbs.count {
return dates
}
return nil
case .iso8601:
let strs: [String] = self.components(separatedBy: ",")
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
let dates = strs.map { _iso8601Formatter.date(from: $0) }
if dates.count == strs.count {
return dates as? [Date]
}
return nil
} else {
fatalError("ISO8601DateFormatter is unavailable on this platform.")
}
case .custom(let closure):
var dateArray: [Date] = []
guard let decoder = decoder else {return dateArray}
var fieldValueArray = self.split(separator: ",")
for _ in fieldValueArray {
// Call closure to decode value
let date = try? closure(decoder)
dateArray.append(date!)
CameronMcWilliam marked this conversation as resolved.
Show resolved Hide resolved
// Delete from array after use
fieldValueArray.removeFirst()
}
return dateArray
default:
Log.error("Decoding strategy not found")
fatalError()
CameronMcWilliam marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

/// Helper Method to decode a string to an LosslessStringConvertible array types.
private func decodeArray<T: LosslessStringConvertible>(_ type: T.Type) -> [T]? {
let strs: [String] = self.components(separatedBy: ",")
Expand Down Expand Up @@ -235,7 +302,7 @@ extension String {
}
let key = String(self[..<range.lowerBound])
let value = String(self[range.upperBound...])

let valueReplacingPlus = value.replacingOccurrences(of: "+", with: " ")
let decodedValue = valueReplacingPlus.removingPercentEncoding
if decodedValue == nil {
Expand All @@ -244,3 +311,11 @@ extension String {
return (key: key, value: decodedValue ?? valueReplacingPlus)
}
}

/// ISO8601 Formatter used for formatting ISO8601 dates.
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
var _iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime
return formatter
}()
72 changes: 66 additions & 6 deletions Sources/KituraContracts/CodableQuery/QueryDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,24 @@ public class QueryDecoder: Coder, Decoder, BodyDecoder {
*/
public var dictionary: [String : String]


/**
A `JSONDecoder.DateDecodingStrategy` date decoder used to determine what strategy
to use when decoding the specific date.
*/
public var dateDecoder: JSONDecoder.DateDecodingStrategy
CameronMcWilliam marked this conversation as resolved.
Show resolved Hide resolved
/**
Initializer with an empty dictionary for decoding from Data.
*/
public override init() {
public init(dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .formatted(Coder().dateFormatter)) {
self.dateDecoder = dateDecodingStrategy
CameronMcWilliam marked this conversation as resolved.
Show resolved Hide resolved
self.dictionary = [:]
super.init()
}
/**
Initializer with a `[String : String]` dictionary.
*/
public init(dictionary: [String : String]) {
public init(dictionary: [String : String], dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .formatted(Coder().dateFormatter)) {
self.dateDecoder = dateDecodingStrategy
CameronMcWilliam marked this conversation as resolved.
Show resolved Hide resolved
self.dictionary = dictionary
super.init()
}
Expand All @@ -90,10 +96,13 @@ public class QueryDecoder: Coder, Decoder, BodyDecoder {
````
*/
public func decode<T: Decodable>(_ type: T.Type, from data: Data) throws -> T {
if let Q = T.self as? QueryParams.Type {
dateDecoder = Q.dateDecoder
}
guard let urlString = String(data: data, encoding: .utf8) else {
throw RequestError.unprocessableEntity
}
let decoder = QueryDecoder(dictionary: urlString.urlDecodedFieldValuePairs)
let decoder = QueryDecoder(dictionary: urlString.urlDecodedFieldValuePairs, dateDecodingStrategy: dateDecoder)
return try T(from: decoder)
}

Expand All @@ -111,6 +120,9 @@ public class QueryDecoder: Coder, Decoder, BodyDecoder {
````
*/
public func decode<T: Decodable>(_ type: T.Type) throws -> T {
if let Q = T.self as? QueryParams.Type {
dateDecoder = Q.dateDecoder
}
let fieldName = Coder.getFieldName(from: codingPath)
let fieldValue = dictionary[fieldName]
Log.verbose("fieldName: \(fieldName), fieldValue: \(String(describing: fieldValue))")
Expand Down Expand Up @@ -175,9 +187,56 @@ public class QueryDecoder: Coder, Decoder, BodyDecoder {
return try decodeType(fieldValue?.doubleArray, to: T.self)
/// Dates
case is Date.Type:
return try decodeType(fieldValue?.date(dateFormatter), to: T.self)
switch dateDecoder {
case .deferredToDate:
guard let doubleValue = fieldValue?.double else {return try decodeType(fieldValue, to: T.self)}
return try decodeType(Date(timeIntervalSinceReferenceDate: (doubleValue)), to: T.self)
case .secondsSince1970:
guard let doubleValue = fieldValue?.double else {return try decodeType(fieldValue, to: T.self)}
return try decodeType(Date(timeIntervalSince1970: (doubleValue)), to: T.self)
case .millisecondsSince1970:
guard let doubleValue = fieldValue?.double else {return try decodeType(fieldValue, to: T.self)}
return try decodeType(Date(timeIntervalSince1970: (doubleValue)), to: T.self)
case .iso8601:
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
guard let stringValue = fieldValue?.string else {return try decodeType(fieldValue, to: T.self)}
guard let date = _iso8601Formatter.date(from: stringValue) else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected date string to be ISO8601-formatted."))
}
return try decodeType(date, to: T.self)
} else {
fatalError("ISO8601DateFormatter is unavailable on this platform.")
}
case .formatted(let formatted):
return try decodeType(fieldValue?.date(formatted), to: T.self)
case .custom(let closure):
return try decodeType(closure(self), to: T.self)
default:
Log.warning("Unknown decoding strategy")
return try decodeType(fieldValue, to: T.self)
CameronMcWilliam marked this conversation as resolved.
Show resolved Hide resolved
}
case is [Date].Type:
return try decodeType(fieldValue?.dateArray(dateFormatter), to: T.self)
switch dateDecoder {
case .deferredToDate:
return try decodeType(fieldValue?.dateArray(decoderStrategy: .deferredToDate), to: T.self)
case .secondsSince1970:
return try decodeType(fieldValue?.dateArray(decoderStrategy: .secondsSince1970), to: T.self)
case .millisecondsSince1970:
return try decodeType(fieldValue?.dateArray(decoderStrategy: .millisecondsSince1970), to: T.self)
case .iso8601:
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
return try decodeType(fieldValue?.dateArray(decoderStrategy: .iso8601), to: T.self)
} else {
fatalError("ISO8601DateFormatter is unavailable on this platform.")
}
case .formatted(let formatter):
return try decodeType(fieldValue?.dateArray(formatter), to: T.self)
case .custom(let closure):
return try decodeType(fieldValue?.dateArray(decoderStrategy: .custom(closure), decoder: self), to: T.self)
default:
Log.warning("Unknown decoding strategy")
return try decodeType(fieldValue, to: T.self)
CameronMcWilliam marked this conversation as resolved.
Show resolved Hide resolved
}
/// Strings
case is String.Type:
return try decodeType(fieldValue?.string, to: T.self)
Expand Down Expand Up @@ -343,4 +402,5 @@ public class QueryDecoder: Coder, Decoder, BodyDecoder {
return decoder
}
}

}
Loading