This repository was archived by the owner on Nov 16, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 22
Add support for sorted keys. #9
Open
jseibert
wants to merge
3
commits into
vapor:master
Choose a base branch
from
digits:sorted
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,6 +11,22 @@ | |
| /// See [Mozilla's](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) docs for more information about | ||
| /// url-encoded forms. | ||
| public final class URLEncodedFormEncoder: DataEncoder { | ||
| /// The formatting of the output data. | ||
| public struct OutputFormatting : OptionSet { | ||
| /// The format's default value. | ||
| public let rawValue: UInt | ||
|
|
||
| /// Creates an OutputFormatting value with the given raw value. | ||
| public init(rawValue: UInt) { | ||
| self.rawValue = rawValue | ||
| } | ||
|
|
||
| /// Produce output with dictionary keys sorted in lexicographic order. | ||
| public static let sortedKeys = OutputFormatting(rawValue: 1 << 1) | ||
| } | ||
|
|
||
| public var outputFormatting: OutputFormatting = [] | ||
|
Member
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. Vapor style is to set all properties in the init method. We try to avoid default properties on members since they can be easy to miss when refactoring. |
||
|
|
||
| /// Create a new `URLEncodedFormEncoder`. | ||
| public init() {} | ||
|
|
||
|
|
@@ -28,7 +44,7 @@ public final class URLEncodedFormEncoder: DataEncoder { | |
| let context = URLEncodedFormDataContext(.dict([:])) | ||
| let encoder = _URLEncodedFormEncoder(context: context, codingPath: []) | ||
| try encodable.encode(to: encoder) | ||
| let serializer = URLEncodedFormSerializer() | ||
| let serializer = URLEncodedFormSerializer(sortedKeys: self.outputFormatting.contains(.sortedKeys)) | ||
| guard case .dict(let dict) = context.data else { | ||
| throw URLEncodedFormError( | ||
| identifier: "invalidTopLevel", | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,19 +3,37 @@ import Bits | |
| /// Converts `[String: URLEncodedFormData]` structs to `Data`. | ||
| final class URLEncodedFormSerializer { | ||
| /// Default form url encoded serializer. | ||
| static let `default` = URLEncodedFormSerializer() | ||
| static let `default` = URLEncodedFormSerializer(sortedKeys: false) | ||
|
|
||
| private var sortedKeys: Bool | ||
|
Member
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. Missing comment |
||
|
|
||
| /// Create a new form-urlencoded data serializer. | ||
| init() {} | ||
| init(sortedKeys: Bool) { | ||
| self.sortedKeys = sortedKeys | ||
| } | ||
|
|
||
| /// Serializes the data. | ||
| func serialize(_ URLEncodedFormEncoded: [String: URLEncodedFormData]) throws -> Data { | ||
| var data: [Data] = [] | ||
| for (key, val) in URLEncodedFormEncoded { | ||
| let key = try key.urlEncodedFormEncoded() | ||
| let subdata = try serialize(val, forKey: key) | ||
| data.append(subdata) | ||
|
|
||
| if self.sortedKeys { | ||
| let elements = URLEncodedFormEncoded.sorted { (left, right) -> Bool in | ||
| return left.key.compare(right.key, options: [.numeric, .caseInsensitive, .forcedOrdering]) == .orderedAscending | ||
| } | ||
|
|
||
| for (key, val) in elements { | ||
| let key = try key.urlEncodedFormEncoded() | ||
| let subdata = try serialize(val, forKey: key) | ||
|
||
| data.append(subdata) | ||
| } | ||
| } else { | ||
| for (key, val) in URLEncodedFormEncoded { | ||
| let key = try key.urlEncodedFormEncoded() | ||
| let subdata = try serialize(val, forKey: key) | ||
| data.append(subdata) | ||
| } | ||
| } | ||
|
|
||
| return data.joinedWithAmpersands() | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
JSON coder calls it "writing options". We should probably follow suit.
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.
Maybe we're looking at different things? This code is copied from https://github.com/apple/swift/blob/master/stdlib/public/SDK/Foundation/JSONEncoder.swift where it is called:
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.
Small change here: