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

Commit

Permalink
Update Mapper for Swift 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
keith committed Oct 9, 2016
1 parent 1c9e189 commit 22fd5ac
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 171 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

## Breaking

- Swift 2.3 support
- Swift 3.0 support
[Keith Smiley](https://github.com/keith)
[#73](https://github.com/lyft/mapper/pull/73)
[#76](https://github.com/lyft/mapper/pull/76)

- Update transformation function to take `Any`
[Keith Smiley](https://github.com/keith)
[#76](https://github.com/lyft/mapper/pull/76)

## Enhancements

Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,4 @@ test-coverage:
! grep -C 10 "^\s*0" coverage.txt

test-swiftpm-osx:
SWIFT_EXEC="/Applications/Xcode.app/Contents/Developer/Toolchains/Swift_2.3.xctoolchain/usr/bin/swiftc" \
swift test
swift test
8 changes: 4 additions & 4 deletions Mapper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@
PRODUCT_NAME = Mapper;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
TVOS_DEPLOYMENT_TARGET = 9.0;
};
name = Debug;
Expand All @@ -400,7 +400,7 @@
PRODUCT_NAME = Mapper;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
TVOS_DEPLOYMENT_TARGET = 9.0;
};
name = Release;
Expand All @@ -415,7 +415,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.Lyft.MapperTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -429,7 +429,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.Lyft.MapperTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Mapper
// Conform to the Mappable protocol
struct User: Mappable {
let id: String
let photoURL: NSURL?
let photoURL: URL?

// Implement this initializer
init(map: Mapper) throws {
Expand Down Expand Up @@ -95,7 +95,7 @@ extension CLLocationCoordinate2D: Convertible {
let latitude = location["lat"] as? Double,
let longitude = location["lng"] as? Double else
{
throw MapperError.ConvertibleError(value: value, type: [String: Double].self)
throw MapperError.convertibleError(value: value, type: [String: Double].self)
}

return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
Expand Down Expand Up @@ -128,15 +128,15 @@ let place = Place.from(JSON)
```swift
private func extractFirstName(object: AnyObject?) throws -> String {
guard let fullName = object as? String else {
throw MapperError.ConvertibleError(value: object, type: String.self)
throw MapperError.convertibleError(value: object, type: String.self)
}

let parts = fullName.characters.split { $0 == " " }.map(String.init)
if let firstName = parts.first {
return firstName
}

throw MapperError.CustomError(field: nil, message: "Couldn't split the string!")
throw MapperError.customError(field: nil, message: "Couldn't split the string!")
}

struct User: Mappable {
Expand Down
11 changes: 5 additions & 6 deletions Sources/Convertible.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
The Convertible protocol defines how to convert values to custom objects
this differs from the Mappable protocol because the creation function is passed
an AnyObject, allowing your definition to accept any data, and convert it as seen fit
an Any, allowing your definition to accept any data, and convert it as seen fit

NSURL's Convertible implementation is provided by default, assuming the passed value
URL's Convertible implementation is provided by default, assuming the passed value
is a String

Example:

// Convertible implementation for custom logic to create `CLLocationCoordinate2D`s from dictionaries
extension CLLocationCoordinate2D: Convertible {
public static func fromMap(value: AnyObject?) throws -> CLLocationCoordinate2D {
public static func fromMap(value: Any) throws -> CLLocationCoordinate2D {
guard let location = value as? NSDictionary,
let latitude = (location["lat"] ?? location["latitude"]) as? Double,
let longitude = (location["lng"] ?? location["longitude"]) as? Double else
Expand All @@ -35,10 +35,9 @@ public protocol Convertible {
- parameter value: Any value (probably from the data source's value for the given field) to create
the expected object with

- throws: Any error from your custom implementation, MapperError.ConvertibleError is recommended
- throws: Any error from your custom implementation, MapperError.convertibleError is recommended

- returns: The successfully created value from the given input
*/
@warn_unused_result
static func fromMap(value: AnyObject?) throws -> ConvertedType
static func fromMap(_ value: Any) throws -> ConvertedType
}
4 changes: 2 additions & 2 deletions Sources/DefaultConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
public protocol DefaultConvertible: Convertible {}

extension DefaultConvertible {
public static func fromMap(value: AnyObject?) throws -> ConvertedType {
public static func fromMap(_ value: Any) throws -> ConvertedType {
if let object = value as? ConvertedType {
return object
}

throw MapperError.ConvertibleError(value: value, type: ConvertedType.self)
throw MapperError.convertibleError(value: value, type: ConvertedType.self)
}
}

Expand Down
25 changes: 8 additions & 17 deletions Sources/Mappable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import Foundation

public struct Thing: Mappable {
let string: String
let URL: NSURL?
let URL: URL?

public init(map: Mapper) throws {
// Attemps to convert the value for the "some_string" key to a String, if it fails
// it throws an error
try string = map.from("some_string")

// Attemps to convert the value for the "base_url" key to an NSURL, if it fails
// Attemps to convert the value for the "base_url" key to an URL, if it fails
// it assigns URL to nil
URL = map.optionalFrom("base_url")
}
Expand All @@ -24,18 +24,20 @@ public protocol Mappable {
/**
Define how your custom object is created from a Mapper object
*/
@warn_unused_result
init(map: Mapper) throws
}

public extension Mappable {
/**
Convenience method for creating Mappable objects from NSDictionaries

- parameter JSON: The JSON to create the object from

- returns: The object if it could be created, nil if creating the object threw an error
*/
@warn_unused_result
static func from(JSON: NSDictionary) -> Self?
public static func from(_ JSON: NSDictionary) -> Self? {
return try? self.init(map: Mapper(JSON: JSON))
}

/**
Convenience method for creating Mappable objects from a NSArray
Expand All @@ -44,18 +46,7 @@ public protocol Mappable {

- returns: An array of the created objects, or nil if creating threw
*/
@warn_unused_result
static func from(JSON: NSArray) -> [Self]?
}

public extension Mappable {
@warn_unused_result
public static func from(JSON: NSDictionary) -> Self? {
return try? self.init(map: Mapper(JSON: JSON))
}

@warn_unused_result
public static func from(JSON: NSArray) -> [Self]? {
public static func from(_ JSON: NSArray) -> [Self]? {
if let array = JSON as? [NSDictionary] {
return try? array.map { try self.init(map: Mapper(JSON: $0)) }
}
Expand Down
Loading

0 comments on commit 22fd5ac

Please sign in to comment.