Skip to content

Commit

Permalink
[gardening][BinaryProject] Use JSONDecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesyo committed Dec 26, 2017
1 parent d698169 commit 8c56644
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Source/CarthageKit/BinaryJSONError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
/// Error parsing a binary-only framework JSON file, used in CarthageError.invalidBinaryJSON.
public enum BinaryJSONError: Error {
/// Unable to parse the JSON.
case invalidJSON(NSError)
case invalidJSON(Error)

/// Unable to parse a semantic version from a framework entry.
case invalidVersion(ScannableError)
Expand Down Expand Up @@ -37,7 +37,7 @@ extension BinaryJSONError: Equatable {
public static func == (lhs: BinaryJSONError, rhs: BinaryJSONError) -> Bool {
switch (lhs, rhs) {
case let (.invalidJSON(left), .invalidJSON(right)):
return left == right
return (left as NSError) == (right as NSError)

case let (.invalidVersion(left), .invalidVersion(right)):
return left == right
Expand Down
16 changes: 5 additions & 11 deletions Source/CarthageKit/BinaryProject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ import Foundation
import Result

public struct BinaryProject {
private static let jsonDecoder = JSONDecoder()

public var versions: [PinnedVersion: URL]

public static func from(jsonData: Data) -> Result<BinaryProject, BinaryJSONError> {
return Result<Any, NSError>(attempt: { try JSONSerialization.jsonObject(with: jsonData, options: []) })
.mapError(BinaryJSONError.invalidJSON)
.flatMap { json in
let error = NSError(
domain: Constants.bundleIdentifier,
code: 1,
userInfo: [NSLocalizedDescriptionKey: "Binary definition was not expected type [String: String]"]
)
return Result(json as? [String: String], failWith: BinaryJSONError.invalidJSON(error))
}
.flatMap { (json: [String: String]) -> Result<BinaryProject, BinaryJSONError> in
return Result<[String: String], AnyError>(attempt: { try jsonDecoder.decode([String: String].self, from: jsonData) })
.mapError { .invalidJSON($0.error) }
.flatMap { json -> Result<BinaryProject, BinaryJSONError> in
var versions = [PinnedVersion: URL]()

for (key, value) in json {
Expand Down
11 changes: 5 additions & 6 deletions Tests/CarthageKitTests/BinaryProjectSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ class BinaryProjectSpec: QuickSpec {

let actualError = BinaryProject.from(jsonData: jsonData).error

let error = NSError(
domain: Constants.bundleIdentifier,
code: 1,
userInfo: [NSLocalizedDescriptionKey: "Binary definition was not expected type [String: String]"]
)
expect(actualError) == .invalidJSON(error)
if case let .invalidJSON(underlyingError)? = actualError {
expect(underlyingError is DecodingError) == true
} else {
fail()
}
}

it("should fail with an invalid semantic version") {
Expand Down

0 comments on commit 8c56644

Please sign in to comment.