Skip to content
This repository was archived by the owner on Jun 18, 2019. It is now read-only.

Commit 5bacfa2

Browse files
author
Kevin Lundberg
committed
Remove underscores from switch case, and use do-catch pattern matching for slightly cleaner code
1 parent 58bbccf commit 5bacfa2

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

Sources/Unboxer.swift

+15-19
Original file line numberDiff line numberDiff line change
@@ -244,26 +244,22 @@ private extension Unboxer {
244244

245245
throw UnboxPathError.emptyKeyPath
246246
}
247-
} catch {
248-
if case UnboxError.pathError(let pathError, let partialPath) = error {
249-
switch pathError {
250-
case .emptyKeyPath,
251-
.invalidCollectionElementType(_),
252-
.invalidDictionaryKey(_):
253-
throw UnboxError.pathError(pathError, partialPath)
254-
case .missingKey(_),
255-
.invalidValue(_, _, _),
256-
.invalidDictionaryKeyType(_),
257-
.invalidDictionaryValue(_, _, _):
258-
throw UnboxError.pathError(pathError, "\(path).\(partialPath)")
259-
case let .invalidArrayElement(_, index, _):
260-
throw UnboxError.pathError(pathError, "\(path).\(index).\(partialPath)")
261-
}
262-
} else if let pathError = error as? UnboxPathError {
263-
throw UnboxError.pathError(pathError, path.description)
247+
} catch UnboxError.pathError(let pathError, let partialPath) {
248+
switch pathError {
249+
case .emptyKeyPath,
250+
.invalidCollectionElementType,
251+
.invalidDictionaryKey:
252+
throw UnboxError.pathError(pathError, partialPath)
253+
case .missingKey,
254+
.invalidValue,
255+
.invalidDictionaryKeyType,
256+
.invalidDictionaryValue:
257+
throw UnboxError.pathError(pathError, "\(path).\(partialPath)")
258+
case let .invalidArrayElement(_, index, _):
259+
throw UnboxError.pathError(pathError, "\(path).\(index).\(partialPath)")
264260
}
265-
266-
throw error
261+
} catch let pathError as UnboxPathError {
262+
throw UnboxError.pathError(pathError, path.description)
267263
}
268264
}
269265

Tests/UnboxTests/UnboxTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ class UnboxTests: XCTestCase {
16351635
_ = try unbox(dictionary: dictionary) as Model
16361636
XCTFail("Should have thrown")
16371637
} catch {
1638-
XCTAssertEqual("\(error)", "[UnboxError] An error occurred while unboxing path \"array\": Invalid array element ([:]) at index 0.")
1638+
XCTAssertEqual("\(error)", "[UnboxError] An error occurred while unboxing path \"array\": Invalid array element ([:]) at index 0, JSON type Dictionary<AnyHashable, Any> cannot be unboxed as String")
16391639
}
16401640
}
16411641

0 commit comments

Comments
 (0)