{"id":15038637,"url":"https://github.com/gperdomor/moyamodelmapper","last_synced_at":"2025-10-04T03:32:04.041Z","repository":{"id":62448054,"uuid":"82434149","full_name":"gperdomor/MoyaModelMapper","owner":"gperdomor","description":"ModelMapper bindings for Moya, RxSwift and ReactiveSwift","archived":true,"fork":false,"pushed_at":"2017-04-03T12:29:22.000Z","size":94,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-09-24T20:39:08.428Z","etag":null,"topics":["json","mapper","modelmapper","moya","reactiveswift","rxswift","swift","swift3"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gperdomor.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-19T04:07:33.000Z","updated_at":"2024-05-21T19:53:27.000Z","dependencies_parsed_at":"2022-11-29T13:20:55.916Z","dependency_job_id":null,"html_url":"https://github.com/gperdomor/MoyaModelMapper","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gperdomor%2FMoyaModelMapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gperdomor%2FMoyaModelMapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gperdomor%2FMoyaModelMapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gperdomor%2FMoyaModelMapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gperdomor","download_url":"https://codeload.github.com/gperdomor/MoyaModelMapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219867392,"owners_count":16554406,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","mapper","modelmapper","moya","reactiveswift","rxswift","swift","swift3"],"created_at":"2024-09-24T20:39:22.633Z","updated_at":"2025-10-04T03:31:58.694Z","avatar_url":"https://github.com/gperdomor.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MoyaModelMapper\n[![Build Status](https://travis-ci.org/gperdomor/MoyaModelMapper.svg?branch=master)](https://travis-ci.org/gperdomor/MoyaModelMapper)\n[![codecov](https://codecov.io/gh/gperdomor/MoyaModelMapper/branch/master/graph/badge.svg)](https://codecov.io/gh/gperdomor/MoyaModelMapper)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![CocoaPods compatible](https://img.shields.io/cocoapods/v/MoyaModelMapper.svg)](https://cocoapods.org/pods/MoyaModelMapper)\n[![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager)\n\n[ModelMapper](https://github.com/lyft/mapper) bindings for \n[Moya](https://github.com/Moya/Moya) for easier JSON serialization. Includes\n[RxSwift](https://github.com/ReactiveX/RxSwift) and [ReactiveSwift](https://github.com/ReactiveCocoa/ReactiveSwift) bindings as well.\n\n# Installation\n\n## CocoaPods\nUse the following entry in your Podfile\n```\npod 'MoyaModelMapper', '1.0.2'\n```\n\nThe subspec if you want to use the bindings over RxSwift.\n```\npod 'MoyaModelMapper/RxSwift', '1.0.2'\n```\n\nAnd the subspec if you want to use the bindings over ReactiveSwift.\n```\npod 'MoyaModelMapper/ReactiveSwift', '1.0.2'\n```\n\n# Usage\n\nCreate a model struct or class. It needs to implement protocol Mappable. More details about model creation [here](https://github.com/lyft/mapper/)\n\n```swift\nimport Foundation\nimport Mapper\n\nstruct Repository: Mappable {\n    let identifier: Int\n    let name: String\n    let fullName: String\n    let language: String? // Optional property\n\n    init(map: Mapper) throws {\n        try identifier = map.from(\"id\")\n        try name = map.from(\"name\")\n        try fullName = map.from(\"full_name\")\n        language = map.optionalFrom(\"language\") // Optional property\n    }\n}\n```\n\nThen you have methods that extends the response from Moya. These methods are:\n```swift\nmap(to: type) // map object\nmap(to: type, fromKey: key) // map object\nmap(to: [type]) // map array of objects\nmap(to: [type], fromKey: key) // map array of objects\n```\n\nWhile using `map(to: type)` tries to map whole response data to object,\nwith `map(to: type, fromKey: key)` you can specify nested object in a response to\nfetch. For example `map(to: type, fromKey: \"data.response.user\")` will go through\ndictionary of data, through dictionary of response to dictionary of user, which it\nwill parse.\n\nFor `RxSwift` and `ReactiveSwift` additionally methods for optional mapping are provided.\nThese methods are:\n\n```swift\nmapOptional(to: type)\nmapOptional(to: type, fromKey: key)\nmapOptional(to: [type])\nmapOptional(to: [type], fromKey: key)\n```\n\nSee examples below, or in a Demo project.\n\n## 1. Normal usage (without RxSwift or ReactiveSwift)\n\n```swift\nprovider = MoyaProvider\u003cGitHub\u003e()\nprovider.request(GitHub.repos(username: \"gperdomor\")) { (result) in\n    if case .success(let response) = result {\n        do {\n            let repos = try response.map(to: [Repository.self])\n            print(repos)\n        } catch Error.jsonMapping(let error) {\n            print(try? error.mapString())\n        } catch {\n            print(\":(\")\n        }\n    }\n}\n```\n\n## 2. RxSwift\n```swift\nprovider = RxMoyaProvider\u003cGitHub\u003e()\nprovider.request(GitHub.repo(fullName: \"gperdomor/sygnaler\"))\n        .map(to: Repository.self)\n        .subscribe { event in\n            switch event {\n            case .next(let repo):\n                print(repo)\n            case .error(let error):\n                print(error)\n            default: break\n            }\n        }\n```\n\nAdditionally, modules for `RxSwift` contains optional mappings. It basically means that if the mapping fails, mapper doesn't throw errors but returns nil. For instance:\n\n```swift\nprovider = RxMoyaProvider\u003cGitHub\u003e()\nprovider\n    .request(GitHub.repos(username: \"gperdomor\"))\n    .mapOptional(to: [Repository.self])\n    .subscribe { event in\n        switch event {\n        case .next(let repos):\n            // Here we can have either nil or [Repository] object.\n            print(repos)\n        case .error(let error):\n            print(error)\n        default: break\n        }\n    }\n```\n\n## 3. ReactiveSwift\n```swift\nprovider = ReactiveSwiftMoyaProvider\u003cGitHub\u003e()\nprovider\n    .request(GitHub.repos(username: \"gperdomor\"))\n    .map(to: [Repository.self])\n    .start { event in\n        switch event {\n        case .value(let repos):\n            print(repos)\n        case .failed(let error):\n            print(error)\n        default: break\n        }\n    }\n```\n\nAdditionally, modules for `ReactiveSwift` contains optional mappings. It basically means that if the mapping fails, mapper doesn't throw errors but returns nil. For instance:\n\n```swift\nprovider = ReactiveSwiftMoyaProvider\u003cGitHub\u003e()\nprovider\n    .request(GitHub.repos(username: \"gperdomor\"))\n    .mapOptional(to: [Repository.self])\n    .start { event in\n        switch event {\n        case .value(let repos):\n            // Here we can have either nil or [Repository] object.\n            print(repos)\n        case .failed(let error):\n            print(error)\n        default: break\n        }\n    }\n```\n\n## Sample Project\n\nThere's a sample project in the Demo directory. To use it, run `pod install` to download the required libraries. Have fun!\n\n## Other Mappers\n - [MoyaObjectMapper](https://github.com/gperdomor/MoyaObjectMapper): ObjectMapper bindings for Moya\n - [MoyaUnbox](https://github.com/gperdomor/MoyaUnbox): Unbox bindings for Moya\n\n## Contributing\n\nHey! Like MoyaModelMapper? Awesome! We could actually really use your help!\n\nOpen source isn't just writing code. MoyaModelMapper could use your help with any of the\nfollowing:\n\n- Finding (and reporting!) bugs.\n- New feature suggestions.\n- Answering questions on issues.\n- Documentation improvements.\n- Reviewing pull requests.\n- Helping to manage issue priorities.\n- Fixing bugs/new features.\n\nIf any of that sounds cool to you, send a pull request! After a few\ncontributions, we'll add you as an admin to the repo so you can merge pull\nrequests and help steer the ship :ship:\n\n## License\n\nMoyaModelMapper is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgperdomor%2Fmoyamodelmapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgperdomor%2Fmoyamodelmapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgperdomor%2Fmoyamodelmapper/lists"}