Swiftpack.co - Package - sunshinejr/Moya-ModelMapper

Moya-ModelMapper

CocoaPods

ModelMapper bindings for Moya for easier JSON serialization with RxSwift and ReactiveCocoa bindings.

Installation

CocoaPods

pod 'Moya-ModelMapper', '~> 10.0'

The subspec if you want to use the bindings over RxSwift.

pod 'Moya-ModelMapper/RxSwift', '~> 10.0'

And the subspec if you want to use the bindings over ReactiveSwift.

pod 'Moya-ModelMapper/ReactiveSwift', '~> 10.0'

Carthage

Specify in Cartfile:

github "sunshinejr/Moya-ModelMapper" ~> 10.0

Carthage users can point to this repository and use whichever generated framework they'd like, Moya-ModelMapper, RxMoya-ModelMapper, or ReactiveMoya-ModelMapper.

Swift Package Manager

Add the following as a dependency to your Package.swift.

.package(url: "https://github.com/sunshinejr/Moya-ModelMapper.git", .upToNextMajor(from: "10.0.0"))

The bindings are available through Moya_ModelMapper module. If you are interested in reactive extensions, use ReactiveMoya_ModelMapper or RxMoya_ModelMapper respectively.

Usage

Create a model struct or class. It needs to implement protocol Mappable.

import Foundation
import Mapper

struct Repository: Mappable {

    let identifier: Int
    let language: String? // Optional property
    let url: String? // Optional property

    init(map: Mapper) throws {
        try identifier = map.from("id")
        language = map.optionalFrom("language")
        url = map.optionalFrom("url")
    }

}

Then you have methods that extends the response from Moya. These methods are:

map(to:)
map(to:keyPath:)
compactMap(to:)
compactMap(to:keyPath)

While using map(to:) tries to map whole response data to object/array, with map(to:keyPath:) you can specify nested object in a response to fetch. For example map(to: User.self, keyPath: "data.response.user") will go through dictionary of data, through dictionary of response to dictionary of user, which it will parse. compactMap is a variant of array map that doesn't fail the whole operation if one of the objects fails, it will just remove the object from the array. RxSwift and ReactiveCocoa extensions also have all of the methods, but RxSwift have optional mapping additionally. See examples below, or in a Demo project.

1. Normal usage (without RxSwift or ReactiveCocoa)

provider = MoyaProvider<GitHub>(endpointClosure: endpointClosure)
provider.request(GitHub.repos("mjacko")) { (result) in
    if case .success(let response) = result {
        do {
            let repos = try response.map(to: [Repository].self)
            print(repos)
        } catch Error.jsonMapping(let error) {
            print(try? error.mapString())
        } catch {
            print(":(")
        }
    }
}

2. RxSwift

provider = MoyaProvider<GitHub>(endpointClosure: endpointClosure)
provider.rx.request(GitHub.repo("Moya/Moya"))
    .map(to: User.self, keyPath: "owner")
    .subscribe { event in
        switch event {
        case .success(let user):
            print(user)
        case .error(let error):
            print(error)
        }
}

Additionally, modules for RxSwift contains optional mappings. It basically means that if the mapping fails, mapper doesn't throw errors but returns nil. For instance:

provider = MoyaProvider<GitHub>(endpointClosure: endpointClosure)
provider.rx.request(GitHub.repos("mjacko"))
    .mapOptional(to: [Repository].self)
    .subscribe { event in
        switch event {
        case .success(let repos):
            // Here we can have either nil or [Repository] object.
            print(repos)
        case .error(let error):
            print(error)
        }
}

3. ReactiveSwift

provider = MoyaProvider<GitHub>(endpointClosure: endpointClosure)
provider.reactive.request(GitHub.repos("mjacko"))
    .map(to: [Repository].self)
    .observeOn(UIScheduler())
    .start { event in
        switch event {
        case .value(let repos):
            print(repos)
        case .failed(let error):
            print(error)
        default: break
        }
}

Author

Sunshinejr, thesunshinejr@gmail.com, @thesunshinejr

License

Moya-ModelMapper is available under the MIT license. See the LICENSE file for more info.

Github

link
Stars: 140

Used By

Total: 0

Releases

10.0.0 - 2020-02-18 09:52:24

Stable release 10.0.0 that uses Moya ~> 14.0. Thank you to @teameh for the help!

Changelog

  • Moya updated to 14.0.0

10.0.0-beta.2 - 2019-10-28 13:05:54

Changelog

  • Moya updated to 14.0.0-beta.5

10.0.0-beta.1 - 2019-10-07 14:58:56

