Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Deprecate manual raw representable functions. #8
Browse files Browse the repository at this point in the history
  • Loading branch information
calebd committed Nov 20, 2015
1 parent de0a1aa commit 69c4916
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Alexander/DecoderType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ public struct NSURLDecoder: DecoderType {
return JSON.stringValue.flatMap({ NSURL(string: $0) })
}
}

public struct RawRepresentableDecoder<T: RawRepresentable>: DecoderType {
public typealias Value = T
public static func decode(JSON: Alexander.JSON) -> Value? {
return (JSON.object as? T.RawValue).flatMap(T.init)
}
}
10 changes: 10 additions & 0 deletions Alexander/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ public extension Alexander.JSON {
public func decodeArray<T: JSONDecodable>(type: T.Type) -> [T]? {
return decodeArray(T.decode)
}

@available(*, deprecated, message = "Use decode(RawRepresentableDecoder<T>) instead.")
public func decode<T: RawRepresentable>(type: T.Type) -> T? {
return decode(RawRepresentableDecoder)
}

@available(*, deprecated, message = "Use decode(RawRepresentableDecoder<T>) instead.")
public func decodeArray<T: RawRepresentable>(type: T.Type) -> [T]? {
return decodeArray(RawRepresentableDecoder)
}
}
8 changes: 0 additions & 8 deletions Alexander/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ public struct JSON {
.map(transform)
.flatMap({ $0 })
}

public func decode<T: RawRepresentable>(type: T.Type) -> T? {
return (object as? T.RawValue).flatMap(T.init)
}

public func decodeArray<T: RawRepresentable>(type: T.Type) -> [T]? {
return (object as? [T.RawValue])?.lazy.map(T.init).flatMap({ $0 })
}
}

extension JSON {
Expand Down
6 changes: 3 additions & 3 deletions Tests/AlexanderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ final class AlexanderTests: XCTestCase {
case Fall = "fall"
}

XCTAssertEqual(JSON(object: "summer").decode(Season), .Summer)
XCTAssertNil(JSON(object: "wrong").decode(Season))
XCTAssertEqual(JSON(object: "summer").decode(RawRepresentableDecoder<Season>), .Summer)
XCTAssertNil(JSON(object: "wrong").decode(RawRepresentableDecoder<Season>))
}

func testDecodeRawRepresentableArray() {
Expand All @@ -73,7 +73,7 @@ final class AlexanderTests: XCTestCase {

let seasons = [ "winter", "summer", "spring", "wrong" ]
let JSON = Alexander.JSON(object: seasons)
guard let decodedSeasons = JSON.decodeArray(Season) else {
guard let decodedSeasons = JSON.decodeArray(RawRepresentableDecoder<Season>) else {
XCTFail()
return
}
Expand Down

0 comments on commit 69c4916

Please sign in to comment.