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.
CodableMapper
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
andCodableMapper
; these are analogous to howDecodable
,Encodable
andCodable
protocols work, where the last is the combination of the previous protocols.To use
DecodableMapper
you must specify aDecodableMapperProvider
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:Notice that you still need to make the
Person
struct conform toDecodable
, 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 aEncodableMapperProvider
conformant class/struct, which does the opposite mapping (from specialized value to raw Data):As well, the
Person
struct must conform toEncodable
.And finally we have
CodableMapper
, which is analougous toCodable
, where the mapper provider must conform to bothDecodableMapperProvider
andEncodableMapperProvider
. Usage is pretty similar to the previous ones: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 😊
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.Help & Feedback
CodableMapper
.