Changelog

  • Moya updated to 14.0.0-beta.4
  • Remove redundant public access level in the extension.
  • Removed Swift < 4.1 compatibility.
  • Removed old mapObject(:) and mapArray(:) methods (they were giving unavailability errors anyways).

9.0.0 - 2019-05-23 12:59:23

  • Moya updated to 13.0.0

9.0.0-beta.1 - 2019-04-26 15:19:15

  • Moya updated to 13.0.0-beta.1
  • Mapper updated to 10.0
  • Swift version updated to 5.0
  • SPM tools updated to SPMv5

- 2019-02-01 10:50:36

Changes:

Moya updated to version 12.

7.1.0 - 2018-04-21 23:27:48

Changes:

Added compactMap as a variation of mapping an array that doesn't fail when one of the objects fails. All reactive extensions get them as well! 🎉

- 2018-03-07 15:30:26

Changes:

  • Fixed a bug that Single extensions still had observing on schedulers that Observable extensions had removed in Moya-ModelMapper 6.0.

- 2018-02-10 23:36:42

Changes:

  • Moya updated to 11.0.0

- 2017-10-24 07:49:38

Changes since 6.0.0-beta.1:

  • Fixed SPM integration for reactive providers.
  • Moya updated to 10.0.0

Full changelog for Moya-ModelMapper 6.0.0:

  • Breaking change Removed observing schedulers from mapping. Take a look at MigrationGuide.md.
  • Fixed SPM integration for reactive providers.
  • Updated Moya 10.0.0
  • SPM updated to SPMv4
  • Carthage support for tvOS, macOS and watchOS

- 2017-10-11 20:16:22

  • Breaking change Removed observing schedulers from mapping. Take a look at MigrationGuide.md.
  • Updated Moya 10.0.0-beta.1
  • SPM updated to SPMv4
  • Carthage support for tvOS, macOS and watchOS

5.0.0 - 2017-09-15 11:27:27

Changes since 5.0.0-beta.1:

  • Updated Moya to 9.0.0.

Full changelog for Moya-ModelMapper 5.0.0:

  • Updated Moya to 9.0.0.
  • Renamed methods from mapObject, mapArray to map(to:keyPath:).
  • Renamed RxSwift extensions method to map(to:keyPath:) as well.
  • Added Single<Response> extensions to RxSwift so Moya 9.0 feels right in usage.
  • Renamed CocoaPods subspec from Moya-ModelMapper/ReactiveCocoa to Moya-ModelMapper/ReactiveSwift. However, the former one will still work for some time as well.

5.0.0-beta.1 - 2017-09-04 20:45:12

  • Renamed methods from mapObject, mapArray to map(to:keyPath:).
  • Renamed RxSwift extensions method to map(to:keyPath:) as well.
  • Updated Moya to 9.0.0-beta.1.
  • Added Single extensions to RxSwift so Moya 9.0 feels right in usage.
  • Renamed CocoaPods subspec from Moya-ModelMapper/ReactiveCocoa to Moya
  • ModelMapper/ReactiveSwift. However, the former one will still work for some time as well.

- 2017-07-22 19:14:59

This release fixes Carthage installation.

- 2017-01-17 19:35:17

  • Update iOS min deploment to 8.0 and macOS to 10.10

- 2017-01-15 22:26:20

  • Moya updated to 8.0.0
  • ReactiveSwift updated to 1.0.0

- 2016-10-19 17:41:47

  • Moya updated to 8.0.0-beta.3
  • RxSwift updated to 3.0.0-rc.1
  • ReactiveSwift updated to 1.0.0-alpha.3

Swift 3.0! 🎉 - 2016-10-11 21:01:46

🎉 🎉 🎉

Please bear in mind that it will stay in beta as long as our dependencies are. Changelog:

  • Swift updated to 3.0 (this version supports only Swift 3.0)
  • Moya updated to 8.0.0-beta.2
  • RxSwift updated to 3.0.0-beta.2
  • ReactiveSwift updated to 1.0.0-alpha.2

KeyPath mappings, Swift update, dependencies update. - 2016-04-03 16:31:55

🎉🎉🎉

  • Add mapObject(withKeyPath:) and mapArray(withKeyPath:) methods.
  • Add mapObject(:keyPath:), mapArray(:keyPath:) to RxSwift and ReactiveCocoa extensions.
  • Use native mapping with array, instead of custom, thanks to Mapper update.
  • Update to Moya 6.4, ReactiveCocoa 4.1, RxSwift 2.3 and Mapper 2.0.

Optional mappings added to RxSwift module. - 2016-03-21 13:28:33

Carthage compatibility - 2016-02-18 15:46:30