Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
URLEncodedForm date encoding strategy (#2273)
* Added ability to configure date coding/decoding for UrlEncodedForm * Added documentation to the `DateFormat` enum * renamed internetDateTime to iso8601 * Added custom date formatter * Added comment about `ISO8601DateFormatter` performance * Added ISO8601DateFormatter.threadSpecific so a new ISO8601DateFormatter isn't created for every encode/decode. * Use typealias instead of declaring DateFormat 2x * Added `ThreadSpecificDateFormatter` to ensure thread safety when using a `custom` DateFormat for `URLEncodedFormEncoder` or `URLEncodedFormDecoder` * Fixed comment * Changed custom interface to mimic `JSONDecoder.DateDecodingStrategy.custom(_:)` and `JSONEncoder.DateEncodingStrategy.custom(_:)` interfaces * Removed unused ThreadSpecificDateFormatter * `ISO8601DateFormatter` seems to be thread safe. This bug was filed: https://bugs.swift.org/browse/SR-7745?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel I tried running the sample program with `10000000` iterations in the `vapor/swift:5.2` docker image without any issue. * Default the date format for URLEncodedFrom coding/decoding to `timeIntervalSinceReferenceDate` * Default to `timeIntervalSince1970` * Removed `timeIntervalSinceReferenceDate` option * Made `Date: URLQueryFragmentConvertible` * Date decoding uses an array of DateFormats and tries in order * Comments * Removed unnecessary import * Comments * Reintroduced threadSpecific `ISO8601DateFormatter` * Renamed unixTimestamp to secondsSince1970 to match Apple's naming convention * Throw last error when decodingDate using multiple decoding approaches * Use `DateDecodingStrategy` and `DateEncodingStrategy` naming convention * Updated test cases * Updated documentation * Added additional default test and updated documentation * Improved error handling * Removed unnecessary comment * Replaced dateDecodingStrategies with dateDecodingStrategy
- Loading branch information
1 parent
9b4ebf7
commit 7f6c827
Showing
4 changed files
with
173 additions
and
8 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
Sources/Vapor/URLEncodedForm/DateFormatter+threadSpecific.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import NIO | ||
|
||
fileprivate final class ISO8601 { | ||
fileprivate static let threadSpecific: ThreadSpecificVariable<ISO8601DateFormatter> = .init() | ||
} | ||
|
||
extension ISO8601DateFormatter { | ||
static var threadSpecific: ISO8601DateFormatter { | ||
if let existing = ISO8601.threadSpecific.currentValue { | ||
return existing | ||
} else { | ||
let new = ISO8601DateFormatter() | ||
ISO8601.threadSpecific.currentValue = new | ||
return new | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters