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

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
psharanda committed Mar 30, 2017
1 parent fa0e689 commit 380bc5b
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions Sources/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension Decoder {

public func decoder(forKey key: String) throws -> Decoder {
if let value = anyValue(forKey: key) {
return try Decoding.castValue(value) { $0 }
return try castValue(value) { $0 }
} else {
throw DecoderErrorType.missing.error
}
Expand All @@ -64,7 +64,7 @@ extension Decoder {
private func handleObjectDecode<T, U>(key: String, action: (T) throws -> U) throws -> U {
return try commitAction(path: .key(key)) {
if let value = anyValue(forKey: key) {
return try Decoding.castValue(value, action: action)
return try castValue(value, action: action)
} else {
throw DecoderErrorType.missing.error
}
Expand All @@ -75,7 +75,7 @@ extension Decoder {
return try commitAction(path: .key(key)) {
if let value = anyValue(forKey: key) {
return try doActionHandlingNull(value: value) {
return try Decoding.castValue(value, action: action)
return try castValue(value, action: action)
}
} else {
if nilIfMissing {
Expand All @@ -90,7 +90,7 @@ extension Decoder {
private func handleObjectDecode<T, U>(key: String, valueIfMissing: U, action: (T) throws -> U) throws -> U {
return try commitAction(path: .key(key)) {
if let value = anyValue(forKey: key) {
return try Decoding.castValue(value, action: action)
return try castValue(value, action: action)
} else {
return valueIfMissing
}
Expand Down Expand Up @@ -624,14 +624,14 @@ public struct Decoding {
try decoder.decode(transform: transform)
}
}
fileprivate static func castValue<T, U>(_ value: Any, action: (T) throws ->U) throws -> U {
if let target = value as? T {
let res = try action(target)
return res
} else {
throw DecoderErrorType.invalidType(T.self, value).error
}
}

fileprivate func castValue<T, U>(_ value: Any, action: (T) throws ->U) throws -> U {
if let target = value as? T {
let res = try action(target)
return res
} else {
throw DecoderErrorType.invalidType(T.self, value).error
}
}

Expand Down Expand Up @@ -664,14 +664,4 @@ fileprivate func doActionHandlingNull<U>(value: Any, action: () throws ->U) thro
}
}

struct Test: Decodable {
let name: String?
let names: [String]?
let naming: [String:String]?

init(decoder: Decoder) throws {
name = try decoder.decode(key: "name", valueIfMissing: "John")
names = try decoder.decode(key: "names", valueIfMissing: ["John"])
naming = try decoder.decode(key: "naming", valueIfMissing: ["First":"John"])
}
}

0 comments on commit 380bc5b

Please sign in to comment.