Skip to content

Commit 7711e55

Browse files
committed
HTTPRouteParameterValue
1 parent 08a3c71 commit 7711e55

File tree

2 files changed

+291
-2
lines changed

2 files changed

+291
-2
lines changed

FlyingFox/Sources/HTTPRouteParameterValue.swift

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,62 @@ extension String: HTTPRouteParameterValue {
4242
}
4343
}
4444

45-
extension Int: HTTPRouteParameterValue {
45+
extension Int: HTTPRouteParameterValue { }
46+
extension Int64: HTTPRouteParameterValue { }
47+
extension Int32: HTTPRouteParameterValue { }
48+
extension Int16: HTTPRouteParameterValue { }
49+
extension Int8: HTTPRouteParameterValue { }
50+
extension UInt: HTTPRouteParameterValue { }
51+
extension UInt64: HTTPRouteParameterValue { }
52+
extension UInt32: HTTPRouteParameterValue { }
53+
extension UInt16: HTTPRouteParameterValue { }
54+
extension UInt8: HTTPRouteParameterValue { }
55+
56+
public extension HTTPRouteParameterValue where Self: BinaryInteger {
57+
init(parameter: String) throws {
58+
guard let int64 = Int64(parameter),
59+
let value = Self(exactly: int64) else {
60+
throw HTTPRouteParameterInvalid()
61+
}
62+
self = value
63+
}
64+
}
65+
66+
extension Double: HTTPRouteParameterValue {
67+
public init(parameter: String) throws {
68+
guard let value = Self(parameter) else {
69+
throw HTTPRouteParameterInvalid()
70+
}
71+
self = value
72+
}
73+
}
74+
75+
extension Float32: HTTPRouteParameterValue {
4676
public init(parameter: String) throws {
47-
guard let value = Int(parameter)else {
77+
guard let value = Self(parameter) else {
78+
throw HTTPRouteParameterInvalid()
79+
}
80+
self = value
81+
}
82+
}
83+
84+
extension Bool: HTTPRouteParameterValue {
85+
public init(parameter: String) throws {
86+
switch parameter.lowercased() {
87+
case "true":
88+
self = true
89+
case "false":
90+
self = false
91+
default:
92+
throw HTTPRouteParameterInvalid()
93+
}
94+
}
95+
}
96+
97+
public extension HTTPRouteParameterValue where Self: RawRepresentable, RawValue: HTTPRouteParameterValue {
98+
init(parameter: String) throws {
99+
let rawValue = try RawValue(parameter: parameter)
100+
guard let value = Self(rawValue: rawValue)else {
48101
throw HTTPRouteParameterInvalid()
49102
}
50103
self = value
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
//
2+
// HTTPRouteParameterValueTests.swift
3+
// FlyingFox
4+
//
5+
// Created by Simon Whitty on 13/07/2024.
6+
// Copyright © 2024 Simon Whitty. All rights reserved.
7+
//
8+
// Distributed under the permissive MIT license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/FlyingFox
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining a copy
14+
// of this software and associated documentation files (the "Software"), to deal
15+
// in the Software without restriction, including without limitation the rights
16+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
// copies of the Software, and to permit persons to whom the Software is
18+
// furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in all
21+
// copies or substantial portions of the Software.
22+
//
23+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
// SOFTWARE.
30+
//
31+
32+
import FlyingFox
33+
import XCTest
34+
35+
final class HTTPRouteParameterValueTests: XCTestCase {
36+
37+
func testBoolConversion() {
38+
XCTAssertTrue(
39+
try Bool(parameter: "true")
40+
)
41+
XCTAssertTrue(
42+
try Bool(parameter: "TRUE")
43+
)
44+
XCTAssertFalse(
45+
try Bool(parameter: "false")
46+
)
47+
XCTAssertFalse(
48+
try Bool(parameter: "FALSE")
49+
)
50+
51+
XCTAssertThrowsError(
52+
try Bool(parameter: "fish")
53+
)
54+
}
55+
56+
func testStringConversion() {
57+
XCTAssertEqual(
58+
String(parameter: "fish"),
59+
"fish"
60+
)
61+
}
62+
63+
func testIntConversion() {
64+
XCTAssertEqual(
65+
try Int8(parameter: "100"),
66+
100
67+
)
68+
XCTAssertEqual(
69+
try Int8(parameter: "-100"),
70+
-100
71+
)
72+
XCTAssertEqual(
73+
try Int16(parameter: "10000"),
74+
10000
75+
)
76+
XCTAssertEqual(
77+
try Int16(parameter: "-10000"),
78+
-10000
79+
)
80+
XCTAssertEqual(
81+
try Int32(parameter: "1000000000"),
82+
1000000000
83+
)
84+
XCTAssertEqual(
85+
try Int32(parameter: "-1000000000"),
86+
-1000000000
87+
)
88+
XCTAssertEqual(
89+
try Int64(parameter: "1000000000000"),
90+
1000000000000
91+
)
92+
XCTAssertEqual(
93+
try Int64(parameter: "-1000000000000"),
94+
-1000000000000
95+
)
96+
97+
XCTAssertThrowsError(
98+
try Int8(parameter: "1000")
99+
)
100+
XCTAssertThrowsError(
101+
try Int8(parameter: "fish")
102+
)
103+
XCTAssertThrowsError(
104+
try Int16(parameter: "1000000000")
105+
)
106+
XCTAssertThrowsError(
107+
try Int16(parameter: "fish")
108+
)
109+
XCTAssertThrowsError(
110+
try Int32(parameter: "1000000000000")
111+
)
112+
XCTAssertThrowsError(
113+
try Int32(parameter: "fish")
114+
)
115+
XCTAssertThrowsError(
116+
try Int64(parameter: "1000000000000000000000")
117+
)
118+
XCTAssertThrowsError(
119+
try Int64(parameter: "fish")
120+
)
121+
}
122+
123+
func testUIntConversion() {
124+
XCTAssertEqual(
125+
try UInt8(parameter: "100"),
126+
100
127+
)
128+
XCTAssertEqual(
129+
try UInt16(parameter: "10000"),
130+
10000
131+
)
132+
XCTAssertEqual(
133+
try UInt32(parameter: "1000000000"),
134+
1000000000
135+
)
136+
XCTAssertEqual(
137+
try UInt64(parameter: "1000000000000"),
138+
1000000000000
139+
)
140+
141+
XCTAssertThrowsError(
142+
try UInt8(parameter: "1000")
143+
)
144+
XCTAssertThrowsError(
145+
try UInt8(parameter: "fish")
146+
)
147+
XCTAssertThrowsError(
148+
try UInt16(parameter: "1000000000")
149+
)
150+
XCTAssertThrowsError(
151+
try UInt16(parameter: "fish")
152+
)
153+
XCTAssertThrowsError(
154+
try UInt32(parameter: "1000000000000")
155+
)
156+
XCTAssertThrowsError(
157+
try UInt32(parameter: "fish")
158+
)
159+
XCTAssertThrowsError(
160+
try UInt64(parameter: "1000000000000000000000")
161+
)
162+
XCTAssertThrowsError(
163+
try UInt64(parameter: "fish")
164+
)
165+
}
166+
167+
func testDoubleConversion() {
168+
XCTAssertEqual(
169+
try Double(parameter: "10.5"),
170+
10.5
171+
)
172+
XCTAssertEqual(
173+
try Double(parameter: "-10.5"),
174+
-10.5
175+
)
176+
177+
XCTAssertThrowsError(
178+
try Double(parameter: "fish")
179+
)
180+
}
181+
182+
func testFloatConversion() {
183+
XCTAssertEqual(
184+
try Float(parameter: "10.5"),
185+
10.5
186+
)
187+
XCTAssertEqual(
188+
try Float(parameter: "-10.5"),
189+
-10.5
190+
)
191+
192+
XCTAssertThrowsError(
193+
try Float(parameter: "fish")
194+
)
195+
}
196+
197+
func testFloat32Conversion() {
198+
XCTAssertEqual(
199+
try Float32(parameter: "10.5"),
200+
10.5
201+
)
202+
XCTAssertEqual(
203+
try Float32(parameter: "-10.5"),
204+
-10.5
205+
)
206+
207+
XCTAssertThrowsError(
208+
try Float32(parameter: "fish")
209+
)
210+
}
211+
212+
func testEnumConversion() {
213+
enum Food: String, HTTPRouteParameterValue {
214+
case fish
215+
case chips
216+
}
217+
218+
XCTAssertEqual(
219+
try Food(parameter: "fish"),
220+
.fish
221+
)
222+
XCTAssertEqual(
223+
try Food(parameter: "chips"),
224+
.chips
225+
)
226+
227+
XCTAssertThrowsError(
228+
try Food(parameter: "10")
229+
)
230+
}
231+
}
232+
233+
private enum Food: String, HTTPRouteParameterValue {
234+
case fish
235+
case chips
236+
}

0 commit comments

Comments
 (0)