All Projects → sunshinejr → Moya Modelmapper

sunshinejr / Moya Modelmapper

Licence: mit
ModelMapper bindings for Moya.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Moya Modelmapper

Moya-Gloss
Gloss bindings for Moya
Stars: ✭ 37 (-74.13%)
Mutual labels:  rxswift, moya, reactivecocoa
Swift-Viper-Weather-App
iOS app with Clean Architecture
Stars: ✭ 20 (-86.01%)
Mutual labels:  rxswift, mapper, moya
iOS Started Kit
iOS Started Kit: Clean Architecture + RxSwift + Moya
Stars: ✭ 12 (-91.61%)
Mutual labels:  rxswift, moya
GitTime
GitTime is GitHub Tracking App. Using ReactorKit, RxSwift, Moya.
Stars: ✭ 55 (-61.54%)
Mutual labels:  rxswift, moya
Swift
💻 Swift - Boilerplate Front : RxSwift, ReactorKit, JWT, Moya (Beta)
Stars: ✭ 17 (-88.11%)
Mutual labels:  rxswift, moya
Easyreact
Are you confused by the functors, applicatives, and monads in RxSwift and ReactiveCocoa? It doesn't matter, the concepts are so complicated that not many developers actually use them in normal projects. Is there an easy-to-use way to use reactive programming? EasyReact is born for this reason.
Stars: ✭ 1,616 (+1030.07%)
Mutual labels:  rxswift, reactivecocoa
Zhuishushenqi
追书神器Swift版客户端(非官方)。 不断更新中......
Stars: ✭ 196 (+37.06%)
Mutual labels:  rxswift, reactivecocoa
Rxnetwork
A swift network library based on Moya/RxSwift.
Stars: ✭ 43 (-69.93%)
Mutual labels:  rxswift, moya
Evreflection
Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift
Stars: ✭ 954 (+567.13%)
Mutual labels:  rxswift, moya
Rxswiftexamples
Examples and resources for RxSwift.
Stars: ✭ 930 (+550.35%)
Mutual labels:  rxswift, moya
Swifthub
GitHub iOS client in RxSwift and MVVM-C clean architecture
Stars: ✭ 2,330 (+1529.37%)
Mutual labels:  rxswift, moya
Moyamapper
快速解析模型工具,支持RxSwift。同时支持缓存功能 【相关手册 https://MoyaMapper.github.io 】
Stars: ✭ 115 (-19.58%)
Mutual labels:  rxswift, moya
Moyasugar
🍯 Syntactic sugar for Moya
Stars: ✭ 165 (+15.38%)
Mutual labels:  rxswift, moya
Rxcodable
RxSwift wrapper for Codable
Stars: ✭ 110 (-23.08%)
Mutual labels:  rxswift, moya
Swiftrex
Swift + Redux + (Combine|RxSwift|ReactiveSwift) -> SwiftRex
Stars: ✭ 267 (+86.71%)
Mutual labels:  rxswift, reactivecocoa
Ios
A sample project demonstrating MVVM, RxSwift, Coordinator Pattern, Dependency Injection
Stars: ✭ 49 (-65.73%)
Mutual labels:  rxswift, moya
Boilerplate
Swift 4 and Using MVVM architecture(Rxswfit + Moya) to implement Github client demo.
Stars: ✭ 102 (-28.67%)
Mutual labels:  rxswift, moya
U17
仿最新V5.0有妖气漫画:Swift5(Moya+Alamofire/HandyJSON/Kingfisher/SnapKit/MJRefresh)
Stars: ✭ 112 (-21.68%)
Mutual labels:  moya
Rxflow
RxFlow is a navigation framework for iOS applications based on a Reactive Flow Coordinator pattern
Stars: ✭ 1,589 (+1011.19%)
Mutual labels:  rxswift
Rxnuke
RxSwift extensions for Nuke
Stars: ✭ 138 (-3.5%)
Mutual labels:  rxswift

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, [email protected], @thesunshinejr

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].