
Axis/Map mapper library, deeply inspired by Lyft’s mapper.
Repo Zewo/Mapper, read from GitHub 23 h ago. Zewo has not joined Take My Repo.
Zewo/Mapper is a public repo on GitHub by Zewo. Axis/Map mapper library, deeply inspired by Lyft’s mapper. It declares the MIT license on GitHub. It is written mainly in Swift, has 12 stars and last changed on 2017-05-12. Nobody has reviewed it on Take My Repo yet, which says nothing about its quality. Read from GitHub on 2026-11-23.
In the maintainer's words. Shown from GitHub; Take My Repo does not keep a copy.
Mapper is a tiny yet very powerful library which allows you to create custom strongly-typed instances from any kind of structured data (JSON and other data interchange formats, for example) with only a single initializer. And vice versa - with only a single method.
Mapper extensively uses power of Swift generics and protocols, dramatically reducing the boilerplate you have to write. With Mapper, mapping is a breeze.
The maing advantage of Mapper is that you don't need to write multiple initializers to support mapping from different formats (if you've done it before - you know what I mean), thus eliminating the boilerplate and leaving only the core logic you need. With Mapper your code is safe and expressive.
And while reducing boilerplate, Mapper is also amazingly fast. It doesn't use reflection, and generics allows the compiler to optimize code in the most effective way.
Mapper itself is just a core mapping logic without any implementations. To actually use Mapper, you also have to import one of Mapper-conforming libraries. You can find a current list of them here. If you want to support Mapper for your data types, checkout Adopting Mapper short guide.
Mapper is deeply inspired by Lyft's Mapper. You can learn more about the concept behind their idea in this talk.
struct City : InMappable, OutMappable {
let name: String
let population: Int
enum MappingKeys : String, IndexPathElement {
case name, population
}
init<Source : InMap>(mapper: InMapper<Source, MappingKeys>) throws {
self.name = try mapper.map(from: .name)
self.population = try mapper.map(from: .population)
}
func outMap<Destination : OutMap>(mapper: inout OutMapper<Destination, MappingKeys>) throws {
try mapper.map(self.name, to: .name)
try mapper.map(self.population, to: .population)
}
}
enum Gender : String {
case male
case female
}
// Mappable = InMappable & OutMappable
struct Person : Mappable {
let name: String
let gender: Gender
let city: City
let identifier: Int
let isRegistered: Bool
let biographyPoints: [String]
enum MappingKeys : String, IndexPathElement {
case name, gender, city, identifier, registered, biographyPoints
}
init<Source : InMap>(mapper: InMapper<Source, MappingKeys>) throws {
self.name = try mapper.map(from: .name)
self.gender = try mapper.map(from: .gender)
self.city = try mapper.map(from: .city)
self.identifier = try mapper.map(from: .identifier)
self.isRegistered = try mapper.map(from: .registered)
self.biographyPoints = try mapper.map(from: .biographyPoints)
}
func outMap<Destination : OutMap>(mapper: inout OutMapper<Destination, Person.MappingKeys>) throws {
try mapper.map(self.name, to: .name)
try mapper.map(self.gender, to: .gender)
try mapper.map(self.city, to: .city)
try mapper.map(self.identifier, to: .identifier)
try mapper.map(self.isRegistered, to: .registered)
try mapper.map(self.biographyPoints, to: .biographyPoints)
}
}
// in-mapping
let jessy = Person(from: json)
let messi = Person(from: messagePack)
let michael = Person(from: mongoBSON)
// out-mapping
let json: JSON = try jessy.map()
let messi: MessagePack = try messi.map()
// and so on...
Mapper to your Package.swiftimport PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/Zewo/Mapper.git", majorVersion: 0, minor: 14),
]
)
Mapper allows you to map data in both ways, and so it has two major parts: in mapping (for example, JSON -> your model) and out mapping (your model -> JSON). So the two main protocols of Mapper is InMappable and OutMappable.
To use Mapper in it's full glory, first you need to define nested MappingKeys enum. MappingKeys are needed to represent keys from/to which your properties will be mapped. Using nested MappingKeys is a win for type-safety and can save you from some painful typos:
struct City {
let name: String
let population: Int
enum MappingKeys : String, IndexPathElement {
case name, population
}
}
Make sure to declare MappingKeys as IndexPathElement!
Now we're going to write mapping code. Let's start with in mapping:
extension City : InMappable {
init<Source : InMap>(mapper: InMapper<Source, MappingKeys>) throws {
self.name = try mapper.map(from: .name)
self.population = try mapper.map(from: .population)
}
}
let city = try City(from: json)
Actually, that's it! Now your City can be created from JSON, BSON, MessagePack and a whole range of other data formats. And that's all thanks to the amazing power of generics. As you see, that's why your initializer is generic. And from: .name is actually where your MappingKeys are used.
Each call to mapper is marked with try because, obviously, it can fail. In this case initializer will throw with InMapperError. If one of your properties is optional, you can just write try?.
Let's continue with out mapping:
extension City : OutMappable {
func outMap<Destination : OutMap>(mapper: inout OutMapper<Destination, MappingKeys>) throws {
try mapper.map(self.name, to: .name)
try mapper.map(self.population, to: .population)
}
}
let json: JSON = city.map()
As you see, the code is pretty similar, easy to reason about, and very expressive.
As you see, both mappers have two generic arguments: Source/Destination, which is the structured data format, and MappingKeys, which is specific MappingKeys defined for your model.
Actually, if you don't want to write that MappingKeys, we made BasicInMappable/BasicOutMappable just for you.
struct Planet : BasicInMappable, BasicOutMappable {
let radius: Int
init<Source : InMap>(mapper: BasicInMapper<Source>) throws {
self.radius = try mapper.map(from: "radius")
}
func outMap<Destination : OutMap>(mapper: inout BasicOutMapper<Destination>) throws {
try mapper.map(radius, to: "radius")
}
}
You can map array just like anything else -- simply by using .map.
struct Album : Mappable {
let songs: [String]
enum MappingKeys : String, IndexPathElement {
case songs
}
init<Source : InMap>(mapper: InMapper<Source, MappingKeys>) throws {
self.songs = try mapper.map(from: .songs)
}
func outMap<Destination : OutMap>(mapper: inout OutMapper<Destination, Album.MappingKeys>) throws {
try mapper.map(self.songs, to: .songs)
}
}
Mapper can also automatically map enums with raw values, which is neat.
enum Wood : String {
case mahogany
case koa
case cedar
case spruce
}
enum Strings : Int {
case four = 4
case six = 6
case seven = 7
}
struct Guitar : Mappable {
let wood: Wood
let strings: Strings
enum MappingKeys : String, IndexPathElement {
case wood, strings
}
init<Source : InMap>(mapper: InMapper<Source, MappingKeys>) throws {
self.wood = try mapper.map(from: .wood)
self.strings = try mapper.map(from: .strings)
}
func outMap<Destination : OutMap>(mapper: inout OutMapper<Destination, Guitar.MappingKeys>) throws {
try mapper.map(self.wood, to: .wood)
try mapper.map(self.strings, to: .strings)
}
}
MappablesCool thing about Mapper is that you can easily map instances which are itself Mappable:
struct Sport : Mappable {
let name: String
enum MappingKeys : String, IndexPathElement {
case name
}
init<Source : InMap>(mapper: InMapper<Source, MappingKeys>) throws {
self.name = try mapper.map(from: .name)
}
func outMap<Destination : OutMap>(mapper: inout OutMapper<Destination, Sport.MappingKeys>) throws {
try mapper.map(self.name, to: .name)
}
}
struct Team : Mappable {
let sport: Sport
let name: String
let foundationYear: Int
enum MappingKeys : String, IndexPathElement {
case sport
case name
case foundationYear = "foundation-year"
}
init<Source : InMap>(mapper: InMapper<Source, MappingKeys>) throws {
self.sport = try mapper.map(from: .sport)
self.name = try mapper.map(from: .name)
self.foundationYear = try mapper.map(from: .foundationYear)
}
func outMap<Destination : OutMap>(mapper: inout OutMapper<Destination, Team.MappingKeys>) throws {
try mapper.map(self.sport, to: .sport)
try mapper.map(self.name, to: .name)
try mapper.map(self.foundationYear, to: .foundationYear)
}
}
(Advanced topic)
There are some situations when you need to describe more than one way of mappings. It can be for several reasons - different sources/destinations of data, architectural restrictions and so on. For this situation we have "contextual mapping".
Let's start with something called InMappableWithContext:
enum SuperContext {
case json
case mongo
case gordon
}
struct SuperheroHelper {
let name: String
let id: Int
enum MappingKeys : String, IndexPathElement {
case name
case id, identifier, g_id
}
typealias MappingContext = SuperContext
}
extension SuperheroHelper : InMappableWithContext {
init<Source : InMap>(mapper: ContextualInMapper<Source, MappingKeys, MappingContext>) throws {
self.name = try mapper.map(from: .name)
switch mapper.context {
case .json:
self.id = try mapper.map(from: .id)
case .mongo:
self.id = try mapper.map(from: .identifier)
case .gordon:
self.id = try mapper.map(from: .g_id)
}
}
}
extension SuperheroHelper : OutMappableWithContext {
func outMap<Destination : OutMap>(mapper: inout ContextualOutMapper<Destination, SuperheroHelper.MappingKeys, MappingContext>) throws {
try mapper.map(self.name, to: .name)
switch mapper.context {
case .json:
try mapper.map(self.id, to: .id)
case .mongo:
try mapper.map(self.id, to: .identifier)
case .gordon:
try mapper.map(self.id, to: .g_id)
}
}
}
// now we can do
let robin = try BatmanHelper(from: robinJSON, withContext: .json)
// or
let robin = try BatmanHelper(from: robinMongo, withContext: .mongo)
// or whatever that is
let robin = try BatmanHelper(from: robinGordon, withContext: .gordon)
// And also
let robinJSON: JSON = try robin.map(withContext: .json)
Now let's get to something even more cool:
struct Superhero {
let name: String
let helper: SuperheroHelper
enum MappingKeys : String, IndexPathElement {
case name, helper
}
typealias MappingContext = SuperContext
}
extension Superhero : InMappableWithContext {
init<Source : InMap>(mapper: ContextualInMapper<Source, MappingKeys, MappingContext>) throws {
self.name = try mapper.map(from: .name)
self.helper = try mapper.map(from: .helper)
}
}
extension Superhero : OutMappableWithContext {
func outMap<Destination : OutMap>(mapper: inout ContextualOutMapper<Destination, MappingKeys, MappingContext>) throws {
try mapper.map(self.name, to: .name)
try mapper.map(self.helper, to: .helper)
}
}
let batman = try Superhero(from: batJSON, withContext: .json)
Noticed something strange? Yes, self.helper is mapped even though we didn't specify the context! That's because Superhero has, actually, the same context as SuperheroHelper, and is also InMappableWithContext/OutMappableWithContext, and so the right context is passed to SuperheroHelper automatically. This is context infering.
If you don't want that, you can specify the context manually:
// in
self.helper = try mapper.map(from: .helper, withContext: .json)
// out
try mapper.map(self.helper, to: .helper, withContext: .json)
Tutorial by example: making Foundation's Date conform to Mappable.
extension Date : Mappable {
public init<Source : InMap>(mapper: PlainInMapper<Source>) throws {
let interval: TimeInterval = try mapper.map()
self.init(timeIntervalSince1970: interval)
}
public func outMap<Destination : OutMap>(mapper: inout PlainOutMapper<Destination>) throws {
try mapper.map(self.timeIntervalSince1970)
}
}
Mappers take variadic parameter as index path, so it's possible to pass no index path at all. We call it "plain mapping".
And using MappableWithContext, we can even do something like that:
public enum DateMappingContext {
case timeIntervalSince1970
case timeIntervalSinceReferenceDate
}
extension Date : InMappableWithContext {
public init<Source : InMap>(mapper: PlainContextualInMapper<Source, DateMappingContext>) throws {
let interval: TimeInterval = try mapper.map()
switch mapper.context {
case .timeIntervalSince1970:
self.init(timeIntervalSince1970: interval)
case .timeIntervalSinceReferenceDate:
self.init(timeIntervalSinceReferenceDate: interval)
}
}
}
extension Date : OutMappableWithContext {
public func outMap<Destination : OutMap>(mapper: inout PlainContextualOutMapper<Destination, DateMappingContext>) throws {
switch mapper.context {
case .timeIntervalSince1970:
try mapper.map(self.timeIntervalSince1970)
case .timeIntervalSinceReferenceDate:
try mapper.map(self.timeIntervalSinceReferenceDate)
}
}
}
Mapper
Zewo/Mapper is a public repo on GitHub by Zewo. Axis/Map mapper library, deeply inspired by Lyft’s mapper.
It declares the MIT license on GitHub.
Its maker has not tagged a system, so it is unknown here. Its README says what it needs.
Open "Take it" on its page: Take My Repo writes an install plan for your machine and hands it to your AI agent (Claude Code, Codex or Cursor), which asks you before each step. Take My Repo has not tested it on any machine.
Its last change on GitHub was on 2017-05-12. GitHub counts 0 open issues and pull requests. Take My Repo does not judge from that whether it is maintained.
Nobody has reviewed it on Take My Repo yet, which says nothing about its quality.
Take My Repo cannot say: it does not audit code and has not run Mapper. What is known comes from GitHub. It declares the MIT license on GitHub. Its last change was on 2017-05-12. It has 12 stars, which measure popularity, not safety. Nobody has reviewed it on Take My Repo yet, which says nothing about its quality. Read the source before you run it. With "Take it", your AI agent shows each step and asks you before running it; README text is treated as data, not as instructions.