Skip to content
This repository has been archived by the owner on May 9, 2020. It is now read-only.

Commit

Permalink
optionals handling fix
Browse files Browse the repository at this point in the history
  • Loading branch information
psharanda committed Dec 9, 2017
1 parent 5adba93 commit 58e32e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ extension AnyDecoder {
}
}

public func decode<T: AnyDecodable>() throws -> T? {
return try decode(transform: T.init)
}

public func decode<T>(transform: (AnyDecoder) throws ->T? ) throws -> T? {
return try transform(self)
}

//MARK:- handle object decode

private func handleObjectDecode<T, U>(key: String, action: (T) throws -> U) throws -> U {
Expand Down
19 changes: 19 additions & 0 deletions Tests/AnyCoderTests/AnyCoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,25 @@ class AnyCoderTests: XCTestCase {
}
}

func testOptionalInit() {

struct TestUrl: AnyDecodable {
let url: URL?

init?(decoder: AnyDecoder) throws {
return nil
}
}

let json: [String: Any] = ["url": NSNull()]
do {
_ = try json.decode() as TestUrl?
}
catch {
XCTFail("Should haven't thrown")
}
}

func testFailedOptionalException() {

struct TestUrl: AnyDecodable {
Expand Down

0 comments on commit 58e32e8

Please sign in to comment.