Skip to content

Commit

Permalink
fix: Make QueryParams extension public (#49)
Browse files Browse the repository at this point in the history
* Improve documentation for default date formatting params
* Make default date formatter singleton
  • Loading branch information
djones6 authored Sep 23, 2019
1 parent 316e301 commit 5211127
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 22 deletions.
24 changes: 14 additions & 10 deletions Sources/KituraContracts/CodableQuery/Coder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,34 @@ import Foundation

### Usage Example: ###
````swift
let date = Coder().dateFormatter.date(from: "2017-10-31T16:15:56+0000")!
let date = Coder.defaultDateFormatter.date(from: "2017-10-31T16:15:56+0000")!
````
*/
public class Coder {

@available(*, deprecated, message: "Use Coder.defaultDateFormatter instead")
public let dateFormatter: DateFormatter = Coder.defaultDateFormatter

/**
The designated [DateFormatter](https://developer.apple.com/documentation/foundation/dateformatter) used for encoding and decoding query parameters.
The default [DateFormatter](https://developer.apple.com/documentation/foundation/dateformatter) used for encoding and decoding query parameters. It uses the "UTC" timezone and "yyyy-MM-dd'T'HH:mm:ssZ" date format.

### Usage Example: ###
````swift
let date = Coder().dateFormatter.date(from: "2017-10-31T16:15:56+0000")
let date = Coder.defaultDateFormatter.date(from: "2017-10-31T16:15:56+0000")
````
*/
public let dateFormatter: DateFormatter
public static let defaultDateFormatter: DateFormatter = {
let value = DateFormatter()
value.timeZone = TimeZone(identifier: "UTC")
value.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return value
}()

/**
Initializes a `Coder` instance with a `DateFormatter`
using the "UTC" timezone and "yyyy-MM-dd'T'HH:mm:ssZ" date format.
*/
public init() {
self.dateFormatter = DateFormatter()
self.dateFormatter.timeZone = TimeZone(identifier: "UTC")
self.dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
}
public init() {}

/**
Helper method to extract the field name from a `CodingKey` array.
Expand Down
2 changes: 1 addition & 1 deletion Sources/KituraContracts/CodableQuery/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ extension String {
- 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(decoderStrategy: JSONDecoder.DateDecodingStrategy = .formatted(Coder().dateFormatter), decoder: Decoder?=nil) -> [Date]? {
public func dateArray(decoderStrategy: JSONDecoder.DateDecodingStrategy = .formatted(Coder.defaultDateFormatter), decoder: Decoder?=nil) -> [Date]? {

switch decoderStrategy {
case .formatted(let formatter):
Expand Down
4 changes: 2 additions & 2 deletions Sources/KituraContracts/CodableQuery/QueryDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public class QueryDecoder: Coder, Decoder, BodyDecoder {
Initializer with an empty dictionary for decoding from Data.
*/
public override init () {
self.dateDecodingStrategy = .formatted(Coder().dateFormatter)
self.dateDecodingStrategy = .formatted(Coder.defaultDateFormatter)
self.dictionary = [:]
super.init()
}
/**
Initializer with a `[String : String]` dictionary.
*/
public init(dictionary: [String : String]) {
self.dateDecodingStrategy = .formatted(Coder().dateFormatter)
self.dateDecodingStrategy = .formatted(Coder.defaultDateFormatter)
self.dictionary = dictionary
super.init()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/KituraContracts/CodableQuery/QueryEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class QueryEncoder: Coder, Encoder, BodyEncoder {
Initializer for the dictionary, which initializes an empty `[String: String]` dictionary.
*/
public override init() {
self.dateEncodingStrategy = .formatted(Coder().dateFormatter)
self.dateEncodingStrategy = .formatted(Coder.defaultDateFormatter)
self.dictionary = [:]
self.anyDictionary = [:]
super.init()
Expand Down
21 changes: 15 additions & 6 deletions Sources/KituraContracts/Contracts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,9 @@ public protocol QueryParams: Codable {
/**
The decoding strategy for Dates.
The variable can be defined within your QueryParams object and tells the `QueryDecoder` how dates should be decoded. The enum used for the DateDecodingStrategy is the same one found in the `JSONDecoder`.

### Usage Example: ###

```swift
struct MyQuery: QueryParams {
let date: Date
Expand All @@ -543,9 +545,11 @@ public protocol QueryParams: Codable {
static var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy { get }

/**
The encoding strategy for Dates.
The variable would be defined within your QueryParams object and tells the `QueryEncoder` how dates should be encoded. The enum used for the DateEncodingStrategy is the same one found in the `JSONEncoder`.
The encoding strategy for Dates.
The variable would be defined within your QueryParams object and tells the `QueryEncoder` how dates should be encoded. The enum used for the DateEncodingStrategy is the same one found in the `JSONEncoder`.

### Usage Example: ###

```swift
struct MyQuery: QueryParams {
let date: Date
Expand All @@ -561,14 +565,19 @@ public protocol QueryParams: Codable {
static var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy { get }
}

/// Defines default values for the `dateDecodingStrategy` and `dateEncodingStrategy`. The
/// default formatting for a `Date` in a `QueryParams` type is defined by `Coder.dateFormatter`,
/// which uses the "UTC" timezone and "yyyy-MM-dd'T'HH:mm:ssZ" date format.
extension QueryParams {

static var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy {
return .formatted(Coder().dateFormatter)
/// Default value: `Coder.defaultDateFormatter`
public static var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy {
return .formatted(Coder.defaultDateFormatter)
}

static var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy {
return .formatted(Coder().dateFormatter)
/// Default value: `Coder.defaultDateFormatter`
public static var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy {
return .formatted(Coder.defaultDateFormatter)
}

}
Expand Down
4 changes: 2 additions & 2 deletions Tests/KituraContractsTests/QueryCoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class QueryCoderTests: XCTestCase {
}

let expectedDateStr = "2017-10-31T16:15:56+0000"
let expectedDate = Coder().dateFormatter.date(from: "2017-10-31T16:15:56+0000")!
let expectedDate = Coder.defaultDateFormatter.date(from: "2017-10-31T16:15:56+0000")!

let expectedMyQuery = MyQuery(boolField: true,
intField: 23,
Expand All @@ -282,7 +282,7 @@ class QueryCoderTests: XCTestCase {
emptyStringField: "",
optionalStringField: nil,
intArray: [1, 2, 3],
dateField: Coder().dateFormatter.date(from: "2017-10-31T16:15:56+0000")!,
dateField: Coder.defaultDateFormatter.date(from: "2017-10-31T16:15:56+0000")!,
optionalDateField: nil,
nested: Nested(nestedIntField: 333, nestedStringField: "nested string"))

Expand Down

0 comments on commit 5211127

Please sign in to comment.