Skip to content

Commit

Permalink
Merge pull request #127 from ikesyo/casting
Browse files Browse the repository at this point in the history
Make `castOrFail` function public to support class cluster for now
  • Loading branch information
ikesyo authored Jun 20, 2016
2 parents 06271ab + 861737d commit 1598dd9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
10 changes: 10 additions & 0 deletions Himotoki.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
CDDDEE181BFC71500058595B /* NSNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDDEE161BFC71500058595B /* NSNumber.swift */; };
CDDDEE191BFC71500058595B /* NSNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDDEE161BFC71500058595B /* NSNumber.swift */; };
CDDDEE1A1BFC71500058595B /* NSNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDDEE161BFC71500058595B /* NSNumber.swift */; };
CDDFC2671D183D3900E119B0 /* Casting.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDFC2661D183D3900E119B0 /* Casting.swift */; };
CDDFC2681D183D3900E119B0 /* Casting.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDFC2661D183D3900E119B0 /* Casting.swift */; };
CDDFC2691D183D3900E119B0 /* Casting.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDFC2661D183D3900E119B0 /* Casting.swift */; };
CDDFC26A1D183D3900E119B0 /* Casting.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDFC2661D183D3900E119B0 /* Casting.swift */; };
CDE4238F1BF2E9B000C9473F /* KeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD6DA5E01B215B7C008B3EC5 /* KeyPath.swift */; };
CDE423901BF2E9B000C9473F /* Extractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD929A551AF4A4C9002F5C53 /* Extractor.swift */; };
CDE423911BF2E9B000C9473F /* decode.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDC4E7861B0ACB42007F8F88 /* decode.swift */; };
Expand Down Expand Up @@ -151,6 +155,7 @@
CDD3962A1C0C0CD000A3A3DD /* NestedObjectParsingTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NestedObjectParsingTest.swift; sourceTree = "<group>"; };
CDDDEE101BFC70B00058595B /* StandardLib.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StandardLib.swift; sourceTree = "<group>"; };
CDDDEE161BFC71500058595B /* NSNumber.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSNumber.swift; sourceTree = "<group>"; };
CDDFC2661D183D3900E119B0 /* Casting.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Casting.swift; sourceTree = "<group>"; };
CDE4239E1BF2E9B000C9473F /* Himotoki.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Himotoki.framework; sourceTree = BUILT_PRODUCTS_DIR; };
CDE423A11BF2EA0A00C9473F /* tvOS-Application.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "tvOS-Application.xcconfig"; sourceTree = "<group>"; };
CDE423A21BF2EA0A00C9473F /* tvOS-Base.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "tvOS-Base.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -365,6 +370,7 @@
isa = PBXGroup;
children = (
CD9ADBF81B00F8F600406AA5 /* Builder.swift */,
CDDFC2661D183D3900E119B0 /* Casting.swift */,
CD929A531AF4A488002F5C53 /* Decodable.swift */,
CDC4E7861B0ACB42007F8F88 /* decode.swift */,
CD8620E21B914C60006DCCDC /* DecodeError.swift */,
Expand Down Expand Up @@ -673,6 +679,7 @@
CD8F0DCB1B6D014B0021196A /* Decodable.swift in Sources */,
CD8F0DCC1B6D014B0021196A /* Builder.swift in Sources */,
CD8F0DCD1B6D014B0021196A /* Operators.swift in Sources */,
CDDFC2691D183D3900E119B0 /* Casting.swift in Sources */,
CD8620E51B914C60006DCCDC /* DecodeError.swift in Sources */,
CD7450201BB7FC7B0092EED3 /* RawRepresentable.swift in Sources */,
);
Expand All @@ -691,6 +698,7 @@
CD929A541AF4A488002F5C53 /* Decodable.swift in Sources */,
CD9ADBF91B00F8F600406AA5 /* Builder.swift in Sources */,
CD181ED91AF4F7C20065963F /* Operators.swift in Sources */,
CDDFC2671D183D3900E119B0 /* Casting.swift in Sources */,
CD8620E31B914C60006DCCDC /* DecodeError.swift in Sources */,
CD74501E1BB7FC7B0092EED3 /* RawRepresentable.swift in Sources */,
);
Expand Down Expand Up @@ -723,6 +731,7 @@
CDE423921BF2E9B000C9473F /* Decodable.swift in Sources */,
CDE423931BF2E9B000C9473F /* Builder.swift in Sources */,
CDE423941BF2E9B000C9473F /* Operators.swift in Sources */,
CDDFC26A1D183D3900E119B0 /* Casting.swift in Sources */,
CDE423951BF2E9B000C9473F /* DecodeError.swift in Sources */,
CDE423961BF2E9B000C9473F /* RawRepresentable.swift in Sources */,
);
Expand Down Expand Up @@ -755,6 +764,7 @@
CDF03E4C1AF607540041C3AA /* Decodable.swift in Sources */,
CD9ADBFA1B00F8F600406AA5 /* Builder.swift in Sources */,
CDF03E4E1AF607650041C3AA /* Operators.swift in Sources */,
CDDFC2681D183D3900E119B0 /* Casting.swift in Sources */,
CD8620E41B914C60006DCCDC /* DecodeError.swift in Sources */,
CD74501F1BB7FC7B0092EED3 /* RawRepresentable.swift in Sources */,
);
Expand Down
19 changes: 19 additions & 0 deletions Sources/Casting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Casting.swift
// Himotoki
//
// Created by Syo Ikeda on 6/21/16.
// Copyright © 2016 Syo Ikeda. All rights reserved.
//

internal func castOrFail<T>(e: Extractor) throws -> T {
return try castOrFail(e.rawValue)
}

public func castOrFail<T>(any: Any?) throws -> T {
guard let result = any as? T else {
throw typeMismatch("\(T.self)", actual: any, keyPath: nil)
}

return result
}
14 changes: 0 additions & 14 deletions Sources/StandardLib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,3 @@ extension DictionaryLiteralConvertible where Value: Decodable {
return try Extractor(JSON).dictionary(rootKeyPath)
}
}

// MARK: Helpers

internal func castOrFail<T>(e: Extractor) throws -> T {
return try castOrFail(e.rawValue)
}

internal func castOrFail<T>(any: Any?) throws -> T {
guard let result = any as? T else {
throw typeMismatch("\(T.self)", actual: any, keyPath: nil)
}

return result
}
2 changes: 1 addition & 1 deletion Tests/Himotoki/DecodeErrorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation
import XCTest
@testable import Himotoki
import Himotoki

extension NSURL: Decodable {
public static func decode(e: Extractor) throws -> Self {
Expand Down

0 comments on commit 1598dd9

Please sign in to comment.