Skip to content

Instantly share code, notes, and snippets.

@jpmartha
Created April 15, 2016 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmartha/eaea62ec21d97baa78f7ff8a882084b2 to your computer and use it in GitHub Desktop.
Save jpmartha/eaea62ec21d97baa78f7ff8a882084b2 to your computer and use it in GitHub Desktop.
diff --git a/Sources/Decodable.swift b/Sources/Decodable.swift
index 25da5a0..4c53acc 100644
--- a/Sources/Decodable.swift
+++ b/Sources/Decodable.swift
@@ -17,11 +17,11 @@ extension Decodable {
/// - Throws: DecodeError or an arbitrary ErrorType
public static func decodeValue(JSON: AnyJSON) throws -> Self {
let extractor = Extractor(JSON)
- return try self.decode(extractor)
+ return try self.decode(e: extractor)
}
/// - Throws: DecodeError or an arbitrary ErrorType
public static func decodeValue(JSON: AnyJSON, rootKeyPath: KeyPath) throws -> Self {
- return try Extractor(JSON).value(rootKeyPath)
+ return try Extractor(JSON).value(keyPath: rootKeyPath)
}
}
diff --git a/Sources/DecodeError.swift b/Sources/DecodeError.swift
index df99563..269157d 100644
--- a/Sources/DecodeError.swift
+++ b/Sources/DecodeError.swift
@@ -6,7 +6,7 @@
// Copyright © 2015 Syo Ikeda. All rights reserved.
//
-public enum DecodeError: ErrorType {
+public enum DecodeError: ErrorProtocol {
case MissingKeyPath(KeyPath)
case TypeMismatch(expected: String, actual: String, keyPath: KeyPath)
case Custom(String)
diff --git a/Sources/Extractor.swift b/Sources/Extractor.swift
index 4b7a83e..a89718b 100644
--- a/Sources/Extractor.swift
+++ b/Sources/Extractor.swift
@@ -35,12 +35,12 @@ public struct Extractor {
}
let components = ArraySlice(keyPath.components)
- return valueFor(components, rawValue)
+ return valueFor(keyPathComponents: components, rawValue)
}
/// - Throws: DecodeError or an arbitrary ErrorType
public func value<T: Decodable>(keyPath: KeyPath) throws -> T {
- guard let rawValue = try _rawValue(keyPath) else {
+ guard let rawValue = try _rawValue(keyPath: keyPath) else {
throw DecodeError.MissingKeyPath(keyPath)
}
@@ -56,7 +56,7 @@ public struct Extractor {
/// - Throws: DecodeError or an arbitrary ErrorType
public func valueOptional<T: Decodable>(keyPath: KeyPath) throws -> T? {
do {
- return try value(keyPath) as T
+ return try value(keyPath: keyPath) as T
} catch let DecodeError.MissingKeyPath(missing) where missing == keyPath {
return nil
}
@@ -64,7 +64,7 @@ public struct Extractor {
/// - Throws: DecodeError or an arbitrary ErrorType
public func array<T: Decodable>(keyPath: KeyPath) throws -> [T] {
- guard let array: [T] = try arrayOptional(keyPath) else {
+ guard let array: [T] = try arrayOptional(keyPath: keyPath) else {
throw DecodeError.MissingKeyPath(keyPath)
}
@@ -73,12 +73,12 @@ public struct Extractor {
/// - Throws: DecodeError or an arbitrary ErrorType
public func arrayOptional<T: Decodable>(keyPath: KeyPath) throws -> [T]? {
- return try _rawValue(keyPath).map([T].decode)
+ return try _rawValue(keyPath: keyPath).map([T].decode)
}
/// - Throws: DecodeError or an arbitrary ErrorType
public func dictionary<T: Decodable>(keyPath: KeyPath) throws -> [String: T] {
- guard let dictionary: [String: T] = try dictionaryOptional(keyPath) else {
+ guard let dictionary: [String: T] = try dictionaryOptional(keyPath: keyPath) else {
throw DecodeError.MissingKeyPath(keyPath)
}
@@ -87,7 +87,7 @@ public struct Extractor {
/// - Throws: DecodeError or an arbitrary ErrorType
public func dictionaryOptional<T: Decodable>(keyPath: KeyPath) throws -> [String: T]? {
- return try _rawValue(keyPath).map([String: T].decode)
+ return try _rawValue(keyPath: keyPath).map([String: T].decode)
}
}
@@ -107,7 +107,7 @@ extension Extractor: CustomStringConvertible {
//
// `ArraySlice` is used for performance optimization.
// See https://gist.github.com/norio-nomura/d9ec7212f2cfde3fb662.
-private func valueFor<C: CollectionType where C.Generator.Element == String, C.SubSequence == C>(keyPathComponents: C, _ JSON: AnyJSON) -> AnyJSON? {
+private func valueFor<C: Collection where C.Generator.Element == String, C.SubSequence == C>(keyPathComponents: C, _ JSON: AnyJSON) -> AnyJSON? {
#if os(Linux)
guard let
first = keyPathComponents.first,
diff --git a/Sources/NSNumber.swift b/Sources/NSNumber.swift
index e5ef43a..d8b32af 100644
--- a/Sources/NSNumber.swift
+++ b/Sources/NSNumber.swift
@@ -16,48 +16,48 @@ extension NSNumber: Decodable {
extension Int8: Decodable {
public static func decode(e: Extractor) throws -> Int8 {
- return try NSNumber.decode(e).charValue
+ return try NSNumber.decode(e: e).charValue
}
}
extension UInt8: Decodable {
public static func decode(e: Extractor) throws -> UInt8 {
- return try NSNumber.decode(e).unsignedCharValue
+ return try NSNumber.decode(e: e).unsignedCharValue
}
}
extension Int16: Decodable {
public static func decode(e: Extractor) throws -> Int16 {
- return try NSNumber.decode(e).shortValue
+ return try NSNumber.decode(e: e).shortValue
}
}
extension UInt16: Decodable {
public static func decode(e: Extractor) throws -> UInt16 {
- return try NSNumber.decode(e).unsignedShortValue
+ return try NSNumber.decode(e: e).unsignedShortValue
}
}
extension Int32: Decodable {
public static func decode(e: Extractor) throws -> Int32 {
- return try NSNumber.decode(e).intValue
+ return try NSNumber.decode(e: e).intValue
}
}
extension UInt32: Decodable {
public static func decode(e: Extractor) throws -> UInt32 {
- return try NSNumber.decode(e).unsignedIntValue
+ return try NSNumber.decode(e: e).unsignedIntValue
}
}
extension Int64: Decodable {
public static func decode(e: Extractor) throws -> Int64 {
- return try NSNumber.decode(e).longLongValue
+ return try NSNumber.decode(e: e).longLongValue
}
}
extension UInt64: Decodable {
public static func decode(e: Extractor) throws -> UInt64 {
- return try NSNumber.decode(e).unsignedLongLongValue
+ return try NSNumber.decode(e: e).unsignedLongLongValue
}
}
diff --git a/Sources/Operators.swift b/Sources/Operators.swift
index 3e408f7..6b55b97 100644
--- a/Sources/Operators.swift
+++ b/Sources/Operators.swift
@@ -15,30 +15,30 @@ infix operator <|-|? { associativity left precedence 150 }
/// - Throws: DecodeError or an arbitrary ErrorType
public func <| <T: Decodable>(e: Extractor, keyPath: KeyPath) throws -> T {
- return try e.value(keyPath)
+ return try e.value(keyPath: keyPath)
}
/// - Throws: DecodeError or an arbitrary ErrorType
public func <|? <T: Decodable>(e: Extractor, keyPath: KeyPath) throws -> T? {
- return try e.valueOptional(keyPath)
+ return try e.valueOptional(keyPath: keyPath)
}
/// - Throws: DecodeError or an arbitrary ErrorType
public func <|| <T: Decodable>(e: Extractor, keyPath: KeyPath) throws -> [T] {
- return try e.array(keyPath)
+ return try e.array(keyPath: keyPath)
}
/// - Throws: DecodeError or an arbitrary ErrorType
public func <||? <T: Decodable>(e: Extractor, keyPath: KeyPath) throws -> [T]? {
- return try e.arrayOptional(keyPath)
+ return try e.arrayOptional(keyPath: keyPath)
}
/// - Throws: DecodeError or an arbitrary ErrorType
public func <|-| <T: Decodable>(e: Extractor, keyPath: KeyPath) throws -> [String: T] {
- return try e.dictionary(keyPath)
+ return try e.dictionary(keyPath: keyPath)
}
/// - Throws: DecodeError or an arbitrary ErrorType
public func <|-|? <T: Decodable>(e: Extractor, keyPath: KeyPath) throws -> [String: T]? {
- return try e.dictionaryOptional(keyPath)
+ return try e.dictionaryOptional(keyPath: keyPath)
}
diff --git a/Sources/RawRepresentable.swift b/Sources/RawRepresentable.swift
index 51ff275..970f6ba 100644
--- a/Sources/RawRepresentable.swift
+++ b/Sources/RawRepresentable.swift
@@ -8,7 +8,7 @@
public extension RawRepresentable where Self: Decodable, RawValue: Decodable {
static func decode(e: Extractor) throws -> Self {
- let rawValue = try RawValue.decode(e)
+ let rawValue = try RawValue.decode(e: e)
guard let value = self.init(rawValue: rawValue) else {
throw typeMismatch("rawValue for \(self)", actual: rawValue, keyPath: nil)
diff --git a/Sources/StandardLib.swift b/Sources/StandardLib.swift
index 8483728..6fa40ae 100644
--- a/Sources/StandardLib.swift
+++ b/Sources/StandardLib.swift
@@ -44,7 +44,7 @@ extension Bool: Decodable {
// MARK: - Extensions
-extension CollectionType where Generator.Element: Decodable {
+extension Collection where Generator.Element: Decodable {
/// - Throws: DecodeError or an arbitrary ErrorType
public static func decode(JSON: AnyJSON) throws -> [Generator.Element] {
guard let array = JSON as? [AnyJSON] else {
@@ -69,14 +69,14 @@ extension DictionaryLiteralConvertible where Value: Decodable {
var result = [String: Value](minimumCapacity: dictionary.count)
try dictionary.forEach { key, value in
- result[key] = try Value.decodeValue(value)
+ result[key] = try Value.decodeValue(JSON: value)
}
return result
}
/// - Throws: DecodeError or an arbitrary ErrorType
public static func decode(JSON: AnyJSON, rootKeyPath: KeyPath) throws -> [String: Value] {
- return try Extractor(JSON).dictionary(rootKeyPath)
+ return try Extractor(JSON).dictionary(keyPath: rootKeyPath)
}
}
diff --git a/Sources/decode.swift b/Sources/decode.swift
index bf5a8c6..bd97d55 100644
--- a/Sources/decode.swift
+++ b/Sources/decode.swift
@@ -8,12 +8,12 @@
/// - Throws: DecodeError or an arbitrary ErrorType
public func decodeValue<T: Decodable>(JSON: AnyJSON) throws -> T {
- return try T.decodeValue(JSON)
+ return try T.decodeValue(JSON: JSON)
}
/// - Throws: DecodeError or an arbitrary ErrorType
public func decodeValue<T: Decodable>(JSON: AnyJSON, rootKeyPath: KeyPath) throws -> T {
- return try T.decodeValue(JSON, rootKeyPath: rootKeyPath)
+ return try T.decodeValue(JSON: JSON, rootKeyPath: rootKeyPath)
}
/// - Throws: DecodeError or an arbitrary ErrorType
@@ -28,12 +28,12 @@ public func decodeArray<T: Decodable>(JSON: AnyJSON, rootKeyPath: KeyPath) throw
/// - Throws: DecodeError or an arbitrary ErrorType
public func decodeDictionary<T: Decodable>(JSON: AnyJSON) throws -> [String: T] {
- return try [String: T].decode(JSON)
+ return try [String: T].decode(JSON: JSON)
}
/// - Throws: DecodeError or an arbitrary ErrorType
public func decodeDictionary<T: Decodable>(JSON: AnyJSON, rootKeyPath: KeyPath) throws -> [String: T] {
- return try [String: T].decode(JSON, rootKeyPath: rootKeyPath)
+ return try [String: T].decode(JSON: JSON, rootKeyPath: rootKeyPath)
}
// MARK: - Deprecated
diff --git a/Result/Result.swift b/Result/Result.swift
index 855e2e8..27ae4c5 100644
--- a/Result/Result.swift
+++ b/Result/Result.swift
@@ -1,7 +1,7 @@
// Copyright (c) 2015 Rob Rix. All rights reserved.
/// An enum representing either a failure with an explanatory error, or a success with a result value.
-public enum Result<T, Error: ErrorType>: ResultType, CustomStringConvertible, CustomDebugStringConvertible {
+public enum Result<T, Error: ErrorProtocol>: ResultType, CustomStringConvertible, CustomDebugStringConvertible {
case Success(T)
case Failure(Error)
@@ -140,18 +140,18 @@ public func != <T: Equatable, Error: Equatable> (left: Result<T, Error>, right:
/// Returns the value of `left` if it is a `Success`, or `right` otherwise. Short-circuits.
public func ?? <T, Error> (left: Result<T, Error>, @autoclosure right: () -> T) -> T {
- return left.recover(right())
+ return left.recover(value: right())
}
/// Returns `left` if it is a `Success`es, or `right` otherwise. Short-circuits.
public func ?? <T, Error> (left: Result<T, Error>, @autoclosure right: () -> Result<T, Error>) -> Result<T, Error> {
- return left.recoverWith(right())
+ return left.recoverWith(result: right())
}
// MARK: - Derive result from failable closure
public func materialize<T>(@noescape f: () throws -> T) -> Result<T, NSError> {
- return materialize(try f())
+ return materialize(f: try f())
}
public func materialize<T>(@autoclosure f: () throws -> T) -> Result<T, NSError> {
@@ -201,7 +201,7 @@ infix operator >>- {
///
/// This is a synonym for `flatMap`.
public func >>- <T, U, Error> (result: Result<T, Error>, @noescape transform: T -> Result<U, Error>) -> Result<U, Error> {
- return result.flatMap(transform)
+ return result.flatMap(transform: transform)
}
@@ -209,7 +209,7 @@ public func >>- <T, U, Error> (result: Result<T, Error>, @noescape transform: T
/// Make NSError conform to ErrorTypeConvertible
extension NSError: ErrorTypeConvertible {
- public static func errorFromErrorType(error: ErrorType) -> NSError {
+ public static func errorFromErrorType(error: ErrorProtocol) -> NSError {
return error as NSError
}
}
@@ -221,6 +221,6 @@ extension NSError: ErrorTypeConvertible {
/// This can be used to describe `Result`s where failures will never
/// be generated. For example, `Result<Int, NoError>` describes a result that
/// contains an `Int`eger and is guaranteed never to be a `Failure`.
-public enum NoError: ErrorType { }
+public enum NoError: ErrorProtocol { }
import Foundation
diff --git a/Result/ResultType.swift b/Result/ResultType.swift
index c7ae4f9..16d5254 100644
--- a/Result/ResultType.swift
+++ b/Result/ResultType.swift
@@ -2,8 +2,8 @@
/// A type that can represent either failure with an error or success with a result value.
public protocol ResultType {
- typealias Value
- typealias Error: ErrorType
+ associatedtype Value
+ associatedtype Error: ErrorProtocol
/// Constructs a successful result wrapping a `value`.
init(value: Value)
@@ -65,9 +65,9 @@ public extension ResultType {
}
/// Protocol used to constrain `tryMap` to `Result`s with compatible `Error`s.
-public protocol ErrorTypeConvertible: ErrorType {
+public protocol ErrorTypeConvertible: ErrorProtocol {
typealias ConvertibleType = Self
- static func errorFromErrorType(error: ErrorType) -> ConvertibleType
+ static func errorFromErrorType(error: ErrorProtocol) -> ConvertibleType
}
public extension ResultType where Error: ErrorTypeConvertible {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment