Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thoughtless fixes for beta 6 #117

Merged
merged 1 commit into from
Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Generator/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ indirect enum Decodable {
}

let documentation = generateDocumentationComment(behaviour)
return overloads + [documentation + "public func \(operatorString) \(generics)(json: AnyObject, keyPath: \(keyPathType)) throws -> \(returnType) {\n" +
return overloads + [documentation + "public func \(operatorString) \(generics)(json: Any, keyPath: \(keyPathType)) throws -> \(returnType) {\n" +
" return try parse(json, keyPath: keyPath, decoder: \(decodeClosure(provider)))\n" +
"}"
]
Expand Down Expand Up @@ -238,7 +238,7 @@ let types = overloads.map { $0.typeString(TypeNameProvider()) }
let all = overloads.flatMap { $0.generateOverloads("=>") } + overloads.flatMap(filterOptionals).map{ $0.wrapInOptionalIfNeeded() }.flatMap { $0.generateOverloads("=>?") }

do {
var template = try String(contentsOfFile: fileManager.currentDirectoryPath + "/Templates/Header.swift") as NSString
var template = try String(contentsOfFile: fileManager.currentDirectoryPath + "/Templates/Header.swift")
template = template.replacingOccurrences(of: "{filename}", with: filename)
template = template.replacingOccurrences(of: "{by}", with: "Generator.swift")
template = template.replacingOccurrences(of: "{overloads}", with: types.joined(separator: ", "))
Expand Down
26 changes: 13 additions & 13 deletions Sources/Castable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@

import Foundation

/// Attempt to cast an `AnyObject` to `T` or throw
/// Attempt to cast an `Any` to `T` or throw
///
/// - throws: `DecodingError.typeMismatch(expected, actual, metadata)`
public func cast<T>(_ object: AnyObject) throws -> T {
public func cast<T>(_ object: Any) throws -> T {
guard let result = object as? T else {
let metadata = DecodingError.Metadata(object: object)
throw DecodingError.typeMismatch(expected: T.self, actual: object.dynamicType, metadata)
throw DecodingError.typeMismatch(expected: T.self, actual: type(of: object), metadata)
}
return result
}

public protocol DynamicDecodable {
associatedtype DecodedType
static var decoder: (AnyObject) throws -> DecodedType {get set}
static var decoder: (Any) throws -> DecodedType {get set}
}

extension Decodable where Self: DynamicDecodable, Self.DecodedType == Self {
public static func decode(_ json: AnyObject) throws -> Self {
public static func decode(_ json: Any) throws -> Self {
return try decoder(json)

}
}

extension String: Decodable, DynamicDecodable {
public static var decoder: (AnyObject) throws -> String = { try cast($0) }
public static var decoder: (Any) throws -> String = { try cast($0) }
}
extension Int: Decodable, DynamicDecodable {
public static var decoder: (AnyObject) throws -> Int = { try cast($0) }
public static var decoder: (Any) throws -> Int = { try cast($0) }
}
extension Double: Decodable, DynamicDecodable {
public static var decoder: (AnyObject) throws -> Double = { try cast($0) }
public static var decoder: (Any) throws -> Double = { try cast($0) }
}
extension Bool: Decodable, DynamicDecodable {
public static var decoder: (AnyObject) throws -> Bool = { try cast($0) }
public static var decoder: (Any) throws -> Bool = { try cast($0) }
}

private let iso8601DateFormatter: DateFormatter = {
Expand All @@ -52,7 +52,7 @@ private let iso8601DateFormatter: DateFormatter = {
}()

extension Date: Decodable, DynamicDecodable {
public static var decoder: (AnyObject) throws -> Date = { object in
public static var decoder: (Any) throws -> Date = { object in
let string = try String.decode(object)
guard let date = iso8601DateFormatter.date(from: string) else {
let metadata = DecodingError.Metadata(object: object)
Expand All @@ -63,14 +63,14 @@ extension Date: Decodable, DynamicDecodable {
}

extension NSDictionary: Decodable {
public static func decode(_ json: AnyObject) throws -> Self {
public static func decode(_ json: Any) throws -> Self {
return try cast(json)
}
}

extension NSArray: DynamicDecodable {
public static var decoder: (AnyObject) throws -> NSArray = { try cast($0) }
public static func decode(_ json: AnyObject) throws -> NSArray {
public static var decoder: (Any) throws -> NSArray = { try cast($0) }
public static func decode(_ json: Any) throws -> NSArray {
return try decoder(json)
}

Expand Down
16 changes: 8 additions & 8 deletions Sources/Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
import Foundation

public protocol Decodable {
static func decode(_ json: AnyObject) throws -> Self
static func decode(_ json: Any) throws -> Self
}


extension Dictionary where Key: Decodable, Value: Decodable {
public static func decode(_ j: AnyObject) throws -> Dictionary {
public static func decode(_ j: Any) throws -> Dictionary {
return try Dictionary.decoder(key: Key.decode, value: Value.decode)(j)
}
}

extension Dictionary where Key: Decodable, Value: AnyObject {
extension Dictionary where Key: Decodable, Value: Any {

public static func decode(_ j: AnyObject) throws -> Dictionary {
let valueDecoder: (AnyObject) throws -> Value = { try cast($0) }
public static func decode(_ j: Any) throws -> Dictionary {
let valueDecoder: (Any) throws -> Value = { try cast($0) }
return try Dictionary.decoder(key: Key.decode, value: valueDecoder)(j)
}
}

extension Array where Element: Decodable {
public static func decode(_ j: AnyObject, ignoreInvalidObjects: Bool = false) throws -> [Element] {
public static func decode(_ j: Any, ignoreInvalidObjects: Bool = false) throws -> [Element] {
if ignoreInvalidObjects {
return try [Element?].decoder { try? Element.decode($0) }(j).flatMap {$0}
} else {
Expand All @@ -43,7 +43,7 @@ extension Array where Element: Decodable {
// MARK: Helpers

/// Attempt to decode one of multiple objects in order until: A: we get a positive match, B: we throw an exception if the last object does not decode
public func decodeAsOneOf(_ json: AnyObject, objectTypes: Decodable.Type...) throws -> Decodable {
public func decodeAsOneOf(_ json: Any, objectTypes: Decodable.Type...) throws -> Decodable {
for decodable in objectTypes.dropLast() {
if let decoded = try? decodable.decode(json) {
return decoded
Expand All @@ -53,7 +53,7 @@ public func decodeAsOneOf(_ json: AnyObject, objectTypes: Decodable.Type...) thr
}

/// Attempt to decode one of multiple objects in order until: A: we get a positive match, B: we throw an exception if the last object does not decode
public func decodeArrayAsOneOf(_ json: AnyObject, objectTypes: Decodable.Type...) throws -> [Decodable] {
public func decodeArrayAsOneOf(_ json: Any, objectTypes: Decodable.Type...) throws -> [Decodable] {
return try NSArray.decode(json).map {
for decodable in objectTypes.dropLast() {
if let decoded = try? decodable.decode($0) {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Decoders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension Optional {
///
/// - parameter wrappedDecoder: A decoder (decode closure) for the wrapped type
/// - returns: A closure takes an JSON object, checks it's `NSNull`, if so returns `nil`, otherwise calls the wrapped decode closure.
static func decoder(_ wrappedDecoder: (AnyObject) throws -> Wrapped) -> (AnyObject) throws -> Wrapped? {
static func decoder(_ wrappedDecoder: @escaping (Any) throws -> Wrapped) -> (Any) throws -> Wrapped? {
return { json in
if json is NSNull {
return nil
Expand All @@ -36,7 +36,7 @@ extension Array {
/// - parameter elementDecoder: A decoder (decode closure) for the `Element` type
/// - throws: if `NSArray.decode` throws or any element decode closure throws
/// - returns: A closure that takes an `NSArray` and maps it using the element decode closure
public static func decoder(_ elementDecoder: (AnyObject) throws -> Element) -> (AnyObject) throws -> Array<Element> {
public static func decoder(_ elementDecoder: @escaping (Any) throws -> Element) -> (Any) throws -> Array<Element> {
return { json in
return try NSArray.decode(json).map { try elementDecoder($0) }
}
Expand All @@ -51,7 +51,7 @@ extension Dictionary {
/// - parameter key: A decoder (decode closure) for the `Key` type
/// - parameter value: A decoder (decode closure) for the `Value` type
/// - returns: A closure that takes a `NSDictionary` and "maps" it using key and value decode closures
public static func decoder(key keyDecoder: (AnyObject) throws -> Key, value valueDecoder: (AnyObject) throws -> Value) -> (AnyObject) throws -> Dictionary {
public static func decoder(key keyDecoder: @escaping (Any) throws -> Key, value valueDecoder: @escaping (Any) throws -> Value) -> (Any) throws -> Dictionary {
return { json in
var dict = Dictionary()
for (key, value) in try NSDictionary.decode(json) {
Expand Down
12 changes: 6 additions & 6 deletions Sources/DecodingError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum DecodingError: Error, Equatable {
/// object graph.
public struct Metadata: Equatable {

public init(path: [String] = [], object: AnyObject, rootObject: AnyObject? = nil) {
public init(path: [String] = [], object: Any, rootObject: Any? = nil) {
self.path = path
self.object = object
self.rootObject = rootObject
Expand All @@ -25,18 +25,18 @@ public enum DecodingError: Error, Equatable {
public var path: [String]

/// The JSON object that failed to be decoded
public let object: AnyObject
public let object: Any

/// The root JSON object for which the `path` can be used to find `object`
public var rootObject: AnyObject?
public var rootObject: Any?

/// Represents the path to the object that failed decoding with "." as a separator.
public var formattedPath: String {
return path.joined(separator: ".")
}
}

/// Thrown when optional casting from `AnyObject` fails.
/// Thrown when optional casting from `Any` fails.
///
/// This can happen both when trying to access a key on a object
/// that isn't a `NSDictionary`, and failing to cast a `Castable`
Expand Down Expand Up @@ -110,9 +110,9 @@ public func ~=<T>(lhs: T.Type, rhs: Any.Type) -> Bool {

// FIXME: I'm not sure about === equality
public func ==(lhs: DecodingError.Metadata, rhs: DecodingError.Metadata) -> Bool {
return lhs.object === rhs.object
return lhs.object as AnyObject === rhs.object as AnyObject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this makes much sense if they're not objects.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NachoSoto In what sense? If they're not objects they get wrapped in different objects and the reference equality fails. Not 100% sure this works as intended with NSDictionaries though, as there is no test coverage of this.

Also, I appreciate your comments – thanks!

&& lhs.path == rhs.path
&& lhs.rootObject === rhs.rootObject
&& lhs.rootObject as AnyObject === rhs.rootObject as AnyObject
}

public func ==(lhs: DecodingError, rhs: DecodingError) -> Bool {
Expand Down
4 changes: 2 additions & 2 deletions Sources/NSValueCastable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public protocol NSNumberCastable: NSValueCastable {

extension NSValueCastable {
private typealias PointerOfSelf = UnsafeMutablePointer<Self> // Why do we have to do this?
public static func decode(_ j: AnyObject) throws -> Self {
public static func decode(_ j: Any) throws -> Self {
let value: NSValue = try cast(j)
let pointer = PointerOfSelf.allocate(capacity: 1)
defer { pointer.deallocate(capacity: 1) }
Expand All @@ -53,7 +53,7 @@ extension NSValueCastable {
}

extension NSNumberCastable {
public static func decode(_ json: AnyObject) throws -> Self {
public static func decode(_ json: Any) throws -> Self {
return try convertFrom(cast(json))
}
}
13 changes: 9 additions & 4 deletions Sources/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ import Foundation

// MARK: - Operators

infix operator => { associativity right precedence 150 }
infix operator =>? { associativity right precedence 150 }
precedencegroup DecodingPrecendence {
associativity: right
higherThan: CastingPrecedence
}

infix operator => : DecodingPrecendence
infix operator =>? : DecodingPrecendence

public func => (lhs: AnyObject, rhs: KeyPath) throws -> AnyObject {
public func => (lhs: Any, rhs: KeyPath) throws -> Any {
return try parse(lhs, keyPath: rhs, decoder: { $0 })
}


public func =>? (lhs: AnyObject, rhs: OptionalKeyPath) throws -> AnyObject? {
public func =>? (lhs: Any, rhs: OptionalKeyPath) throws -> Any? {
return try parse(lhs, keyPath: rhs, decoder: { $0 })
}

Expand Down
Loading