Skip to content

Commit d3712b3

Browse files
committed
Failable conversion
1 parent 177be64 commit d3712b3

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

Sources/AsyncHTTP/Utils/Converted.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ public protocol ConversionStrategy {
66
}
77

88
public protocol DecoderStrategy: ConversionStrategy {
9-
static func decode(_ value: RawValue) -> ConvertedValue
9+
static func decode(_ value: RawValue) throws -> ConvertedValue
1010
}
1111

1212
public protocol EncoderStrategy: ConversionStrategy {
13-
static func encode(_ value: ConvertedValue) -> RawValue
13+
static func encode(_ value: ConvertedValue) throws -> RawValue
1414
}
1515

1616
public typealias TwoWayConversionStrategy = EncoderStrategy & DecoderStrategy
@@ -31,14 +31,14 @@ extension Converted: Decodable where Converter.RawValue: Decodable, Converter: D
3131
public init(from decoder: Decoder) throws {
3232
let container = try decoder.singleValueContainer()
3333
let rawValue = try container.decode(Converter.RawValue.self)
34-
let decodedValue = Converter.decode(rawValue)
34+
let decodedValue = try Converter.decode(rawValue)
3535
self.init(wrappedValue: decodedValue)
3636
}
3737
}
3838

3939
extension Converted: Encodable where Converter.RawValue: Encodable, Converter: EncoderStrategy {
4040
public func encode(to encoder: Encoder) throws {
41-
let rawValue = Converter.encode(wrappedValue)
41+
let rawValue = try Converter.encode(wrappedValue)
4242
try rawValue.encode(to: encoder)
4343
}
4444
}
@@ -49,14 +49,14 @@ extension Optional: ConversionStrategy where Wrapped: ConversionStrategy {
4949
}
5050

5151
extension Optional: EncoderStrategy where Wrapped: EncoderStrategy {
52-
public static func encode(_ value: Wrapped.ConvertedValue?) -> Wrapped.RawValue? {
53-
value.map(Wrapped.encode)
52+
public static func encode(_ value: Wrapped.ConvertedValue?) throws -> Wrapped.RawValue? {
53+
try value.map(Wrapped.encode)
5454
}
5555
}
5656

5757
extension Optional: DecoderStrategy where Wrapped: DecoderStrategy {
58-
public static func decode(_ value: Wrapped.RawValue?) -> Wrapped.ConvertedValue? {
59-
value.map(Wrapped.decode)
58+
public static func decode(_ value: Wrapped.RawValue?) throws -> Wrapped.ConvertedValue? {
59+
try value.map(Wrapped.decode)
6060
}
6161
}
6262

Tests/AsyncHTTPTests/ConvertedTests.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ private struct ISO8601: TwoWayConversionStrategy {
8282
formatter.string(from: value)
8383
}
8484

85-
static func decode(_ value: String) -> Date {
86-
formatter.date(from: value)!
85+
static func decode(_ value: String) throws -> Date {
86+
guard let date = formatter.date(from: value) else {
87+
throw ConversionError()
88+
}
89+
return date
8790
}
91+
92+
struct ConversionError: Error {}
8893
}

Tests/AsyncHTTPTests/HTTPFormatterTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class HTTPFormatterTests: XCTestCase {
2020
// Given
2121
let request = try HTTPRequest(url: URL(string: "https://google.com")!)!.configured { request in
2222
request[header: .userAgent] = "Safari"
23-
request.body = try .json(["a": "b", "c": 1])
23+
request.body = try .json(["a": "b", "c": 1], options: [.sortedKeys, .prettyPrinted])
2424
}
2525

2626
// When
@@ -32,7 +32,10 @@ final class HTTPFormatterTests: XCTestCase {
3232
Content-Type: application/json; charset="utf-8"
3333
User-Agent: Safari
3434
35-
{"a":"b","c":1}
35+
{
36+
"a" : "b",
37+
"c" : 1
38+
}
3639
"""#)
3740
}
3841

0 commit comments

Comments
 (0)