-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(request): Handle array query parameters (#5)
- Loading branch information
Showing
5 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Foundation | ||
|
||
extension Dictionary where Key == String, Value == QueryParam { | ||
/// transform query params into URLQueryItem` | ||
var queryItems: [URLQueryItem] { | ||
self.flatMap { key, value -> [URLQueryItem] in | ||
switch value.queryValue { | ||
case .single(let value): | ||
return [URLQueryItem(name: key, value: value)] | ||
case .collection(let values): | ||
return values.map { URLQueryItem(name: "\(key)[]", value: $0) } | ||
case .none: | ||
return [] | ||
} | ||
} | ||
} | ||
|
||
} |
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,56 @@ | ||
import Foundation | ||
|
||
/// Protocol allowing to use a type as a query parameter | ||
public protocol QueryParam { | ||
/// the parameter value | ||
var queryValue: QueryValue? { get } | ||
} | ||
|
||
/// Query parameter value | ||
public enum QueryValue: Equatable { | ||
case single(String) | ||
case collection([String]) | ||
} | ||
|
||
extension QueryParam where Self: RawRepresentable, RawValue: QueryParam { | ||
public var queryValue: QueryValue? { rawValue.queryValue } | ||
} | ||
|
||
extension Int: QueryParam { | ||
public var queryValue: QueryValue? { .single(String(self)) } | ||
} | ||
|
||
extension String: QueryParam { | ||
public var queryValue: QueryValue? { .single(self) } | ||
} | ||
|
||
extension Bool: QueryParam { | ||
public var queryValue: QueryValue? { .single(self ? "true" : "false") } | ||
} | ||
|
||
extension Data: QueryParam { | ||
public var queryValue: QueryValue? { String(data: self, encoding: .utf8).map(QueryValue.single) } | ||
} | ||
|
||
extension Optional: QueryParam where Wrapped: QueryParam { | ||
public var queryValue: QueryValue? { | ||
self.flatMap { $0.queryValue } | ||
} | ||
} | ||
|
||
extension Array: QueryParam where Element: QueryParam { | ||
public var queryValue: QueryValue? { | ||
let values = self | ||
.compactMap { $0.queryValue } | ||
.flatMap { queryValue -> [String] in | ||
switch queryValue { | ||
case .single(let value): | ||
return [value] | ||
case .collection(let values): | ||
return values | ||
} | ||
} | ||
|
||
return .collection(values) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import XCTest | ||
import SimpleHTTP | ||
|
||
class QueryParamTests: XCTestCase { | ||
|
||
func test_queryValue_multidimenstionalArray_returnFlattenCollection() { | ||
let array: [[Int]] = [[1, 2, 3],[4,5,6]] | ||
|
||
XCTAssertEqual(array.queryValue, .collection(["1", "2", "3", "4", "5", "6"])) | ||
} | ||
|
||
} |