Skip to content

Commit

Permalink
HTTPRouteParameterValue
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Jul 13, 2024
1 parent 7711e55 commit 79cf615
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions FlyingFox/Sources/HTTPRequest+RouteParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public extension Array where Element == HTTPRequest.RouteParameter {
first { $0.name == name }?.value
}
}

subscript<T: HTTPRouteParameterValue>(_ name: String, of type: T.Type = T.self) -> T? {
guard let text = first(where: { $0.name == name })?.value,
let value = try? T(parameter: text) else {
return nil
}
return value
}
}
20 changes: 20 additions & 0 deletions FlyingFox/Tests/HTTPRouteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,26 @@ final class HTTPRouteTests: XCTestCase {
)
}

func testRouteParameterValuesA() {
let route = HTTPRoute("GET /:foo/:bar")
enum Beast: String, HTTPRouteParameterValue {
case fish
}

XCTAssertEqual(
route.extractParameters(from: .make("/10/fish"))["foo"],
10
)
XCTAssertEqual(
route.extractParameters(from: .make("/20/fish"))["bar"],
"fish"
)
XCTAssertEqual(
route.extractParameters(from: .make("/20/fish"))["bar"],
Beast.fish
)
}

#if compiler(>=5.9)
func testPathParameters() {
// given
Expand Down

0 comments on commit 79cf615

Please sign in to comment.