Skip to content

A convenient way of custom mapping properties using Swift Codable and property wrappers

License

Notifications You must be signed in to change notification settings

rkreutz/CodableMapper

Repository files navigation

CodableMapper

Swift 5.1 Swift Package Manager MacOS Linux

CodableMapper is a Swift µpackage that provides an interface (through Property Wrappers) to define custom mappers for individual properties in a Codable struct.

Usage

There are three Property Wrappers provided in this package: DecodableMapper, EncodableMapper and CodableMapper; these are analogous to how Decodable, Encodable and Codable protocols work, where the last is the combination of the previous protocols.

To use DecodableMapper you must specify a DecodableMapperProvider conformant class/struct, which basically consists of a static method method to map from the value in the raw Data to the specialized value. Something like:

struct Person: Decodable {

    ...

    @DecodableMapper<CustomDateProvider>
    var dateOfBirth: Date

    ...
}

Notice that you still need to make the Person struct conform to Decodable, otherwise you won't be able to decode it (just in case it's not obvious 😅)

Similarly you may use EncodableMapper, where you must specialize it with a EncodableMapperProvider conformant class/struct, which does the opposite mapping (from specialized value to raw Data):

struct Person: Encodable {

    ...

    @EncodableMapper<CustomDateProvider>
    var dateOfBirth: Date

    ...
}

As well, the Person struct must conform to Encodable.

And finally we have CodableMapper, which is analougous to Codable, where the mapper provider must conform to both DecodableMapperProvider and EncodableMapperProvider. Usage is pretty similar to the previous ones:

struct Person: Codable {

    ...

    @CodableMapper<CustomDateProvider>
    var dateOfBirth: Date

    ...
}

Notice that you must specify different providers for optional and non-optional values, even if the mapping used is the same, unfortunately I couldn't find a nice way to reuse them so feel free to open a PR if you have any ideas on it 😊

struct Person: Codable {

    ...

    @CodableMapper<ISODateProvider>
    var dateAdded: Date

    @CodableMapper<OptionalISODateProvider>
    var lastUpdated: Date?
    
    ...
}

You may check the Tests in this repo to see how the CodableMapper Property Wrapper works in practice and how to define your own mapper providers.

Installation

Using the Swift Package Manager

Add CodableMapper as a dependency to your Package.swift file. For more information, see the Swift Package Manager documentation.

.package(url: "https://github.com/rkreutz/CodableMapper", from: "1.0.0")

Help & Feedback

  • Open an issue if you need help, if you found a bug, or if you want to discuss a feature request.
  • Open a PR if you want to make some change to CodableMapper.

About

A convenient way of custom mapping properties using Swift Codable and property wrappers

Resources

License

Stars

Watchers

Forks

Packages

No packages published